Begin Java Pepper
Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some changes and see them run Understand basic program flow - Create two methods in one program
What Is Programming? Computers cannot do all the wonderful things that we expect without instructions telling them what to do. Program – a set detailed of instructions telling a computer what to do Programming – designing and writing computer programs Programming language – a language used to express computer programs. (Syntax & Vocab)
A Very Simple Java Program public class JavaWorld { public static void main(String[] args) { System.out.println ("This is my first Java program."); } Class header Method header statements Close braces mark the end Open braces mark the beginning
A First Program – What Does It Do? Prints the message This is my first Java program. Ends the line System.out.println ("This is my first Java program.");
6 public class { public static void main(String[] args) { ; ; ; ; } Every executable Java program consists of a class –that contains a method named main that contains the statements (commands) to be executed Structure of Java programs
Steps to create a program Design Code Compile Run Test
Make the Program Run Compile (translate to java byte code) Run (interpreter for that OS reads java byte code and translates for machine) Source x.java Compile (javac x.java) Object x.class Execute (java x) compileexecute outputsource code Hello.java byte code Hello.class
Development Environment Blue Jay Lets you enter code and run it to get results Download at: Instructions on syllabus
Exercise Add the line “Welcome to Java” When you run the program, you will see: This is my first Java Program. Welcome to Java. Extra: –Info: print() does not go to the next line –Try splitting Welcome to Java into two statements: “Welcome” and then “ to Java.”
Comments // -> comment line ex: // this is a comment /* xxx */ comment between marks ex: /* these are a bunch of comments x=y; that line above is meaningless */ Space liberally For you to do: Add some comments to your program and see it makes no changes
Add a method What: a set of statements that has a name you can call. Why: –deal with complex code –repeatedly call the same statements –Divide responsibility among programmers How: –Add a method header with { } following: Ex: public static void method1(){ } –Write the statements inside the { }. –Call the method a few times from your main routine ex: method1();
Example of a new method public class JavaWorld { public static void main(String[ ] args) { System.out.println ("This is my first Java program."); method1(); } public static void method1(){ System.out.println (“I love Java."); } YOU TRY: add method1 to your program and have your main method call method1 a few times. Have it call method1 before and after the “This is my first java program” line. Add another print statement to method1 and see what happens.
Escape Characters 1 BUT, I really want a double quote inside my string! \” is “ - “abc\”def” - abc”def \\ is \ - “abc\def” - abc\def
Escape Characters 2 How do I get new lines and tabs? \n= new line (go to beginning of next line) \r =carriage return (go to beginning of this line) \t = tab (go to next tab stop)
Escape characters in your program Insert \t inside the quotes of a print statement in your program what prints? Insert a \\ inside the quotes of a print statement in your program what prints? Now remove the ‘\’ and see that the program wont even compile.
Your toolset Basic setup Create and call methods Print to screen including escape commands Comments Here are the programming steps you exploredHere are the programming steps you explored