Title: Java Methods
1Console Output
2Objectives
- Writing a Java program.
- How to send output to the command line console.
- Learn about escape sequences.
- Learn how to compile, debug, and execute a Java
program.
3Console 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
- _
4println 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
5The print Method
- System.out.print(Hello )
- System.out.print(World)
6The print Method
- System.out.print(Hello )
- System.out.print(World)
- Output
- HelloWorld
7The print Method
- System.out.print(Hello )
- System.out.print(World)
8The print Method
- System.out.print(Hello )
- System.out.print(World)
- Output
- Hello World
9The println Method
- System.out.println(Hello)
- System.out.println(World)
10The println Method
- System.out.println(Hello)
- System.out.println(World)
- Output
- Hello
- World
11The Symbol
21 is a Blackjack 7 7 is lucky Lucky
number 34
- A plus sign is used to connect more than one item
- System.out.println(21 is a Blackjack)
- System.out.println(3 4)
- System.out.println(3 4 is lucky)
- System.out.println(Lucky number 3 4)
12Other Math Symbol
6 Error the symbol can not be used with
Strings 7 is lucky Error the symbol can
not be used with Strings 3
- Be careful when using other math symbols.
- System.out.println(3 2)
- System.out.println(3 Hello World)
- System.out.println(11 4 is lucky)
- System.out.println(Lucky number 11 - 4)
- System.out.println(15 / 4)
13Escape 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
14Escape Sequences (Cont)
System.out.println(Hello\nWorld)
15Escape Sequences (Cont)
- System.out.println(Hello\tWorld)
Output Hello World
16Escape Sequences (Cont)
- System.out.println(\Hello\tWorld\)
Output Hello World
17Sequential Programming
The order in which statements occur is important.
- Normally program statements execute from top to
bottom. - This is called sequential control. Sequential
control is the default control structure. - There are other control structures that we will
discuss later.
18Sequential Programming (cont)
- What will be output by the following program
segment? - System.out.print(Chester )
- System.out.print(Nimitz)
Output Chester Nimitz
19Sequential Programming (cont)
- Now what will be output by the following program
segment? - System.out.print(Chester\nNimitz)
-
Output Chester Nimitz
20Sequential Programming (cont)
- Now what will be output by the following program
segment? - System.out.println(Chester\\Nimitz)
-
Output Chester\Nimitz
21Sequential Programming (cont)
- Keep in mind as you are programming that where
you place a statement determines when it will be
executed. - SEQUENTIAL CONTROL
22How Does String Concatenation Work?
23String Concatenation
Here is a typical print statement.
- System.out.print(Hello 1 2)
24String Concatenation (cont)
- System.out.print(Hello 1 2)
Hello 1
25String Concatenation (cont)
- System.out.print(Hello 1 2)
Hello 1
Hello 12
26String Concatenation (cont)
Here is another typical print statement.
- System.out.print(1 2 Hello)
27String Concatenation (cont)
- System.out.print(1 2 Hello)
3
28String Concatenation (cont)
- System.out.print(1 2 Hello)
3
3 Hello
29String Concatenation (cont)
Here is another typical print statement.
- System.out.print(Hello 3 2)
30String Concatenation (cont)
- System.out.print(Hello 3 2)
Multiplication has a higher precedence than
addition!
31String Concatenation (cont)
- System.out.print(Hello 3 2)
6
32Sequential Programming (cont)
- System.out.print(Hello 3 2)
6
Hello 6
33String Concatenation (cont)
Here is another typical print statement.
- System.out.print(Hello 6 - 4)
34String Concatenation (cont)
- System.out.print(Hello 6 - 4)
Hello 6
35String Concatenation (cont)
- System.out.print(Hello 6 - 4)
Hello 6
Compile Time Error operator - can not be applied
to String, int
36String Concatenation (cont)
How can we fix this problem?
- System.out.print(Hello 6 - 4)
37String Concatenation (cont)
Use parantheses!
- System.out.print(Hello (6 - 4))
38Sequential Programming (cont)
- System.out.print(Hello (6 - 4))
2
39Sequential Programming (cont)
- System.out.print(Hello (6 - 4))
2
Hello 2
40Java
Object Oriented Programming
Top Down Design
41Top Down Design
- Start JCreator.
- Create a new file called Lab01.java.
- Save the new file in your Lab01 folder.
42Creating A Java Class File
43Creating A Java Class File (cont)
44Creating A Java Class File (cont)
45Creating A Java Class File (cont)
46Creating A Java Class File (cont)
47Creating A Java Class File (cont)
Class names should always begin with an uppercase
letter.
48Creating A Java Class File (cont)
Click this button to set the location.
49Creating A Java Class File (cont)
50Creating A Java Class File (cont)
51Declaring A Java Class
52Declaring A Java Class
53Declaring A Java Class
54Declaring A Java Class
55The 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.
56The main Method (cont)
- public class Lab01
-
- 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.
57The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
-
58The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- System.out.println(Main Method!)
-
59Compiling A Java Class File
60Compiling A Java Class File
61Running A Java Program
62Running A Java Program
63The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- System.out.println(Main Method!)
-
- public void output()
- System.out.println(Hello World)
-
64The main Method (cont)
Run the program
- public class Lab01
-
- public static void main(String args)
-
- System.out.println(Main Method!)
-
- public void output()
- System.out.println(Hello World)
-
65The main Method (cont)
66The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- System.out.println(Main Method!)
-
- public void output()
- System.out.println(Hello World)
-
67The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- System.out.println(Main Method!)
-
- public void output()
- System.out.println(Hello World)
-
68The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
-
-
- public void output()
- System.out.println(Hello World)
-
69The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
-
-
- public void output()
- System.out.println(Hello World)
-
70The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- output()
-
- public void output()
- System.out.println(Hello World)
-
71The main Method (cont)
72The main Method (cont)
73The main Method (cont)
74The main Method (cont)
75The main Method (cont)
76The main Method (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 Lab01.
77The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- output()
-
- public void output()
- System.out.println(Hello World)
-
78The main Method (cont)
lab is an object. An object is an instance of a
class.
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
-
- public void output()
- System.out.println(Hello World)
-
79The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(Hello World)
-
80Run The Program
81The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(Hello\nWorld)
-
82The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(Hello\\World)
-
83The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(Hello World 1 2)
-
84The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(1 2 Hello World)
-
85Control Structures
86Sequential ControlBranchingConditionsLooping
Control Structures
87How The Program Executes.
88The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(1 2 Hello World)
-
89The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(1 2 Hello World)
-
90The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(1 2 Hello World)
-
91The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(1 2 Hello World)
-
92The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(1 2 Hello World)
-
93The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(1 2 Hello World)
-
94The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(1 2 Hello World)
-
95The main Method (cont)
- public class Lab01
-
- public static void main(String args)
-
- Lab01 lab new Lab01()
- lab.output()
-
- public void output()
- System.out.println(1 2 Hello World)
-
96Java
Object Oriented Programming
Control Structures
97Refers to the order in which the individual
statements of a program are executed or evaluated
Control Structures
98Control Structures
- Sequential (Default)
- Branching
- Conditional
- Repetition (looping)
99Sequential ControlStatements are executed in
the order in which they are written.
Control Structures
100System.out.println(ABC)System.out.println(DEF
)
Control Structures
101System.out.println(ABC)System.out.println(DEF
)
Control Structures
Output ABCDEF
102BranchingAllows the flow of execution to jump
to a different part of the program.
Control Structures
103public static void main(String args) Lab01
lab new Lab01() lab.output()public void
output() System.out.println(Hello World)
Control Structures
104public void methodA() methodB() methodC()
public void methodB() System.out.print(Hello
)public void methodC() System.out.print(
World)
Control Structures
105Questions?
106Java
Object Oriented Programming
Begin Lab 01