Chapter 1 Basic Computing and your first program - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 1 Basic Computing and your first program

Description:

Hard Disk Where data is saved for more permanent storage (your files and ... When they do, it assigns (single equal sign) whatever the user pressed in the variable. ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 33
Provided by: erics84
Learn more at: https://www.cs.uaf.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 1 Basic Computing and your first program


1
Chapter 1- Basic Computing and your first program
2
Overview
  • Computer Basics
  • Programming Basics
  • Java Basics and your first program.

3
Computer Basics
4
Basic Computers and terms
  • Computer is made up of two parts
  • Hardware
  • Physical equipment
  • Monitor, keyboard, memory, hard disk, processor,
    mouse.
  • Usually not changeable.
  • Software
  • The programs/information you run on the hardware.
  • Operating Systems (Windows, Linux), applications
    (Word, Photoshop), and games.
  • Modifiable.

5
Hardware components
  • Monitor/Printer (Output)
  • Shows what is happening in the computer.
  • Keyboard/Mouse/Joystick (Input)
  • Lets you interact with the computer
  • CPU (Central Processing Unit)
  • Brains of the computer.
  • Receives input from the user, performs
    operations, and directs the output devices to
    display the results.

6
Hardware Storage
  • We use storage to keep programs on our computer.
    It comes in many types
  • Main memory Where most of the calculations are
    done, temporary, wiped out when the power is
    turned off.
  • Hard Disk Where data is saved for more
    permanent storage (your files and directories are
    usually on your hard disk).
  • Removable media Usually used for archiving
    information/backing up. Can consist of tapes,
    CD-Rs, removable hard disks, ZIP disks, or
    floppies (floppies bad).

7
Hardware storage How
  • Information is stored on our computers in bits,
    or 1s and 0s.
  • A byte is 8 bits, which used to be the length
    needed to store a single character (there were
    256 characters possible).
  • The bytes are stored in a big list in memory, or
    on the hard drive. When I need a piece of
    information (say I open a midterm paper), the
    computer looks up its address in the list, and
    retrieves those 1s and 0s for me. The computer
    then interprets these bits and displays my paper.

8
Software
  • Software consists of the programs that we run on
    the computer.
  • Operating systems
  • Applications
  • Drivers
  • When we combine programs with data or input, then
    we get some sort of results or output.

9
Software programming
  • Software today is usually written in high-level
    languages, or programming languages that are
    easily read by humans. C/C, Java, Perl, VB
  • Computers dont understand these languages, so
    programs have to be translated into lower-level
    languages for the computer. Machine-language,
    assembly
  • Depending on when this translation is done, it
    is called compiling or interpreting.

10
Compiling/Interpreting
x x 1
00011101001001011000100101001011
Compiling
Interpreting
Object code Machine code
Source Program Source Code
11
Compiling vs. Interpreting
  • Compiling
  • Done before program is run.
  • Translates the whole program at once.
  • Takes longer to translate the program(compile).
  • Finished program runs faster.
  • Need to recompile for every new platform.
  • Interpreting
  • Done while program is run.
  • Translates a single line at a time.
  • Takes no time at all for any compilation.
  • Finished program runs slower.
  • No need to recompile for any new platforms.

12
How Java does it.
  • Java uses a mixture of interpretation and
    compilation.
  • Use the Java compiler to turn our high-level
    program into byte-code.
  • To run the program, we then give the byte-code to
    the Java byte-code interpreter to run the code.
  • This allows us to run our program on any OS that
    a Java byte-code interpreter has been created for
    (currently Windows, Linux, and Solaris) without
    having to recompile.

13
Large Java programs
  • When programs get big, they are often done in
    multiple pieces, each in a separate file.
  • Each file is separately compiled into its own
    piece of Java byte-code.
  • Before you can run these large programs, the
    individual byte-code files need to be linked.
  • Luckily for us, this is done behind-the-scenes
    for us, so we dont have to worry about it.

14
Programming Basics
15
How to go about creating a computer program
  • Four main steps (in this class)
  • Find out what you need to do (gather
    requirements).
  • Figure out how youll do it (design).
  • Do it (implement).
  • Check to see if youve done it correctly (test).

16
Gather Requirements.
  • Read the problem carefully!
  • Read it twice if you have to.
  • Figure out what it really wants.

17
Design
  • Start off with an English description of what you
    want to do in general.
  • Step by step, start adding more details to your
    description.
  • Continue this until you can translate this
    description into Java code.

18
Design Example
  • I want to write a program that converts
    temperatures.
  • a program that converts Fahrenheit to Celsius.
  • a program that
  • gets a value in Fahrenheit
  • converts that value to Celsius
  • outputs the Celsius value to the screen.
  • b. Celsius 5(degreesF 32)/9

19
Implement
  • Translate your design into actual Java code.

20
Test
  • Test your written program to make sure that is
    does all that the problem originally asked you to
    do.

21
Object Oriented Programming and Java.
  • Java is heavily object oriented. In fact, almost
    everything we end up doing is with objects.
  • Objects are the things that surround us books,
    cars, flashlights, etc.
  • Methods are the actions that objects can perform.
    Cars can accelerate or turn, flashlights can turn
    on and off, etc.
  • A class is a category of objects. Toyota Camry
    and Honda Accord both belong to the class of cars
    and to the class of automobiles.

22
Object Oriented Programming (OOP)
  • Composed of three principles
  • Encapsulation- I dont need to know how it works,
    just that it does.
  • Polymorphism- The same word means different
    things to different people.
  • Inheritance- You inherit characteristics from
    your ancestors.
  • We will cover these more at a later time.

23
Your first Java program.
24
FirstProgram.java
public class FirstProgram public static
void main(String args)
System.out.println("Hello out there.")
System.out.println("Want to talk some more?")
System.out.println("Answer y for yes or n
for no.") char answerLetter
answerLetter SavitchIn.readLineNonwhiteChar()
if (answerLetter 'y')
System.out.println("Nice weather we are
having.") System.out.println("Good-bye.")
System.out.println("Press enter key to
end program.") String junk junk
SavitchIn.readLine()
25
Code explanation
public class FirstProgram public static
void main(String args)
  • Always have nearly the same first two lines in
    every program for now. You will only change the
    class name (FirstProgram).
  • Since it says public class FirstProgram you must
    save this in a file called FirstProgram.java .
  • The beginning of the program is indicated by the
    main method.

26
Code Explanation
System.out.println("Hello out there.") System.out
.println("Want to talk some more?") System.out.pr
intln("Answer y for yes or n for no.")
  • System.out.println tells the computer to write
    the upcoming message out to the monitor.
  • When the program is run, these three lines will
    appear on the screen

Hello out there. Want to talk some more? Answer y
for yes or n for no.
27
Code Explanation
char answerLetter answerLetter
SavitchIn.readLineNonwhiteChar()
  • Makes answerLetter a variable that can hold a
    single character.
  • Second line waits until the user inputs a single
    character. When they do, it assigns (single equal
    sign) whatever the user pressed in the variable.

28
Code Explanation
if (answerLetter 'y') System.out.println(
"Nice weather we are having.")
  • Checks to see if what is stored in answerLetter
    (what the user entered), is equal (double equal
    sign) to the letter y.
  • If they are equal (if the user entered y) then
    it prints out the message about the weather. Else
    the weather message is not printed at all.

29
Code Explanation
System.out.println("Good-bye.") System.out.printl
n("Press enter key to end program.") String
junk junk SavitchIn.readLine()
  • Prints out a few more lines and then waits for
    the user to press the enter key before ending the
    program.
  • Depending on your computer, you may or may not
    need these last two lines. It prevents the
    computer from shutting the program down before
    the user gets a chance to see all of the output.

30
Code Explanation
  • The ending curly brackets tell the computer that
    the program has ended. For every opening curly
    bracket (), you must have a closing curly
    bracket (). If you dont the computer will
    complain of an error.

31
Java rules of syntax
  • Javas syntax is very picky.
  • Dont forget the semicolons! They usually go at
    the end of every statement.
  • Java is case sensitive. System and system are
    not the same thing in Java.

32
End of Chapter 1
  • Computer Basics
  • Programming Basics
  • A first Java program.
  • Any questions?
Write a Comment
User Comments (0)
About PowerShow.com