Today - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Today

Description:

New type can be tailored: cars vs trucks. March 2005. 9 /20 ... Usually captured in quotes. Note that 'String' is capitalized. Examples. int sampleInt = 12; ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 21
Provided by: ricks6
Category:
Tags: today

less

Transcript and Presenter's Notes

Title: Today


1
Todays 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

2
Java Program
  • Always in a file
  • Starts with public class
  • Usually contains a public static void main
  • We compile it to make it work

3
Sample Java Program
  • public class hello
  • public static void main (String args)
  • System.out.println(Hello, world!)

4
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

5
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.

6
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!

7
My History of Java
  • Interpreting expressions (1940s)
  • Sort/Merge Generator
  • A2 Compiler
  • Fortran
  • Algol
  • C
  • Then, Objects happened

8
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

9
Enough background.
  • Back to Programming
  • Printing out text
  • Statements and Variables

10
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

11
Example
  • Print one two three
  • On different lines
  • On the same line
  • On different lines using \n
  • What does println do that print doesnt?
  • Whats the difference?

12
Statements and Variables
  • Statements well 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

13
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

14
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)

15
Printing Variables
  • Our print and println methods
  • Will print out integers and strings
  • Combine strings with
  • Combine a String and int with

16
Example
  • String myname Joe
  • int age22
  • System.out.println(My name is
  • myname)
  • System.out.println(I am age
  • years old.)

17
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 .)

18
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
  • NameSally, pronounshe
  • NameFrank, pronounhe

19
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.

20
Suggestion
  • Do the first line, compile it, run it.
  • Fix what doesnt work, try again till its right.
  • Add another line, compile, run.
  • Etc.
  • Find and fix problems incrementally.
  • Its much easier than trying to fix 30 different
    errors.
Write a Comment
User Comments (0)
About PowerShow.com