Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1/20R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Revised history of JavaRevised.

Similar presentations


Presentation on theme: "March 2005 1/20R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Revised history of JavaRevised."— Presentation transcript:

1 March 2005 1/20R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Revised history of JavaRevised history of Java Overview of Statements and VariablesOverview of Statements and Variables Simple typesSimple types Printing out textPrinting out text The nuts and bolts of compiling programsThe nuts and bolts of compiling programs

2 March 2005 2/20R. Smith - University of St Thomas - Minnesota Java Program Always in a fileAlways in a file Starts with “public class”Starts with “public class” Usually contains a “public static void main”Usually contains a “public static void main” We “compile” it to make it workWe “compile” it to make it work

3 March 2005 3/20R. Smith - University of St Thomas - Minnesota Sample Java Program public class hello { public static void main (String[] args) { System.out.println(“Hello, world!”); }}

4 March 2005 4/20R. Smith - University of St Thomas - Minnesota Observations Case sensitive: match cases “exactly”Case sensitive: match cases “exactly” Program is worked “in order” from start to endProgram is worked “in order” from start to end –“Sequences” are fundamental to computing Curly braces mark “nested” componentsCurly braces mark “nested” components –“Hierarchies” are fundamental to computing Learn the “patterns” of typical programming tasksLearn the “patterns” of typical programming tasks –Setting up the program –Doing output

5 March 2005 5/20R. Smith - University of St Thomas - Minnesota The Big Picture CPU and RAMCPU and RAM Programs are sequences of statementsPrograms 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”Java is “write once, run anywhere” –The “trick” is the Java Virtual Machine.

6 March 2005 6/20R. Smith - University of St Thomas - Minnesota “Compiling” - the mechanics Should work the same on PCs and MacsShould work the same on PCs and Macs You have to “compile” programs to run themYou have to “compile” programs to run them $ javac hello.java $ You have to use the “JVM” to run them, tooYou have to use the “JVM” to run them, too $ java hello Hello, world! $

7 March 2005 7/20R. Smith - University of St Thomas - Minnesota “My” History of Java Interpreting expressions (1940s)Interpreting expressions (1940s) Sort/Merge GeneratorSort/Merge Generator A2 CompilerA2 Compiler FortranFortran AlgolAlgol C Then, Objects happenedThen, Objects happened

8 March 2005 8/20R. Smith - University of St Thomas - Minnesota Objects Programming where the data is “logically” associated with its own softwareProgramming 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” objectA “Vehicle” object –Attributes: location, direction, speed –Methods: start, stop, accelerate, checkSpeed, maxCapacity “Inheritance”“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 March 2005 9/20R. Smith - University of St Thomas - Minnesota Enough background. Back to ProgrammingBack to Programming Printing out textPrinting out text Statements and VariablesStatements and Variables

10 March 2005 10/20R. Smith - University of St Thomas - Minnesota Printing out text System.out.print - print out textSystem.out.print - print out text System.out.println - print out text w/newlineSystem.out.println - print out text w/newline “Text strings” bounded by quotes“Text strings” bounded by quotes \ = “escape character” for specials\ = “escape character” for specials \n = “new line” = starts new line\n = “new line” = starts new line \r = “carriage return” = new line on Mac\r = “carriage return” = new line on Mac \t = “tab” character = depends on tab sets\t = “tab” character = depends on tab sets \\ = puts a backslash in a text string\\ = puts a backslash in a text string

11 March 2005 11/20R. Smith - University of St Thomas - Minnesota Example Print “one…” “two…” “three…”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 does println do that print doesn’t? –What’s the difference?

12 March 2005 12/20R. Smith - University of St Thomas - Minnesota Statements and Variables Statements we’ll useStatements we’ll use –“Wrappings” like class and static void Always followed by a pair of curly bracesAlways followed by a pair of curly braces –Other statements are followed by a “;” Using “methods” or “procedures”Using “methods” or “procedures” Assignment statementsAssignment statements Variable declarationsVariable declarations VariablesVariables –Storage cells in your program –Each one is like a spreadsheet cell, only dumber

13 March 2005 13/20R. Smith - University of St Thomas - Minnesota Variable Naming Naming StructureNaming Structure First character: A-Z, a-z, _ or $First character: A-Z, a-z, _ or $ Remaining: same plus digitsRemaining: same plus digits Style rulesStyle rules Descriptive namesDescriptive names Usually start with lowercase letterUsually start with lowercase letter Camelback: use capitals to mark wordsCamelback: use capitals to mark words itemsOrdered, totalPaymentitemsOrdered, totalPayment DO NOT USE $ to start a nameDO NOT USE $ to start a name

14 March 2005 14/20R. Smith - University of St Thomas - Minnesota “Types” of variables First use: you say the typeFirst use: you say the type int = “integer”int = “integer” signed, no decimal part, less than 2Gsigned, no decimal part, less than 2G String = character stringString = character string Usually captured in quotesUsually captured in quotes Note that “String” is capitalizedNote that “String” is capitalized ExamplesExamples int sampleInt = 12; sampleInt = 25; System.out.println(sampleInt);

15 March 2005 15/20R. Smith - University of St Thomas - Minnesota Printing Variables Our print and println methods Will print out integers and strings Combine strings with “+” Combine a String and int with “+”

16 March 2005 16/20R. Smith - University of St Thomas - Minnesota Example String myname = “Joe”; int age=22; System.out.println(“My name is “ +myname); System.out.println(“I am “+age+ “ years old.”);

17 March 2005 17/20R. Smith - University of St Thomas - Minnesota 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 March 2005 18/20R. Smith - University of St Thomas - Minnesota 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

19 March 2005 19/20R. Smith - University of St Thomas - Minnesota 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 March 2005 20/20R. Smith - University of St Thomas - Minnesota Suggestion Do the first line, compile it, run it.Do the first line, compile it, run it. –Fix what doesn’t work, try again till it’s right. Add another line, compile, run.Add another line, compile, run. Etc.Etc. Find and fix problems incrementally.Find and fix problems incrementally. –It’s much easier than trying to fix 30 different errors.


Download ppt "March 2005 1/20R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Revised history of JavaRevised."

Similar presentations


Ads by Google