Java Object Oriented Programming Introduction to Java Programming
Java Object Oriented Programming Introduction to Java Programming
Objectives: q q q Writing a Java program. How to send output to the command line console. Learn how String concatenation works. Learn about escape sequences. Learn how to compile, debug, and execute a Java program. Lab 01 -2
Java Object Oriented Programming Top Down Design
Top Down Design q q q Start JCreator. Create a new file called “Lab 01. java”. Save the new file in your Lab 01 folder. Lab 01 -4
Creating A Java Class File Lab 01 -5
Creating A Java Class File Lab 01 -6
Creating A Java Class File Lab 01 -7
Creating A Java Class File (cont…) Lab 01 -8
Creating A Java Class File (cont…) Lab 01 -9
Creating A Java Class File (cont…) Lab 01 -10
Creating A Java Class File (cont…) Lab 01 -11
Creating A Java Class File (cont…) Class names should always begin with an uppercase letter. Lab 01 -12
Creating A Java Class File (cont…) Click this button to set the location. Lab 01 -13
Creating A Java Class File (cont…) Lab 01 -14
Creating A Java Class File (cont…) Lab 01 -15
Declaring A Java Class This class can be used by other classes! public class Lab 01 { } Lab 01 -16
Declaring A Java Class The keyword class defines this file as a Java class. public class Lab 01 { } Lab 01 -17
Declaring A Java Class User defined name. Class names should always begin with an uppercase letter. public class Lab 01 { } Lab 01 -18
Declaring A Java Class public class Lab 01 { The body of every class begins and ends with a set of curly brackets. } Lab 01 -19
Compiling A Java Class File Lab 01 -20
Compiling A Java Class File Lab 01 -21
Compiling A Java Class File Lab 01 -22
Running A Java Program Lab 01 -23
Running A Java Program Lab 01 -24
Running A Java Program Lab 01 -25
Java Object Oriented Programming The Main Method
The main Method • In Java, you need to have a method named main in at least one class. • This method must appear within a class, but it can be any class. • A class containing a main method is a program. Every program has a main method but not every class is a program. Lab 01 -27
The main Method (cont…) public class Lab 01 { public static void main(String[ ] args) { Curly brackets are used to define block } statements. Methods, like classes, are } block statements and must begin and end with a set of curly brackets. Lab 01 -28
The main Method (cont…) The main method is static. public class Lab 01 { public static void main(String[ ] args) { } } Lab 01 -29
Running A Java Program Lab 01 -30
Running A Java Program Lab 01 -31
Java Object Oriented Programming Console Output
Console Output In Java, console output is achieved by calling System. out. print or System. out. println. • The data to be output is given as an argument in parentheses. System. out. println(“Blackjack”); • Every invocation of println ends a line of output. Blackjack _ Lab 01 -33
println Versus print The print method is like println, except that it does not end a line • With println, the next output goes on a new line • With print, the next output goes on the same line Lab 01 -34
The main Method (cont…) public class Lab 01 { public static void main(String[ ] args) { System. out. println(“Main Method!”); } } Lab 01 -35
Running A Java Program Lab 01 -36
Java Object Oriented Programming Calling Methods
Calling Methods output is not static public class Lab 01 { public static void main(String[ ] args) { System. out. println(“Main Method!”); } public void output() { System. out. println(“Hello World”); } } Lab 01 -38
Calling methods (cont…) Run the program public class Lab 01 { public static void main(String[ ] args) { System. out. println(“Main Method!”); } public void output() { System. out. println(“Hello World”); } } Lab 01 -39
Calling methods (cont…) Lab 01 -40
Calling methods (cont…) output did not execute public class Lab 01 { public static void main(String[ ] args) { System. out. println(“Main Method!”); } public void output() { System. out. println(“Hello World”); } } Lab 01 -41
Calling methods (cont…) public class Lab 01 { public static void main(String[ ] args) { System. out. println(“Main Method!”); } public void output() { System. out. println(“Hello World”); } } Lab 01 -42
Calling methods (cont…) How do we get from here public class Lab 01 { public static void main(String[ ] args) { } public void output() { System. out. println(“Hello World”); } } Lab 01 -43
Calling methods (cont…) How do we get from here to there public class Lab 01 { public static void main(String[ ] args) { } public void output() { System. out. println(“Hello World”); } } Lab 01 -44
Calling methods (cont…) We need to call the method output() public class Lab 01 { public static void main(String[ ] args) { output(); } public void output() { System. out. println(“Hello World”); } } Lab 01 -45
Calling methods (cont…) Build the program public class Lab 01 { public static void main(String[ ] args) { output(); } public void output() { System. out. println(“Hello World”); } } Lab 01 -46
Lab 01 -47
error: non-static method output() cannot be referenced from a static context Lab 01 -48
Calling methods (cont…) Double click on the first line of the error. Lab 01 -49
Calling methods (cont…) The editor will send the cursor to the line that contains the error. Lab 01 -50
Calling methods (cont…) Static methods are class methods. Non-static methods are instance methods. Class methods can not call instance methods – however instance methods can call class methods. More on this topic later. To be able to call output we need an instance of the class Lab 01. NOTE: An instance of a class is an OBJECT. Hence the term Object Oriented Programming. Lab 01 -51
Java Object Oriented Programming Creating an instance of a class.
Creating an Object Create an instance of the class public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); } public void output() { System. out. println(“Hello World”); } } Lab 01 -53
Creating an Object (cont…) lab is an object. An object is an instance of a class. public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); } public void output() { System. out. println(“Hello World”); } } Lab 01 -54
Creating an Object (cont…) A method call to output public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(“Hello World”); } } Lab 01 -55
Creating an Object (cont…) We can call output using lab which public class Lab 01 is an instance of the class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(“Hello World”); } } Lab 01 -56
Run The Program Lab 01 -57
Java Object Oriented Programming Escape Sequencies
Escape Sequences • Escape sequences are used to print characters that are non-printable. Escape sequences always begin with a (backslash) character. • Common Escape sequences n – new line ” – quote symbol t – tab \ - backslash Lab 01 -59
Escape Sequences public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(“Hellon. World”); } } Lab 01 -60
Escape Sequences (cont…) public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(“Hello\World”); } } Lab 01 -61
Escape Sequences (cont…) public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(““Hello World””); } } Lab 01 -62
Java Object Oriented Programming String Concatenation
String Concatenation public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(“Hello World” + 1 + 2); } } Lab 01 -64
String Concatenation (cont…) public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(1 + 2 + “Hello World”); } } Lab 01 -65
Java Object Oriented Programming How Does String Concatenation Work?
The + Symbol • The plus symbol is an overloaded operator. 3 + 4 is addition and results in 7 3 + “ 7” is string concatenation and results in “ 37” Lab 01 -67
String Concatenation Here is a typical print statement. System. out. print(“Hello ” + 1 + 2); Lab 01 -68
String Concatenation (cont…) System. out. print(“Hello ” + 1 + 2); “Hello 1” Lab 01 -69
String Concatenation (cont…) System. out. print(“Hello ” + 1 + 2); “Hello 1” “Hello 12” Lab 01 -70
String Concatenation (cont…) Here is another typical print statement. System. out. print(1 + 2 + “ Hello”); Lab 01 -71
String Concatenation (cont…) System. out. print(1 + 2 + “ Hello”); 3 Lab 01 -72
String Concatenation (cont…) System. out. print(1 + 2 + “ Hello”); 3 “ 3 Hello” Lab 01 -73
String Concatenation (cont…) Here is another typical print statement. System. out. print(“Hello ” + 3 * 2); Lab 01 -74
String Concatenation (cont…) System. out. print(“Hello ” + 3 * 2); Multiplication has a higher precedence than addition! Lab 01 -75
String Concatenation (cont…) System. out. print(“Hello ” + 3 * 2); 6 Lab 01 -76
Sequential Programming (cont…) System. out. print(“Hello ” + 3 * 2); 6 “Hello 6” Lab 01 -77
String Concatenation (cont…) Here is another typical print statement. System. out. print(“Hello ” + 6 - 4); Lab 01 -78
String Concatenation (cont…) System. out. print(“Hello ” + 6 - 4); “Hello 6” Lab 01 -79
String Concatenation (cont…) System. out. print(“Hello ” + 6 - 4); “Hello 6” Compile Time Error “operator - can not be applied to String, int” Lab 01 -80
String Concatenation (cont…) How can we “fix” this problem? System. out. print(“Hello ” + 6 - 4); Lab 01 -81
String Concatenation (cont…) Use parantheses! System. out. print(“Hello ” + (6 - 4)); Lab 01 -82
String Concatenation (cont…) System. out. print(“Hello ” + (6 - 4)); 2 Lab 01 -83
String Concatenation (cont…) System. out. print(“Hello ” + (6 - 4)); 2 “Hello 2” Lab 01 -84
Java Object Oriented Programming How The Program Runs
How The Program Runs public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(1 + 2 + “Hello World”); } } Lab 01 -86
How The Program Runs (cont…) public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(1 + 2 + “Hello World”); } } Lab 01 -87
How The Program Runs (cont…) public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(1 + 2 + “Hello World”); } } Lab 01 -88
How The Program Runs (cont…) public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(1 + 2 + “Hello World”); } } Lab 01 -89
How The Program Runs (cont…) public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(1 + 2 + “Hello World”); } } Lab 01 -90
How The Program Runs (cont…) public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(1 + 2 + “Hello World”); } } Lab 01 -91
How The Program Runs (cont…) public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(1 + 2 + “Hello World”); } } Lab 01 -92
How The Program Runs (cont…) public class Lab 01 { public static void main(String[ ] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(1 + 2 + “Hello World”); } } Lab 01 -93
Java Object Oriented Programming Control Structures
Control Structures Refers to the order in which the individual statements of a program are executed or evaluated Lab 01 -95
Control Structures (cont…) • • Sequential (Default) Branching (Method Calls) Conditional (if and switch) Repetition (while & for loops) Lab 01 -96
Sequential Control: Statements are executed in the order in which they are written. Lab 01 -97
Sequential Control (cont…) System. out. println(“ABC”); System. out. println(“DEF”); Lab 01 -98
Sequential Control (cont…) System. out. println(“ABC”); System. out. println(“DEF”); Output: ABC DEF Lab 01 -99
Branching (cont…) Branching: Allows the flow of execution to jump to a different part of the program. Lab 01 -100
Branching (cont…) public static void main(String[] args) { Lab 01 lab = new Lab 01(); lab. output(); } public void output() { System. out. println(“Hello World”); } Lab 01 -101
Branching (cont…) public void method. A() { method. B(); method. C(); } public void method. B() { System. out. print(“Hello ”); } public void method. C() { System. out. print(“World”); } Lab 01 -102
Begin Every Program with Your Name in the form of a Remark Statement // John Spicer public class Lab 01 A { } Lab 01 -103
Java Object Oriented Programming Questions?
Java Object Oriented Programming Begin Lab 01
- Slides: 105