Chapter 3- Flow Control
Download
Report
Transcript Chapter 3- Flow Control
Chapter 1- Basic Computing and
your first program
Overview
Computer Basics
Programming Basics
Java Basics and your first program.
Computer Basics
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.
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.
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, CDR’s, removable hard disks, ZIP disks, or floppies
(floppies bad).
Hardware storage – How
Information is stored on our computers in “bits,” or 1’s
and 0’s.
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 1’s and 0’s for
me. The computer then interprets these bits and
displays my paper.
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.
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 don’t 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.
Compiling/Interpreting
…
x = x + 1;
…
Source Program
Source Code
Compiling
Interpreting
00011101
00100101
10001001
01001011
Object code
Machine code
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.
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.
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 don’t have to worry about it.
Programming Basics
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 you’ll do it (design).
– Do it (implement).
– Check to see if you’ve done it correctly
(test).
Gather Requirements.
Read the problem carefully!
Read it twice if you have to.
Figure out what it really wants.
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.
Design Example
I want to write a program that converts
temperatures.
…a program that converts Fahrenheit to Celsius.
…a program that
a.
b.
c.
gets a value in Fahrenheit
converts that value to Celsius
outputs the Celsius value to the screen.
… b. Celsius = 5*(degreesF – 32)/9
Implement
Translate your design into actual Java
code.
Test
Test your written program to make sure
that is does all that the problem
originally asked you to do.
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.
Object Oriented Programming
(OOP)
Composed of three principles
– Encapsulation- I don’t 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.
Your first Java program.
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();
}
}
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.
Code Explanation
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.");
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:
System.out.println
Hello out there.
Want to talk some more?
Answer y for yes or n for no.
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.
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.
Code Explanation
System.out.println("Good-bye.");
System.out.println("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.
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 don’t the
computer will complain of an error.
Java rules of syntax
Java’s syntax is very picky.
– Don’t 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.
End of Chapter 1
Computer Basics
Programming Basics
A first Java program.
Any questions?