Computer architecture, lab

Download Report

Transcript Computer architecture, lab

Today’s Class
•
•
•
•
•
•
Recap - simple Java program
Revised history of Java
Overview of Statements and Variables
Simple types
Printing out text
The nuts and bolts of compiling programs
March 2005
R. Smith - University of St Thomas - Minnesota
1/20
Java Program
•
•
•
•
Always in a file
Starts with “public class”
Usually contains a “public static void main”
We “compile” it to make it work
March 2005
R. Smith - University of St Thomas - Minnesota
2/20
Sample Java Program
public class hello
{
public static void main (String[] args)
{
System.out.println(“Hello, world!”);
}
}
March 2005
R. Smith - University of St Thomas - Minnesota
3/20
Observations
• Case sensitive: match cases “exactly”
• Program is worked “in order” from start
to end
– “Sequences” are fundamental to computing
• Curly braces mark “nested” components
– “Hierarchies” are fundamental to computing
• Learn the “patterns” of typical
programming tasks
– Setting up the program
– Doing output
March 2005
R. Smith - University of St Thomas - Minnesota
4/20
The Big Picture
• CPU and RAM
• Programs are sequences of statements
– Or commands or instructions (pick a word)
– The CPU has a built in numeric ‘machine language’
• Java is “write once, run anywhere”
– The “trick” is the Java Virtual Machine.
March 2005
R. Smith - University of St Thomas - Minnesota
5/20
“Compiling” - the mechanics
• Should work the same on PCs and Macs
• You have to “compile” programs to run them
$ javac hello.java
$
• You have to use the “JVM” to run them, too
$ java hello
Hello, world!
$
March 2005
R. Smith - University of St Thomas - Minnesota
6/20
“My” History of Java
•
•
•
•
•
•
•
Interpreting expressions (1940s)
Sort/Merge Generator
A2 Compiler
Fortran
Algol
C
Then, Objects happened
March 2005
R. Smith - University of St Thomas - Minnesota
7/20
Objects
• Programming where the data is “logically”
associated with its own software
– Attributes - specific pieces of data associated with an object
– Methods - software procedures associated with objects
• A “Vehicle” object
– Attributes: location, direction, speed
– Methods: start, stop, accelerate, checkSpeed, maxCapacity
• “Inheritance”
– You can define new types of objects in terms of old ones
– New types ‘inherit’ objects/methods of the old one
– New type can be tailored: cars vs trucks
March 2005
R. Smith - University of St Thomas - Minnesota
8/20
Enough background.
• Back to Programming
• Printing out text
• Statements and Variables
March 2005
R. Smith - University of St Thomas - Minnesota
9/20
Printing out text
• System.out.print - print out text
• System.out.println - print out text w/newline
•
•
•
•
•
•
“Text strings” bounded by quotes
\ = “escape character” for specials
\n = “new line” = starts new line
\r = “carriage return” = new line on Mac
\t = “tab” character = depends on tab sets
\\ = puts a backslash in a text string
March 2005
R. Smith - University of St Thomas - Minnesota
10/20
Example
• Print “one…” “two…” “three…”
– On different lines
– On the same line
– On different lines using \n
• What does println do that print doesn’t?
– What’s the difference?
March 2005
R. Smith - University of St Thomas - Minnesota
11/20
Statements and Variables
• Statements we’ll use
– “Wrappings” like class and static void
• Always followed by a pair of curly braces
– Other statements are followed by a “;”
• Using “methods” or “procedures”
• Assignment statements
• Variable declarations
• Variables
– Storage cells in your program
– Each one is like a spreadsheet cell,
only dumber
March 2005
R. Smith - University of St Thomas - Minnesota
12/20
Variable Naming
• Naming Structure
• First character: A-Z, a-z, _ or $
• Remaining: same plus digits
• Style rules
• Descriptive names
• Usually start with lowercase letter
• Camelback: use capitals to mark words
• itemsOrdered, totalPayment
• DO NOT USE $ to start a name
March 2005
R. Smith - University of St Thomas - Minnesota
13/20
“Types” of variables
• First use: you say the type
• int = “integer”
• signed, no decimal part, less than 2G
• String = character string
• Usually captured in quotes
• Note that “String” is capitalized
• Examples
int sampleInt = 12;
sampleInt = 25;
System.out.println(sampleInt);
March 2005
R. Smith - University of St Thomas - Minnesota
14/20
Printing Variables
Our print and println methods
Will print out integers and strings
Combine strings with “+”
Combine a String and int with “+”
March 2005
R. Smith - University of St Thomas - Minnesota
15/20
Example
String myname = “Joe”;
int age=22;
System.out.println(“My name is “
+myname);
System.out.println(“I am “+age+
“ years old.”);
March 2005
R. Smith - University of St Thomas - Minnesota
16/20
Another Example
String myname = “Joe”;
int age = 25;
System.out.print(“My name is “
+myname + “. I am “ + age +”.”);
myname = “Barb”;
age = 26
System.out.print(“My name is “
+myname + “. I am “ + age +”.”);
March 2005
R. Smith - University of St Thomas - Minnesota
17/20
Lab Problem
Given the text, write print and/or
println method calls to print it out.
Replace the name (“Jon”) and
pronoun (“he”) with string variables.
Set the variables to different values
and produce the results:
Name=Sally, pronoun=she
Name=Frank, pronoun=he
March 2005
R. Smith - University of St Thomas - Minnesota
18/20
The Text
Jon went to the store. When he arrived, he got a shopping cart.
Then he walked to the produce section. Jon was surprised. The
raspberries were fresh, so he picked up two boxes. Then he went
to the cereal section and picked up three boxes. Jon really
likes cereal. After he had visited the whole store, Jon pushed
the cart to the front. There, he paid for the groceries. It
took six bags to carry all of Jon's groceries.
The End.
March 2005
R. Smith - University of St Thomas - Minnesota
19/20
Suggestion
• Do the first line, compile it, run it.
– Fix what doesn’t work, try again till it’s right.
• Add another line, compile, run.
• Etc.
• Find and fix problems incrementally.
– It’s much easier than trying to fix 30 different
errors.
March 2005
R. Smith - University of St Thomas - Minnesota
20/20