www.daniel-deutsch.com

Download Report

Transcript www.daniel-deutsch.com

Intro to Java Midterm Review
About Java
• Compiled vs. Interpreted
• Java is compiled to Java Bytecode, not interpreted
• Bytecode is then interpreted when the program runs
• Python is interpreted, so it translates as the program runs on the computer
• The Java API
• The Java Application Programming Interface is a library of code that you can
use in Java programs
• It has things like the String class and the Scanner class
Daniel Deutsch
2
Primitives and Reserved Words
• Primitives
• Types already built into Java.
Variables can be these types
• They turn purple in jGrasp
• Integer types:
• byte, short, int, long
• Floating point types:
• float, double
• Other:
• boolean, char
Daniel Deutsch
3
Primitives and Reserved Words
• Primitives
• Reserved words
• Types already built into Java.
Variables can be these types
• They turn purple in jGrasp
• Integer types:
• You can’t use any of the words
that Java has reserved to be
“keywords” as a variable name
• All of the primitives, public, private,
protected, static, void, class, switch,
case, default, for, while, do,
continue, if, else, return, new, this,
throw, throws,
• byte, short, int, long
• Floating point types:
• float, double
• Other:
• boolean, char
Daniel Deutsch
4
Variable Names
• Variables must start with a letter, underscore or $
• After the first letter, it can be a letter, an underscore, $ or a number
Daniel Deutsch
5
Variable Names
• Variables must start with a letter, underscore or $
• After the first letter, it can be a letter, an underscore, $ or a number
• Good examples:
• Num1, DAYS_IN_YEAR, $, money$$, _scanner
Daniel Deutsch
6
Variable Names
• Variables must start with a letter, underscore or $
• After the first letter, it can be a letter, an underscore, $ or a number
• Good examples:
• Num1, DAYS_IN_YEAR, $, money$$, _scanner
• Bad examples:
• 7even, hello world, private, num1*
Daniel Deutsch
7
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
Daniel Deutsch
8
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
Daniel Deutsch
9
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
Daniel Deutsch
10
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
This means
‘a’ > ‘A’
Daniel Deutsch
11
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
This means
‘a’ > ‘A’
Daniel Deutsch
12
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
This means
‘a’ > ‘A’
Daniel Deutsch
13
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
This means
‘a’ > ‘A’
Daniel Deutsch
14
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
Since the type of c1 is a char, the actual
character will be displayed.
This means
‘a’ > ‘A’
Daniel Deutsch
15
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
a
Since the type of c1 is a char, the actual
character will be displayed.
This means
‘a’ > ‘A’
Daniel Deutsch
16
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
a
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
This means
‘a’ > ‘A’
Daniel Deutsch
17
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
a
You have to be careful to care here. c1 is a char and 2 is an
int. You are allowed to add them. Because an int is the more
“general” type, the result of this addition will be an integer, it
cannot be a character.
This means
‘a’ > ‘A’
Daniel Deutsch
18
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
a
You have to be careful to care here. c1 is a char and 2 is an
int. You are allowed to add them. Because an int is the more
“general” type, the result of this addition will be an integer, it
cannot be a character.
The maximum value of a character is 255. So c1 + 5000 must be
This means
an integer. The result isn’t guaranteed to be able to fit in a character.
‘a’ > ‘A’
The same thing is true for this example
Daniel Deutsch
19
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
a
int
You have to be careful to care here. c1 is a char and 2 is an
int. You are allowed to add them. Because an int is the more
“general” type, the result of this addition will be an integer, it
cannot be a character.
The maximum value of a character is 255. So c1 + 5000 must be
This means
an integer. The result isn’t guaranteed to be able to fit in a character.
‘a’ > ‘A’
The same thing is true for this example
Daniel Deutsch
20
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
a
99
You have to be careful to care here. c1 is a char and 2 is an
int. You are allowed to add them. Because an int is the more
“general” type, the result of this addition will be an integer, it
cannot be a character.
The maximum value of a character is 255. So c1 + 5000 must be
This means
an integer. The result isn’t guaranteed to be able to fit in a character.
‘a’ > ‘A’
The same thing is true for this example
Daniel Deutsch
21
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
a
99
int c2 = c1 + 2;
System.out.println(c2);
This means
‘a’ > ‘A’
Daniel Deutsch
22
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
a
99
int c2 = c1 + 2;
System.out.println(c2);
This means
‘a’ > ‘A’
Daniel Deutsch
23
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
a
99
int c2 = c1 + 2;
System.out.println(c2);
This means
‘a’ > ‘A’
Daniel Deutsch
24
Characters
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
a
99
int c2 = c1 + 2;
System.out.println(c2);
c2 is an int, so the int will be displayed, not the char.
This means
‘a’ > ‘A’
Daniel Deutsch
25
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
99
int c2 = c1 + 2;
System.out.println(c2);
99
a
c2 is an int, so the int will be displayed, not the char.
This means
‘a’ > ‘A’
Daniel Deutsch
26
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
99
int c2 = c1 + 2;
System.out.println(c2);
99
a
char c3 = (char) (c1 + 2);
System.out.println(c3);
This means
‘a’ > ‘A’
Daniel Deutsch
27
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
99
int c2 = c1 + 2;
System.out.println(c2);
99
a
char c3 = (char) (c1 + 2);
System.out.println(c3);
This means
‘a’ > ‘A’
Daniel Deutsch
28
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
99
int c2 = c1 + 2;
System.out.println(c2);
99
a
char c3 = (char) (c1 + 2); The resulting type of
this is an integer.
System.out.println(c3);
This means
‘a’ > ‘A’
Daniel Deutsch
29
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
99
int c2 = c1 + 2;
System.out.println(c2);
99
a
char c3 = (char) (c1 + 2); But we convert it
back into a char by
System.out.println(c3);
the cast. Now c3 has
the value ‘c’
This means
‘a’ > ‘A’
Daniel Deutsch
30
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
99
int c2 = c1 + 2;
System.out.println(c2);
99
a
char c3 = (char) (c1 + 2);
System.out.println(c3);
This means
‘a’ > ‘A’
Daniel Deutsch
31
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
99
int c2 = c1 + 2;
System.out.println(c2);
99
a
char c3 = (char) (c1 + 2);
System.out.println(c3);
c3 has type char.
This means
‘a’ > ‘A’
Daniel Deutsch
32
Characters
• Stored in memory as 8 bits
• Indicated by single quotes
• Each character maps to a
number between 0 and 255
‘a’
‘b’
‘c’
97
98
99
‘A’
‘B’
‘C’
65
66
67
‘0’
‘1’
48
49
char c1 = ‘a’;
System.out.println(c1);
System.out.println(c1 + 2);
99
int c2 = c1 + 2;
System.out.println(c2);
99
char c3 = (char) (c1 + 2);
System.out.println(c3);
c
a
c3 has type char.
This means
‘a’ > ‘A’
Daniel Deutsch
33
Arithmetic
• Operators have an order of
operations, just like math
Daniel Deutsch
34
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
Daniel Deutsch
35
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
36
Arithmetic
int a = 5, b = 2;
System.out.println(a / b);
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
37
Arithmetic
int a = 5, b = 2;
System.out.println(a / b);
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
38
Arithmetic
int a = 5, b = 2;
System.out.println(a / b);
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
39
Arithmetic
int a = 5, b = 2;
System.out.println(a / b);
a is an int, b is an int. This will be
integer division.
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
40
Arithmetic
int a = 5, b = 2;
System.out.println(a / b);
a is an int, b is an int. This will be
integer division.
• Operators have an order of
operations, just like math
•
•
•
•
•
5 / 2 = 2.5 => 2 because we truncate, not round
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
41
Arithmetic
int a = 5, b = 2;
System.out.println(a / b);
a is an int, b is an int. This will be
integer division.
• Operators have an order of
operations, just like math
•
•
•
•
•
2
5 / 2 = 2.5 => 2 because we truncate, not round
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
42
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
43
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
44
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
The casting is done first because of order of operations
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
45
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
The casting is done first because of order of operations
So for this operation, we will pretend like a is a double.
That means we are dividing a double by an integer,
which is normal division.
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
46
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
The casting is done first because of order of operations
So for this operation, we will pretend like a is a double.
That means we are dividing a double by an integer,
which is normal division.
Since a double is the more “general” type, the result
will be a double.
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
47
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
The casting is done first because of order of operations
So for this operation, we will pretend like a is a double.
That means we are dividing a double by an integer,
which is normal division.
Since a double is the more “general” type, the result
will be a double.
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
48
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
49
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
50
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
51
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
A double divided by a int => double
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
52
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
2.5
A double divided by a int => double
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
53
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
System.out.println(5.0 / b);
System.out.println(5 / b);
2.5
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
54
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
System.out.println(5.0 / b);
System.out.println(5 / b);
2.5
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
55
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
System.out.println(5.0 / b);
System.out.println(5 / b);
2.5
5.0 is considered a double because of the “.0”
part. It does have a decimal, it just so happens
that the decimal is 0.
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
56
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
System.out.println(5.0 / b);
System.out.println(5 / b);
2.5
2.5
5.0 is considered a double because of the “.0”
part. It does have a decimal, it just so happens
that the decimal is 0.
• Be careful when dividing
integers! Casting helps you get
around this
Therefore, we are dividing a double by an int => double
Daniel Deutsch
57
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
System.out.println(5.0 / b);
System.out.println(5 / b);
2.5
2.5
2.5
5.0 is considered a double because of the “.0”
part. It does have a decimal, it just so happens
that the decimal is 0.
• Be careful when dividing
integers! Casting helps you get
around this
Therefore, we are dividing a double by an int => double
Daniel Deutsch
58
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
System.out.println(5.0 / b);
System.out.println(5 / b);
2.5
2.5
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
59
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
System.out.println(5.0 / b);
System.out.println(5 / b);
2.5
2.5
5 with no decimal is treated like an integer. So we
are doing integer division.
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
60
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
System.out.println(5.0 / b);
System.out.println(5 / b);
2.5
2.5
2
5 with no decimal is treated like an integer. So we
are doing integer division.
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
61
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
62
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
• Be careful when dividing
integers! Casting helps you get
around this
Daniel Deutsch
63
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
This works exactly like the previous cast. 5 gets turned
into 5.0 and acts like a double
Daniel Deutsch
64
2.5
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
This works exactly like the previous cast. 5 gets turned
into 5.0 and acts like a double
Daniel Deutsch
65
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
Daniel Deutsch
66
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
Daniel Deutsch
67
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
Daniel Deutsch
68
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
Here, the decimal is truncated. We do not round. e = 8
Daniel Deutsch
69
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
Here, the decimal is truncated. We do not round. e = 8
Daniel Deutsch
70
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
8
Here, the decimal is truncated. We do not round. e = 8
Daniel Deutsch
71
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
8
double d2 = 5 / 2;
System.out.println(d2);
Daniel Deutsch
72
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
8
double d2 = 5 / 2;
System.out.println(d2);
Daniel Deutsch
73
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
8
The result of this is an int, the value 2
double d2 = 5 / 2;
System.out.println(d2);
Daniel Deutsch
74
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
8
Then the value 2 is set
to a double, which
means d2 = 2.0, not 2.5.
double d2 = 5 / 2;
System.out.println(d2); The assignment
operation happens last
Daniel Deutsch
75
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
8
Then the value 2 is set
to a double, which
means d2 = 2.0, not 2.5.
double d2 = 5 / 2;
System.out.println(d2); The assignment
operation happens last
Daniel Deutsch
76
Arithmetic
• Operators have an order of
operations, just like math
•
•
•
•
•
()
- (negation)
*, /, %
+, = (assignment)
• Be careful when dividing
integers! Casting helps you get
around this
int a = 5, b = 2;
System.out.println(a / b);
2
System.out.println((double) a / b);
2.5
double c = 5.0;
System.out.println(c / b);
2.5
System.out.println(5.0 / b);
2.5
System.out.println(5 / b);
2
System.out.println((double) 5 / b);
2.5
double d = 8.825;
int e = (int) d;
System.out.println(e);
8
Then the value 2 is set
to a double, which
means d2 = 2.0, not 2.5.
double d2 = 5 / 2;
System.out.println(d2); The assignment
operation happens last
2.0
Daniel Deutsch
77
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
Daniel Deutsch
78
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Daniel Deutsch
79
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
33 / 4 = 8 + remainder of 1.
mod = 1
Daniel Deutsch
80
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
Daniel Deutsch
81
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
Daniel Deutsch
82
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
Daniel Deutsch
83
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
Daniel Deutsch
84
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
Daniel Deutsch
85
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
This gets rid of the ones digit.
Daniel Deutsch
86
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
235
Daniel Deutsch
87
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
235
int temp = 2358 / 10;
int tens = temp % 10; Now we just find the ones digit of 235
Daniel Deutsch
88
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
235
5
Daniel Deutsch
89
Arithmetic
• The mod operator
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
235
5
Daniel Deutsch
90
Arithmetic
• The mod operator
• Misc. operators
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• num++;
num += 1;
num = num + 1;
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
235
5
Daniel Deutsch
91
Arithmetic
• The mod operator
• Misc. operators
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• num++;
num += 1;
num = num + 1;
These are all
equivalent!
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
235
5
Daniel Deutsch
92
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• num++;
num += 1;
num = num + 1;
These are all
equivalent!
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
235
5
Daniel Deutsch
93
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
These are all
equivalent!
• What’s the difference between
num++ and ++num?
What’s the ones digit of 2358?
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
235
5
Daniel Deutsch
94
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
• What’s the difference between
num++ and ++num?
What’s the ones digit of 2358?
num++ is run after the line executes, ++num before.
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
These are all
equivalent!
235
5
Daniel Deutsch
95
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
• What’s the difference between
num++ and ++num?
What’s the ones digit of 2358?
num++ is run after the line executes, ++num before.
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
These are all
equivalent!
int num = 0;
System.out.println(num++);
System.out.println(++num);
235
5
Daniel Deutsch
96
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
Mods are useful for finding digits of a number
• What’s the difference between
num++ and ++num?
What’s the ones digit of 2358?
num++ is run after the line executes, ++num before.
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
These are all
equivalent!
int num = 0;
System.out.println(num++);
System.out.println(++num);
235
5
Daniel Deutsch
97
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• What’s the difference between
num++ and ++num?
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
num
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
These are all
equivalent!
0
num++ is run after the line executes, ++num before.
int num = 0;
System.out.println(num++);
System.out.println(++num);
235
5
Daniel Deutsch
98
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• What’s the difference between
num++ and ++num?
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
num
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
These are all
equivalent!
0
num++ is run after the line executes, ++num before.
int num = 0;
System.out.println(num++);
System.out.println(++num);
235
5
Daniel Deutsch
99
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• What’s the difference between
num++ and ++num?
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
num
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
These are all
equivalent!
0
235
5
Daniel Deutsch
num++ is run after the line executes, ++num before.
int num = 0;
This line
System.out.println(num++); executes with
the current
System.out.println(++num); value of num,
then adds 1 to
it after.
100
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• What’s the difference between
num++ and ++num?
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
num
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
These are all
equivalent!
0
num++ is run after the line executes, ++num before.
int num = 0;
System.out.println(num++);
0
System.out.println(++num);
235
5
Daniel Deutsch
101
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• What’s the difference between
num++ and ++num?
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
num
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
These are all
equivalent!
1
num++ is run after the line executes, ++num before.
int num = 0;
System.out.println(num++);
0
System.out.println(++num);
235
5
Daniel Deutsch
102
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• What’s the difference between
num++ and ++num?
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
num
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
These are all
equivalent!
1
num++ is run after the line executes, ++num before.
int num = 0;
System.out.println(num++);
0
System.out.println(++num);
235
5
Daniel Deutsch
103
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• What’s the difference between
num++ and ++num?
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
num
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
num++ is run after the line executes, ++num before.
int num = 0;
System.out.println(num++);
1
0
System.out.println(++num);
235
5
These are all
equivalent!
This line increments num, then executes the line.
Daniel Deutsch
104
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• What’s the difference between
num++ and ++num?
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
num
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
num++ is run after the line executes, ++num before.
int num = 0;
System.out.println(num++);
2
0
System.out.println(++num);
235
5
These are all
equivalent!
This line increments num, then executes the line.
Daniel Deutsch
105
--, *=, /=, -=, %=
Arithmetic
All of these exist, too!
• The mod operator
• Misc. operators
• num++;
num += 1;
num = num + 1;
• % gives you the reminder when
dividing two numbers (normally)
int mod = 33 % 4;
• What’s the difference between
num++ and ++num?
Mods are useful for finding digits of a number
What’s the ones digit of 2358?
num
int a = 2358;
int ones = a % 10;
What’s the tens digit of 2358?
int temp = 2358 / 10;
int tens = temp % 10;
num++ is run after the line executes, ++num before.
2
235
5
These are all
equivalent!
int num = 0;
System.out.println(num++);
0
System.out.println(++num);
2
This line increments num, then executes the line.
Daniel Deutsch
106
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Daniel Deutsch
107
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
Daniel Deutsch
108
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
input is an instantiation of the class Scanner
Daniel Deutsch
109
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
We create a new Scanner using the “new” syntax
Daniel Deutsch
110
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
Here we are calling the .next() method on the input object.
Daniel Deutsch
111
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
Notice that we can’t do these things with primitives like ints.
Daniel Deutsch
112
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
Notice that we can’t do these things with primitives like ints.
int a = new int(12);
int b = a.plus(7);
Daniel Deutsch
113
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
Notice that we can’t do these things with primitives like ints.
int a = new int(12);
int b = a.plus(7);
You can’t do anything like this!!
Daniel Deutsch
114
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
Notice that we can’t do these things with primitives like ints.
int a = new int(12);
int b = a.plus(7);
You can’t do anything like this!!
Daniel Deutsch
115
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
Notice that we can’t do these things with primitives like ints.
int a = new int(12);
int b = a.plus(7);
You can’t do anything like this!!
Strings are objects too since you can say
String s = “hello”;
String s1 = s.substring(8);
Daniel Deutsch
116
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
Notice that we can’t do these things with primitives like ints.
int a = new int(12);
int b = a.plus(7);
You can’t do anything like this!!
Strings are objects too since you can say
We can call methods on
String s = “hello”;
String s1 = s.substring(8); Strings => they are objects.
Daniel Deutsch
117
• The Math and Character classes
provide static methods that you
can call with the class name
because they are static.
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
Scanner input = new Scanner(System.in);
String d = input.next();
Notice that we can’t do these things with primitives like ints.
int a = new int(12);
int b = a.plus(7);
You can’t do anything like this!!
Strings are objects too since you can say
We can call methods on
String s = “hello”;
String s1 = s.substring(8); Strings => they are objects.
Daniel Deutsch
118
• The Math and Character classes
provide static methods that you
can call with the class name
because they are static.
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
double d = Math.floor(3.75);
Scanner input = new Scanner(System.in);
String d = input.next();
Notice that we can’t do these things with primitives like ints.
int a = new int(12);
int b = a.plus(7);
You can’t do anything like this!!
Strings are objects too since you can say
We can call methods on
String s = “hello”;
String s1 = s.substring(8); Strings => they are objects.
Daniel Deutsch
119
• The Math and Character classes
provide static methods that you
can call with the class name
because they are static.
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
double d = Math.floor(3.75);
Notice that we did not create a Math object
(because you can’t!)
Scanner input = new Scanner(System.in);
String d = input.next();
Notice that we can’t do these things with primitives like ints.
int a = new int(12);
int b = a.plus(7);
You can’t do anything like this!!
Strings are objects too since you can say
We can call methods on
String s = “hello”;
String s1 = s.substring(8); Strings => they are objects.
Daniel Deutsch
120
• The Math and Character classes
provide static methods that you
can call with the class name
because they are static.
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
double d = Math.floor(3.75);
Notice that we did not create a Math object
(because you can’t!)
Scanner input = new Scanner(System.in);
String d = input.next();
Math m = new Math();
Notice that we can’t do these things with primitives like ints.
int a = new int(12);
int b = a.plus(7);
You can’t do anything like this!!
Strings are objects too since you can say
We can call methods on
String s = “hello”;
String s1 = s.substring(8); Strings => they are objects.
Daniel Deutsch
121
• The Math and Character classes
provide static methods that you
can call with the class name
because they are static.
Objects
• Objects are instantiations of
classes. They have data
associated with them and you
can call methods on them
double d = Math.floor(3.75);
Notice that we did not create a Math object
(because you can’t!)
Scanner input = new Scanner(System.in);
String d = input.next();
Notice that we can’t do these things with primitives like ints.
int a = new int(12);
int b = a.plus(7);
Math m = new Math();
You can’t do this!
You can’t do anything like this!!
Strings are objects too since you can say
We can call methods on
String s = “hello”;
String s1 = s.substring(8); Strings => they are objects.
Daniel Deutsch
122
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Daniel Deutsch
123
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
Daniel Deutsch
124
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
Daniel Deutsch
125
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
Daniel Deutsch
126
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
Daniel Deutsch
127
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Daniel Deutsch
128
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Daniel Deutsch
129
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Daniel Deutsch
130
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Daniel Deutsch
131
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
132
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
133
indexOf
int index = s.indexOf(‘c’);
int nope = s.indexOf(‘z’);
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
134
indexOf
int index = s.indexOf(‘c’);
int nope = s.indexOf(‘z’);
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
135
indexOf
int index = s.indexOf(‘c’);
int nope = s.indexOf(‘z’);
Strings
index = 2
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
136
indexOf
int index = s.indexOf(‘c’);
int nope = s.indexOf(‘z’);
Strings
index = 2
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
137
indexOf
int index = s.indexOf(‘c’);
int nope = s.indexOf(‘z’);
Strings
Since ‘z’ is not in s, indexOf will return -1
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
index = 2
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
138
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
Strings
Since ‘z’ is not in s, indexOf will return -1
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
139
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
Strings
Since ‘z’ is not in s, indexOf will return -1
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
This is really helpful for testing if a String contains a
specific character.
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
140
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
substring
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
141
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
substring
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
142
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
substring
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
Substring with one argument takes
the characters from that index until
the end.
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
143
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
substring
s1 = “defghijk”
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
Substring with one argument takes
the characters from that index until
the end.
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
144
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
Strings
• Strings are Objects, not
primitives, so you can call
methods on them
Length
String s = “abcdefghijk”;
int length = s.length();
substring
s1 = “defghijk”
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
length = 11
Substring with two arguments
takes starts at the first argument
and stops and does not include the
second argument.
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
145
Strings
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
• Strings are Objects, not
primitives, so you can call
methods on them
substring
s1 = “defghijk”
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
s2 = “cdef”
Length
String s = “abcdefghijk”;
int length = s.length();
toLowerCase
String lower = “UPPER”.toLowerCase();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
146
Strings
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
• Strings are Objects, not
primitives, so you can call
methods on them
substring
s1 = “defghijk”
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
s2 = “cdef”
Length
String s = “abcdefghijk”;
int length = s.length();
toLowerCase
String lower = “UPPER”.toLowerCase();
length = 11
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
147
Strings
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
• Strings are Objects, not
primitives, so you can call
methods on them
substring
s1 = “defghijk”
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
s2 = “cdef”
Length
String s = “abcdefghijk”;
int length = s.length();
toLowerCase
String lower = “UPPER”.toLowerCase();
length = 11
lower = “upper”
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
148
Strings
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
• Strings are Objects, not
primitives, so you can call
methods on them
substring
s1 = “defghijk”
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
s2 = “cdef”
Length
String s = “abcdefghijk”;
int length = s.length();
toLowerCase
String lower = “UPPER”.toLowerCase();
length = 11
lower = “upper”
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
String a = “AAA”;
String lower = a.toLowerCase();
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
149
Strings
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
• Strings are Objects, not
primitives, so you can call
methods on them
substring
s1 = “defghijk”
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
s2 = “cdef”
Length
String s = “abcdefghijk”;
int length = s.length();
toLowerCase
String lower = “UPPER”.toLowerCase();
length = 11
lower = “upper”
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
String a = “AAA”;
String lower = a.toLowerCase();
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
150
Strings
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
• Strings are Objects, not
primitives, so you can call
methods on them
substring
s1 = “defghijk”
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
s2 = “cdef”
Length
String s = “abcdefghijk”;
int length = s.length();
toLowerCase
String lower = “UPPER”.toLowerCase();
length = 11
lower = “upper”
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
String a = “AAA”;
String lower = a.toLowerCase();
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
151
Strings
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
• Strings are Objects, not
primitives, so you can call
methods on them
substring
s1 = “defghijk”
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
s2 = “cdef”
Length
String s = “abcdefghijk”;
int length = s.length();
toLowerCase
String lower = “UPPER”.toLowerCase();
length = 11
lower = “upper”
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
String a = “AAA”;
String lower = a.toLowerCase();
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
a = “AAA”
lower = “aaa”
152
Strings
indexOf
int index = s.indexOf(‘c’); index = 2
int nope = s.indexOf(‘z’);
nope = -1
• Strings are Objects, not
primitives, so you can call
methods on them
substring
s1 = “defghijk”
String s1 = s.substring(3);
String s2 = s.substring(index, 6);
s2 = “cdef”
Length
String s = “abcdefghijk”;
int length = s.length();
toLowerCase
String lower = “UPPER”.toLowerCase();
length = 11
lower = “upper”
charAt
c = ‘a’
char c = s.charAt(0);
char c2 = s.charAt(s.length());
String a = “AAA”;
String lower = a.toLowerCase();
Since we start counting at 0, the character at s.length() does not exist!
The program will crash
Daniel Deutsch
a = “AAA”
lower = “aaa”
The toLowerCase
method does NOT
change the value of “a”
153
Strings
• Escape characters
• There are special characters which use two symbols to represent them
• \t is a tab
• \n is a newline character
• \\ is the \ character
Daniel Deutsch
154
Strings
• Escape characters
• There are special characters which use two symbols to represent them
• \t is a tab
• \n is a newline character
• \\ is the \ character
String s = “1\t2\n\t3\t\\”;
System.out.println(s);
Daniel Deutsch
155
Strings
• Escape characters
• There are special characters which use two symbols to represent them
• \t is a tab
• \n is a newline character
• \\ is the \ character
String s = “1\t2\n\t3\t\\”;
System.out.println(s);
1
2
3
\
Daniel Deutsch
156
Strings
• Escape characters
• There are special characters which use two symbols to represent them
• \t is a tab
• \n is a newline character
• \\ is the \ character
String s = “1\t2\n\t3\t\\”;
System.out.println(s);
1
2
3
\
Daniel Deutsch
157
Strings
• Escape characters
• There are special characters which use two symbols to represent them
• \t is a tab
• \n is a newline character
• \\ is the \ character
String s = “1\t2\n\t3\t\\”;
System.out.println(s);
1
2
3
\
Daniel Deutsch
158
Strings
• Escape characters
• There are special characters which use two symbols to represent them
• \t is a tab
• \n is a newline character
• \\ is the \ character
String s = “1\t2\n\t3\t\\”;
System.out.println(s);
1
2
3
\
Daniel Deutsch
159
Strings
• Escape characters
• There are special characters which use two symbols to represent them
• \t is a tab
• \n is a newline character
• \\ is the \ character
String s = “1\t2\n\t3\t\\”;
System.out.println(s);
1
2
3
\
Daniel Deutsch
160
Strings
• Convert a String to an int or a
double
Daniel Deutsch
161
Strings
• Convert a String to an int or a
double
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
Daniel Deutsch
162
Strings
• Convert a String to an int or a
double
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
Daniel Deutsch
163
Strings
• Convert a String to an int or a
double
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
Daniel Deutsch
164
Strings
• Convert a String to an int or a
double
a = 72
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
Daniel Deutsch
165
Strings
• Convert a String to an int or a
double
a = 72
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
Daniel Deutsch
166
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
Daniel Deutsch
167
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
int b = Integer.parseInt(“seven”);
Daniel Deutsch
168
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
int b = Integer.parseInt(“seven”);
Daniel Deutsch
169
Strings
• Convert a String to an int or a
double
d = 38.08
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
Crashes!
int b = Integer.parseInt(“seven”);
a = 72
Daniel Deutsch
170
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
Daniel Deutsch
171
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
Daniel Deutsch
172
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
Crashes! 8.50 is not an int!
Daniel Deutsch
173
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
Crashes! 8.50 is not an int!
• Convert an int to a String
Daniel Deutsch
174
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
Crashes! 8.50 is not an int!
• Convert an int to a String
String s1 = “” + 7;
Daniel Deutsch
175
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
Crashes! 8.50 is not an int!
• Convert an int to a String
String s1 = “” + 7;
Daniel Deutsch
176
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
Crashes! 8.50 is not an int!
• Convert an int to a String
String s1 = “” + 7;
s1 = “7”, not 7
Daniel Deutsch
177
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
• If you need to see if two Strings
are the same, never use ==.
Always use .equals or
.equalsIgnoreCase
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
String s1 = “Java”, s2 = “Java”;
if (s1.equals(s2))
System.out.println(“same!”);
Crashes! 8.50 is not an int!
• Convert an int to a String
String s1 = “” + 7;
s1 = “7”, not 7
Daniel Deutsch
178
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
• If you need to see if two Strings
are the same, never use ==.
Always use .equals or
.equalsIgnoreCase
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
String s1 = “Java”, s2 = “Java”;
if (s1.equals(s2))
System.out.println(“same!”);
Crashes! 8.50 is not an int!
• Convert an int to a String
String s1 = “” + 7;
s1 = “7”, not 7
Daniel Deutsch
179
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
• If you need to see if two Strings
are the same, never use ==.
Always use .equals or
.equalsIgnoreCase
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
String s1 = “Java”, s2 = “Java”;
if (s1.equals(s2))
System.out.println(“same!”);
Crashes! 8.50 is not an int!
• Convert an int to a String
String s1 = “” + 7;
s1 = “7”, not 7
Daniel Deutsch
180
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
• If you need to see if two Strings
are the same, never use ==.
Always use .equals or
.equalsIgnoreCase
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
Crashes! 8.50 is not an int!
String s1 = “Java”, s2 = “Java”;
if (s1.equals(s2))
System.out.println(“same!”);
This will evaluate to true
• Convert an int to a String
String s1 = “” + 7;
s1 = “7”, not 7
Daniel Deutsch
181
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
• If you need to see if two Strings
are the same, never use ==.
Always use .equals or
.equalsIgnoreCase
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
Crashes! 8.50 is not an int!
String s1 = “Java”, s2 = “Java”;
if (s1.equals(s2))
System.out.println(“same!”);
This will evaluate to true
• Convert an int to a String
String s1 = “” + 7;
s1 = “7”, not 7
Daniel Deutsch
182
Strings
• Convert a String to an int or a
double
a = 72
d = 38.08
Crashes!
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
• If you need to see if two Strings
are the same, never use ==.
Always use .equals or
.equalsIgnoreCase
int b = Integer.parseInt(“seven”);
int c = Integer.parseInt(“8.50”);
Crashes! 8.50 is not an int!
String s1 = “Java”, s2 = “Java”;
if (s1.equals(s2))
System.out.println(“same!”);
same
This will evaluate to true
• Convert an int to a String
String s1 = “” + 7;
s1 = “7”, not 7
Daniel Deutsch
183
Scanner
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Daniel Deutsch
184
Scanner
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
185
Scanner
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
This Scanner can read input that the user
types into the program. It comes from the
System.in steam.
186
Scanner
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
This Scanner is created with a String. It acts
like a Scanner over System.in with the user
entering “Dan 38 12.7”
187
System.out.print(“Enter input: “);
Scanner
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
188
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
189
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
next
String s1 = userInput.next();
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
190
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
next
String s1 = userInput.next();
This will return the first token divided up by whitespace, “java”
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
191
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
next
String s1 = userInput.next();
s1 = “java”
This will return the first token divided up by whitespace, “java”
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
192
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
next
String s1 = userInput.next();
s1 = “java”
This will return the first token divided up by whitespace, “java”
After this line execute, the scanner has passed
“java” and is waiting to read the next token.
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
193
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
next
String s1 = userInput.next();
s1 = “java”
nextInt
int a = userInput.nextInt();
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
194
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
next
String s1 = userInput.next();
s1 = “java”
nextInt
int a = userInput.nextInt();
nextInt will return the next token as an integer.
If it isn’t possible to turn it into an int, like
“hello,” your program will crash!
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
195
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
next
String s1 = userInput.next();
s1 = “java”
nextInt
int a = userInput.nextInt();
a = 29
nextInt will return the next token as an integer.
If it isn’t possible to turn it into an int, like
“hello,” your program will crash!
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
196
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
next
String s1 = userInput.next();
s1 = “java”
nextInt
int a = userInput.nextInt();
a = 29
nextDouble
double d = userInput.nextDouble();
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
197
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
next
String s1 = userInput.next();
s1 = “java”
nextInt
int a = userInput.nextInt();
a = 29
nextDouble
double d = userInput.nextDouble();
nextDouble works much the same way as nextInt
198
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
next
String s1 = userInput.next();
s1 = “java”
nextInt
int a = userInput.nextInt();
a = 29
nextDouble
d = 3.14
double d = userInput.nextDouble();
nextDouble works much the same way as nextInt
199
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
Daniel Deutsch
next
String s1 = userInput.next();
s1 = “java”
nextInt
int a = userInput.nextInt();
a = 29
nextDouble
d = 3.14
double d = userInput.nextDouble();
Unfortunately nextChar does not exist. If
you want to do that, the best solution is to
take the next token with .next(), then take
the character at index 0.
200
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
next
String s1 = userInput.next();
s1 = “java”
nextInt
int a = userInput.nextInt();
a = 29
nextDouble
d = 3.14
double d = userInput.nextDouble();
Unfortunately nextChar does not exist. If
you want to do that, the best solution is to
take the next token with .next(), then take
the character at index 0.
char c = userInput.next().charAt(0);
Daniel Deutsch
201
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
next
String s1 = userInput.next();
s1 = “java”
nextInt
int a = userInput.nextInt();
a = 29
nextDouble
d = 3.14
double d = userInput.nextDouble();
Unfortunately nextChar does not exist. If
you want to do that, the best solution is to
take the next token with .next(), then take
the character at index 0.
char c = userInput.next().charAt(0);
Has the result “A”
Daniel Deutsch
202
System.out.print(“Enter input: “);
Scanner
For this example, pretend the user typed java 29 3.14 A
• The Scanner class allows you to
iterate over a stream of text like
System.in or a String
• It’s located in java.util.Scanner,
so you must import that first
Initialization
Scanner userInput = new Scanner(System.in);
Scanner scanner = new Scanner(“Dan 38 12.7”);
next
String s1 = userInput.next();
s1 = “java”
nextInt
int a = userInput.nextInt();
a = 29
nextDouble
d = 3.14
double d = userInput.nextDouble();
Unfortunately nextChar does not exist. If
you want to do that, the best solution is to
take the next token with .next(), then take
the character at index 0.
char c = userInput.next().charAt(0);
Has the result “A”
Daniel Deutsch
So this is like “A”.charAt(0)
203
Scanner
There is also a function to tell you if there is more input to be read.
Daniel Deutsch
204
Scanner
There is also a function to tell you if there is more input to be read.
scanner
first second third
Daniel Deutsch
205
Scanner
There is also a function to tell you if there is more input to be read.
scanner
first second third
The red String is what the user typed in and the arrow is
where our Scanner currently is
Daniel Deutsch
206
Scanner
There is also a function to tell you if there is more input to be read.
scanner
first second third
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
Daniel Deutsch
207
Scanner
There is also a function to tell you if there is more input to be read.
scanner
first second third
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
Daniel Deutsch
208
Scanner
There is also a function to tell you if there is more input to be read.
scanner
first second third
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
Daniel Deutsch
There is still more left to read!
209
Scanner
There is also a function to tell you if there is more input to be read.
scanner
first second third
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
Daniel Deutsch
true
There is still more left to read!
210
Scanner
There is also a function to tell you if there is more input to be read.
scanner
first second third
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
Daniel Deutsch
true
211
Scanner
There is also a function to tell you if there is more input to be read.
scanner
first second third
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
Daniel Deutsch
true
These lines advance the Scanner past the
next two tokens. There is no way to
recover them.
212
Scanner
There is also a function to tell you if there is more input to be read.
scanner
first second third
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
Daniel Deutsch
true
These lines advance the Scanner past the
next two tokens. There is no way to
recover them.
213
Scanner
There is also a function to tell you if there is more input to be read.
scanner
first second third
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
Daniel Deutsch
true
These lines advance the Scanner past the
next two tokens. There is no way to
recover them.
214
Scanner
There is also a function to tell you if there is more input to be read.
first second third
scanner
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
Daniel Deutsch
true
These lines advance the Scanner past the
next two tokens. There is no way to
recover them.
215
Scanner
There is also a function to tell you if there is more input to be read.
first second third
scanner
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
Daniel Deutsch
true
216
Scanner
There is also a function to tell you if there is more input to be read.
first second third
scanner
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
true
Since there’s nothing left in the input stream, it outputs false
Daniel Deutsch
217
Scanner
There is also a function to tell you if there is more input to be read.
first second third
scanner
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
true
false
Since there’s nothing left in the input stream, it outputs false
Daniel Deutsch
218
Scanner
There is also a function to tell you if there is more input to be read.
first second third
scanner
The red String is what the user typed in and the arrow is
where our Scanner currently is
System.out.println(userInput.hasNext());
userInput.next();
userInput.next();
System.out.println(userInput.hasNext());
true
false
There is also a hasNextLine equivalent which will tell you if there is more text before a newline character, \n
Daniel Deutsch
219
Scanner
First 3 24.8 12 Last
Daniel Deutsch
220
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
Daniel Deutsch
221
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
Daniel Deutsch
222
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
Daniel Deutsch
223
Scanner
First 3 24.8 12 Last
The location of the pointer starts at the beginning of the stream.
Scanner keyboard = new Scanner(System.in);
Daniel Deutsch
224
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
keyboard.nextDouble();
System.out.println(keyboard.hasNext());
keyboard.next();
int a = keyboard.nextInt();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
Daniel Deutsch
225
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
keyboard.nextDouble();
System.out.println(keyboard.hasNext());
keyboard.next();
int a = keyboard.nextInt();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
Daniel Deutsch
226
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
keyboard.nextDouble();
System.out.println(keyboard.hasNext());
keyboard.next();
int a = keyboard.nextInt();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
Daniel Deutsch
227
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
keyboard.nextDouble();
System.out.println(keyboard.hasNext());
keyboard.next();
int a = keyboard.nextInt();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
Daniel Deutsch
228
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
keyboard.nextDouble();
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
Daniel Deutsch
229
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
keyboard.nextDouble();
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
Daniel Deutsch
230
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
Daniel Deutsch
231
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
Daniel Deutsch
232
Java will attempt to turn 24.8 into an int. Since it cannot, this line
would cause the program to crash! Let’s pretend we didn’t do it.
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
Daniel Deutsch
233
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
Daniel Deutsch
234
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
24.8
Daniel Deutsch
235
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
24.8
Daniel Deutsch
236
Scanner
First 3 24.8 12 Last
This method call is okay because we can turn 12 into a double: 12.0
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
24.8
Daniel Deutsch
237
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
12.0
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
24.8
Daniel Deutsch
238
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
12.0
keyboard.next();
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
24.8
Daniel Deutsch
239
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
keyboard.next();
12.0
“Last”
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
24.8
Daniel Deutsch
240
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextDouble();
keyboard.next();
12.0
“Last”
System.out.println(keyboard.hasNext());
keyboard.nextInt();
keyboard.nextDouble();
24.8
Daniel Deutsch
241
Scanner
First 3 24.8 12 Last
Scanner keyboard = new Scanner(System.in);
String s = keyboard.next();
s = “First”
System.out.println(keyboard.hasNext());
true
int a = keyboard.nextInt();
a=3
keyboard.nextInt();
keyboard.nextDouble();
keyboard.nextDouble();
keyboard.next();
12.0
“Last”
System.out.println(keyboard.hasNext());
false
24.8
Daniel Deutsch
242
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
Daniel Deutsch
243
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
pow
double d1 = Math.pow(2, 3);
sqrt
double d6 = Math.sqrt(34);
round
double d2 = Math.round(7.8);
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
Daniel Deutsch
244
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
pow
double d1 = Math.pow(2, 3);
sqrt
double d6 = Math.sqrt(34);
round
double d2 = Math.round(7.8);
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
Daniel Deutsch
245
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
sqrt
double d6 = Math.sqrt(34);
round
double d2 = Math.round(7.8);
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
Daniel Deutsch
246
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
sqrt
double d6 = Math.sqrt(34);
round
double d2 = Math.round(7.8);
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
Daniel Deutsch
247
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
sqrt
double d6 = Math.sqrt(34);
8.0
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
Daniel Deutsch
248
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
sqrt
double d6 = Math.sqrt(34);
8.0
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
Daniel Deutsch
249
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
sqrt
double d6 = Math.sqrt(34);
8.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
250
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
sqrt
double d6 = Math.sqrt(34);
8.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
251
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
sqrt
double d6 = Math.sqrt(34);
8.0
3.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
252
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
sqrt
double d6 = Math.sqrt(34);
8.0
3.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
253
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
5.0
sqrt
double d6 = Math.sqrt(34);
8.0
3.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
254
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
5.0
sqrt
double d6 = Math.sqrt(34);
8.0
3.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
255
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
sqrt
double d6 = Math.sqrt(34);
5.0
5.83…
8.0
3.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
256
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
sqrt
double d6 = Math.sqrt(34);
5.0
5.83…
8.0
3.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
257
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
sqrt
double d6 = Math.sqrt(34);
5.0
5.83…
8.0
3.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
8.0
258
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
sqrt
double d6 = Math.sqrt(34);
5.0
5.83…
8.0
3.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
8.0
259
Math Class
• You can use the Math class to do useful math operations
• You call the static methods with the Math class, you don’t instantiate
it
floor
double d5 = Math.floor(5.6);
3
2 =8
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
sqrt
double d6 = Math.sqrt(34);
5.0
5.83…
8.0
3.0
3.0
Daniel Deutsch
max/min
double d7 = Math.max(5, 8);
double d8 = Math.min(3, 28);
8.0
3.0
260
Printf
• If you want a really, really specific format, you can use the printf
function, which is a bit harder to use than println, but gives you
more control
int a = 7;
System.out.print(“The value of a is %d\n”, a);
Daniel Deutsch
261
Printf
• If you want a really, really specific format, you can use the printf
function, which is a bit harder to use than println, but gives you
more control
int a = 7;
System.out.print(“The value of a is %d\n”, a);
The way that it works is that you type the String that you want to be displayed on the screen. Wherever you
want a variable to be displayed, you use the special % character which indicates you want a variable to be
printed where that is.
Daniel Deutsch
262
Printf
• If you want a really, really specific format, you can use the printf
function, which is a bit harder to use than println, but gives you
more control
int a = 7;
System.out.print(“The value of a is %d\n”, a);
The way that it works is that you type the String that you want to be displayed on the screen. Wherever you
want a variable to be displayed, you use the special % character which indicates you want a variable to be
printed where that is.
What comes after the % indicates how the value of the variable should be formatted. For example,
if you want to print an integer, you have to put %d
Daniel Deutsch
263
Input and Output: Printf
• If you want a really, really specific format, you can use the printf
function, which is a bit harder to use than println, but gives you
more control
This %d code corresponds to the first argument, a
int a = 7;
System.out.print(“The value of a is %d\n”, a);
The way that it works is that you type the String that you want to be displayed on the screen. Wherever you
want a variable to be displayed, you use the special % character which indicates you want a variable to be
printed where that is.
What comes after the % indicates how the value of the variable should be formatted. For example,
if you want to print an integer, you have to put %d
Daniel Deutsch
264
Printf
• If you want a really, really specific format, you can use the printf
function, which is a bit harder to use than println, but gives you
more control
int a = 7;
System.out.print(“The value of a is %d\n”, a);
The way that it works is that you type the String that you want to be displayed on the screen. Wherever you
want a variable to be displayed, you use the special % character which indicates you want a variable to be
printed where that is.
What comes after the % indicates how the value of the variable should be formatted. For example,
if you want to print an integer, you have to put %d
Output:
The value of a is 7
Daniel Deutsch
265
Printf
• The printf codes are:
• %d for integers
• %f for floats and doubles
• %s for Objects (like Strings)
• You can specify how many spaces wide you want the variable to be by
putting a number after %
• %8d prints an integer that is minimally 8 spaces wide, like “
7”
• You can also specify how many decimal points you want to be printed
• %.2f will display 2 decimals
• You can also pad a number with 0s to fit a certain width
• %08d will print an integer that is minimally 8 spaces wide with 0s in front, like
“00000007”
Daniel Deutsch
266
Printf
• Examples:
int a = 7;
System.out.printf(“%3d\n”, a);
double d2 = 1234567.8;
System.out.printf(“%f\n”, d2);
System.out.printf(“%03d\n”, a);
System.out.printf(“%2f\n”, d2);
double d = 3.2;
System.out.printf(“%04d\n”, d);
System.out.printf(“%.3f\n”, d);
System.out.printf(“%06.3f%%\n”, d);
System.out.printf(“My name is %s\n”, “Dan”);
Daniel Deutsch
267
Printf
• Examples:
int a = 7;
System.out.printf(“%3d\n”, a);
“ 7”
double d2 = 1234567.8;
System.out.printf(“%f\n”, d2);
System.out.printf(“%03d\n”, a);
System.out.printf(“%2f\n”, d2);
double d = 3.2;
System.out.printf(“%04d\n”, d);
System.out.printf(“%.3f\n”, d);
System.out.printf(“%06.3f%%\n”, d);
System.out.printf(“My name is %s\n”, “Dan”);
Daniel Deutsch
268
Printf
• Examples:
int a = 7;
System.out.printf(“%3d\n”, a);
“ 7”
double d2 = 1234567.8;
System.out.printf(“%f\n”, d2);
System.out.printf(“%03d\n”, a); “007”
System.out.printf(“%2f\n”, d2);
double d = 3.2;
System.out.printf(“%04d\n”, d);
System.out.printf(“%.3f\n”, d);
System.out.printf(“%06.3f%%\n”, d);
System.out.printf(“My name is %s\n”, “Dan”);
Daniel Deutsch
269
Printf
• Examples:
int a = 7;
System.out.printf(“%3d\n”, a);
“ 7”
double d2 = 1234567.8;
System.out.printf(“%f\n”, d2);
System.out.printf(“%03d\n”, a); “007”
System.out.printf(“%2f\n”, d2);
double d = 3.2;
System.out.printf(“%04d\n”, d);
crashes!
System.out.printf(“%.3f\n”, d);
System.out.printf(“%06.3f%%\n”, d);
System.out.printf(“My name is %s\n”, “Dan”);
Daniel Deutsch
270
Printf
• Examples:
int a = 7;
System.out.printf(“%3d\n”, a);
“ 7”
double d2 = 1234567.8;
System.out.printf(“%f\n”, d2);
System.out.printf(“%03d\n”, a); “007”
System.out.printf(“%2f\n”, d2);
double d = 3.2;
System.out.printf(“%04d\n”, d);
crashes!
System.out.printf(“%.3f\n”, d);
“3.200”
System.out.printf(“%06.3f%%\n”, d);
System.out.printf(“My name is %s\n”, “Dan”);
Daniel Deutsch
271
Printf
• Examples:
int a = 7;
System.out.printf(“%3d\n”, a);
“ 7”
double d2 = 1234567.8;
System.out.printf(“%f\n”, d2);
System.out.printf(“%03d\n”, a); “007”
System.out.printf(“%2f\n”, d2);
double d = 3.2;
System.out.printf(“%04d\n”, d);
crashes!
System.out.printf(“%.3f\n”, d);
“3.200”
System.out.printf(“%06.3f%%\n”, d);
“03.200%”
System.out.printf(“My name is %s\n”, “Dan”);
Daniel Deutsch
272
Printf
• Examples:
int a = 7;
System.out.printf(“%3d\n”, a);
“ 7”
double d2 = 1234567.8;
System.out.printf(“%f\n”, d2);
System.out.printf(“%03d\n”, a); “007”
System.out.printf(“%2f\n”, d2);
double d = 3.2;
System.out.printf(“%04d\n”, d);
crashes!
System.out.printf(“%.3f\n”, d);
“3.200”
System.out.printf(“%06.3f%%\n”, d);
“03.200%”
System.out.printf(“My name is %s\n”, “Dan”);
Daniel Deutsch
“My name is Dan”
273
Printf
• Examples:
int a = 7;
System.out.printf(“%3d\n”, a);
“ 7”
double d2 = 1234567.8;
System.out.printf(“%f\n”, d2);
1234567.800000
System.out.printf(“%03d\n”, a); “007”
System.out.printf(“%2f\n”, d2);
double d = 3.2;
System.out.printf(“%04d\n”, d);
crashes!
System.out.printf(“%.3f\n”, d);
“3.200”
System.out.printf(“%06.3f%%\n”, d);
“03.200%”
System.out.printf(“My name is %s\n”, “Dan”);
Daniel Deutsch
“My name is Dan”
274
Printf
• Examples:
int a = 7;
System.out.printf(“%3d\n”, a);
“ 7”
double d2 = 1234567.8;
System.out.printf(“%f\n”, d2);
1234567.800000
System.out.printf(“%03d\n”, a); “007”
System.out.printf(“%2f\n”, d2);
1234567.800000
double d = 3.2;
System.out.printf(“%04d\n”, d);
crashes!
System.out.printf(“%.3f\n”, d);
“3.200”
System.out.printf(“%06.3f%%\n”, d);
“03.200%”
System.out.printf(“My name is %s\n”, “Dan”);
Daniel Deutsch
“My name is Dan”
275
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
276
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
277
a=3
If Statements
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
278
a=3
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
Since a is equal to 3, the expression a == 3
evaluates to true
279
if (a == 3)
a=3
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
280
if (a == 3)
a=3
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
Therefore it executes the next line.
281
if (a == 3)
a=3
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
equal
Therefore it executes the next line.
282
If Statements
a=3
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
equal
283
If Statements
a=3
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
equal
Since this statement isn’t
contained in the if
statement, it will always
run.
284
If Statements
a=3
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
equal
always runs
Since this statement isn’t
contained in the if
statement, it will always
run.
285
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
equal
always runs
286
a=6
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
equal
always runs
287
a=6
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
equal
always runs
Since a = 6, this statement
evaluates to false
288
If Statements
a=6
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
equal
always runs
289
If Statements
a=6
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
equal
always runs
Therefore, it skips the line
in the if statement.
290
If Statements
a=6
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
equal
always runs
always runs
291
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
292
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
Daniel Deutsch
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
293
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
a=3
statement block (1 line or curly
braces) will execute
Daniel Deutsch
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
294
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
a=3
else
System.out.println(“not equal”);
Daniel Deutsch
295
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
a=3
else
System.out.println(“not equal”);
equal
Daniel Deutsch
296
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
a=3
Daniel Deutsch
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
equal
297
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
a=3
Daniel Deutsch
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
equal
Since a == 3 evaluated to true, the
statement in the else did not
298 run.
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next a = 4
statement block (1 line or curly
braces) will execute
Daniel Deutsch
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
299
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
a=4
Daniel Deutsch
300
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
a=4
Daniel Deutsch
Since a == 3 as false, the line in the if
statement was not executed, but the else
301
is.
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
equal
always runs
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
a=4
not equal
Daniel Deutsch
302
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
equal
always runs
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
a=4
Daniel Deutsch
not equal
303
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
If Statements
equal
always runs
• If statements are useful when
you want to change the
commands which execute based
on the values of variables
• They use expressions which
evaluate to Booleans
• If the Boolean is true, the next
statement block (1 line or curly
braces) will execute
always runs
• If statements can combine with else
statements for even more control.
• An else statement only executes if the
if statement evaluated to false
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
a=4
Daniel Deutsch
not equal
304
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
Daniel Deutsch
305
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
306
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
Curly braces are optional if
there is only 1 statement in
the blocks. Otherwise, you
need them.
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
307
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
308
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
‘a’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
309
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
‘a’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
310
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
‘a’
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
311
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
if (choice == ‘a’)
{
System.out.println(“a chosen”);
‘a’
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
312
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
Since choice == ‘a’ was true,
none of the other else-ifs or else
statements will be executed.
‘a’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
313
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
‘b’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
314
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘b’
‘a’
a chosen
‘b’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
315
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘b’
‘a’
a chosen
‘b’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
316
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
‘b’
‘b’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
317
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
‘b’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
‘b’
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
318
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
‘b’
b chosen
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
‘b’
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
319
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
‘a’
a chosen
‘b’
b chosen
‘b’
Daniel Deutsch
320
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
‘b’
b chosen
‘c’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
321
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘c’
‘a’
a chosen
‘b’
b chosen
‘c’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
322
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘c’
‘a’
a chosen
‘b’
b chosen
‘c’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
323
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
‘b’
b chosen
‘c’
‘c’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
Daniel Deutsch
324
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
‘b’
b chosen
‘c’
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
‘c’
}
Daniel Deutsch
325
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
‘b’
b chosen
‘c’
Since all of
the other if
statements
evaluated to
false, the else
statement
runs.
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
‘c’
}
Daniel Deutsch
326
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
‘a’
a chosen
‘b’
b chosen
‘c’
Neither
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
‘c’
}
Daniel Deutsch
327
If Statements
• If you have mutually exclusive options, you can use an if-else-if
structure. If you include an else, exactly one of the options will be
executed
char choice = kb.next().charAt(0);
if (choice == ‘a’)
{
System.out.println(“a chosen”);
}
else if (choice == ‘b’)
{
System.out.println(“b chosen”);
}
else
{
System.out.println(“Neither”);
}
‘a’
a chosen
‘b’
b chosen
‘c’
Neither
‘c’
Daniel Deutsch
328
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
Daniel Deutsch
329
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
This statement will never run when a = 2 and b = 3. It’s impossible
Daniel Deutsch
330
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5)
System.out.println(“a is 8”);
System.out.println(“b is 5”);
Daniel Deutsch
331
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 8, b = 5
if (a == 8 && b == 5)
System.out.println(“a is 8”);
System.out.println(“b is 5”);
Daniel Deutsch
332
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5)
a = 8, b = 5
System.out.println(“a is 8”);
System.out.println(“b is 5”);
Daniel Deutsch
333
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5)
a = 8, b = 5
System.out.println(“a is 8”);
System.out.println(“b is 5”);
a is 8
Daniel Deutsch
334
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5)
System.out.println(“a is 8”);
System.out.println(“b is 5”);
a = 8, b = 5
a is 8
Daniel Deutsch
335
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5)
System.out.println(“a is 8”);
System.out.println(“b is 5”);
a = 8, b = 5
a is 8
b is 5
Daniel Deutsch
336
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5)
System.out.println(“a is 8”);
System.out.println(“b is 5”);
a = 8, b = 5
a is 8
b is 5
Daniel Deutsch
Great, this seems to work.
337
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 8, b = 10
if (a == 8 && b == 5)
System.out.println(“a is 8”);
System.out.println(“b is 5”);
a is 8
b is 5
Daniel Deutsch
338
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5)
System.out.println(“a is 8”);
System.out.println(“b is 5”);
a = 8, b = 10
a is 8
b is 5
Daniel Deutsch
339
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5)
System.out.println(“a is 8”);
System.out.println(“b is 5”);
a = 8, b = 10
a is 8
b is 5
Daniel Deutsch
b is 5
340
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5)
System.out.println(“a is 8”);
System.out.println(“b is 5”);
a = 8, b = 10
a is 8
b is 5
b is 5
“b is 5” was still printed. This is because of
missing curly braces. Don’t let misleading
indentation fool you. Unless there are curly
braces, only the first statement after the if is
contained in the block.
Daniel Deutsch
341
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
a is 8
b is 5
b is 5
“b is 5” was still printed. This is because of
missing curly braces. Don’t let misleading
indentation fool you. Unless there are curly
braces, only the first statement after the if is
contained in the block.
Daniel Deutsch
342
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 8, b = 10
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
a is 8
b is 5
Daniel Deutsch
b is 5
343
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2); a = 8, b = 10
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
a is 8
b is 5
Daniel Deutsch
b is 5
344
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2); a = 8, b = 10
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
a is 8
b is 5
Daniel Deutsch
b is 5
Nothing is printed. Great!
345
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
346
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
If you have nested if statements, the else goes
with the if block immediately before it.
347
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
If you have nested if statements, the else goes
with the if block immediately before it.
The curly braces around this if force the else to be
paired with it.
348
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5)
if (b <= 2)
System.out.println(“tiny”);
else
System.out.println(“huge”);
System.out.println(“end”);
If you have nested if statements, the else goes
with the if block immediately before it.
The curly braces around this if force the else to be
paired with it.
Daniel Deutsch
If they weren’t there, the else would be paired to
this if statement.
349
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 6,
b=1
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
350
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
a = 6,
System.out.println(“huge”);
b=1
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
351
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
a = 6,
System.out.println(“huge”);
b=1
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
352
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 6,
b=1
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
353
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 6,
b=1
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
354
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 5,
b=1
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
a = 5, b = 1
355
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
a = 5,
if (b <= 2)
b=1
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
a = 5, b = 1
356
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
a = 5,
System.out.println(“tiny”);
b=1
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
a = 5, b = 1
357
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
a = 5,
System.out.println(“tiny”);
b=1
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
a = 5, b = 1
tiny
358
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 5,
b=1
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
a = 5, b = 1
tiny
359
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 5,
b=1
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
a = 5, b = 1
tiny
end
360
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 5,
b=3
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
a = 5, b = 1
tiny
end
a = 5, b = 3
361
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
a = 5,
if (b <= 2)
b=3
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
a = 5, b = 1
tiny
end
a = 5, b = 3
362
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 5,
b=3
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
a = 5, b = 1
tiny
end
a = 5, b = 3
363
If Statements
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (a != 2 || b != 3)
System.out.println(2);
a = 5,
b=3
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
}
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
}
else
System.out.println(“huge”);
System.out.println(“end”);
Daniel Deutsch
a = 6, b = 1
huge
end
a = 5, b = 1
tiny
end
a = 5, b = 3
end
364
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
Daniel Deutsch
365
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
366
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
367
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
‘a’
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
368
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
‘a’
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
369
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
‘a’
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
370
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’:
case ‘A’:
‘a’
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
371
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’:
case ‘A’:
‘a’
‘a’
x++;
break;
Even though the first case matched, it goes on to the
case ‘b’:
next command, which is case ‘A.’ Since there is no break,
y++;
it will continue even though case ‘A’ does not match.
default:
z++;
}
Daniel Deutsch
372
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
‘a’
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
373
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
‘a’
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
374
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
‘a’
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
375
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
‘a’
Daniel Deutsch
‘a’
x++
376
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
‘a’
x++;
x++
break;
case ‘b’:
y++;
The break causes it to exit the switch statement
default:
z++;
}
‘a’
Daniel Deutsch
377
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
378
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
‘A’
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
379
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
‘A’
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
380
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
‘A’
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
381
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’:
case ‘A’:
‘A’
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
382
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
‘A’
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
383
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
‘A’
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
384
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
‘A’
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
385
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
‘A’
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
386
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
387
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
‘b’
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
388
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
‘b’
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
389
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’:
case ‘A’:
‘b’
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
390
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
‘b’
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
391
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
‘b’
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
392
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
‘b’
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
y++
393
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
‘b’
‘a’
‘A’
x++;
y++
x++
x++
break;
case ‘b’:
y++;
Since there is no break, ‘b’ falls through to the next case
default:
‘b’
z++;
}
Daniel Deutsch
394
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
‘b’
‘a’
‘A’
x++;
y++
x++
x++
break;
z++
case ‘b’:
y++;
Since there is no break, ‘b’ falls through to the next case
default:
‘b’
z++;
}
Daniel Deutsch
395
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
‘b’
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
y++
z++
396
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
y++
z++
397
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
‘c’
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
y++
z++
‘c’
398
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
‘c’
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
y++
z++
‘c’
399
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
‘c’
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
y++
z++
‘c’
400
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’:
case ‘A’:
‘c’
x++;
break;
case ‘b’:
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
y++
z++
‘c’
401
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
‘c’
y++;
default:
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
y++
z++
‘c’
402
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
x++;
break;
case ‘b’:
y++;
default:
‘c’
z++;
}
Daniel Deutsch
‘a’
x++
‘A’
x++
‘b’
y++
z++
‘c’
403
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
‘c’
‘b’
‘a’
‘A’
x++;
y++
x++
x++
break;
z++
case ‘b’:
y++;
default:
‘c’
No other case was executed, so it goes into default
z++;
}
Daniel Deutsch
404
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
‘c’
‘b’
‘a’
‘A’
x++;
y++
x++
x++
break;
z++
case ‘b’:
y++;
default:
No other case was executed, so it goes into default
z++;
‘c’
}
Daniel Deutsch
405
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
‘c’
‘b’
‘a’
‘A’
x++;
z++
y++
x++
x++
break;
z++
case ‘b’:
y++;
default:
No other case was executed, so it goes into default
z++;
‘c’
}
Daniel Deutsch
406
Switch Statements
• Switch statements are like if-else statements, but they allow for
multiple blocks to be executed
• Unless there is a break, it will continue to execute
• The default case is used when no other case is selected, kind of like
else
char choice = kb.next().charAt(0);
switch (choice) {
case ‘a’: case ‘A’:
‘c’
‘b’
‘a’
‘A’
x++;
z++
y++
x++
x++
break;
z++
case ‘b’:
y++;
default:
No other case was executed, so it goes into default
z++;
}
‘c’
Daniel Deutsch
407
Switch Statements
• You can write a switch statement based on an integer (or character)
• In Java 7, they added switches based on Strings, but we don’t want
you to do those
int a = userInput.nextInt();
switch (a) {
case 1:
...
}
String entry = userInput.next();
switch (entry) {
case “start”:
...
}
Daniel Deutsch
408
For Loops
• For loops are useful when code needs to be repeated and you know how many times it
needs to execute. You might not know it will execute exactly 5 times, but you do know it
needs to execute s.length() times
• There are 3 parts that create the for loop
Daniel Deutsch
409
For Loops
• For loops are useful when code needs to be repeated and you know how many times it
needs to execute. You might not know it will execute exactly 5 times, but you do know it
needs to execute s.length() times
• There are 3 parts that create the for loop
for (int i = 0; i < s.length(); i++)
initialization
condition
Daniel Deutsch
afterthought
410
For Loops
• For loops are useful when code needs to be repeated and you know how many times it
needs to execute. You might not know it will execute exactly 5 times, but you do know it
needs to execute s.length() times
• There are 3 parts that create the for loop
for (int i = 0; i < s.length(); i++)
condition
initialization
afterthought
• They are executed in the following order:
1. Initialization
2. Condition
1.
2.
3.
4.
If true, the body
Otherwise, end
Afterthought
Go to step 2
Daniel Deutsch
411
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
Daniel Deutsch
412
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
Daniel Deutsch
413
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=0
Daniel Deutsch
414
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=0
Daniel Deutsch
415
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=0
Daniel Deutsch
416
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=0
0
Daniel Deutsch
417
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=0
0
Daniel Deutsch
418
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=1
0
Daniel Deutsch
419
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=1
0
Daniel Deutsch
420
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=1
0
Daniel Deutsch
421
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=1
01
Daniel Deutsch
422
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=1
01
Daniel Deutsch
423
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=2
01
Daniel Deutsch
424
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=2
01
Daniel Deutsch
425
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=2
01
Daniel Deutsch
426
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=2
012
Daniel Deutsch
427
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=9
0123456789
Fast forward…
Daniel Deutsch
428
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i=9
0123456789
Daniel Deutsch
429
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i = 10
0123456789
Daniel Deutsch
430
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i = 10
0123456789
Daniel Deutsch
431
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
i = 10
0123456789
Now i is not less than 10, so the for loop finishes.
Daniel Deutsch
432
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Daniel Deutsch
433
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Daniel Deutsch
434
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=0
Daniel Deutsch
435
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=0
Daniel Deutsch
436
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=0
Daniel Deutsch
437
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=0
I
Daniel Deutsch
438
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=0
I
Daniel Deutsch
439
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=2
I
Daniel Deutsch
440
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=2
I
Daniel Deutsch
441
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=2
I
Daniel Deutsch
442
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=2
Il
Daniel Deutsch
443
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=2
Il
Daniel Deutsch
444
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=4
Il
Daniel Deutsch
445
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=4
Il
Daniel Deutsch
446
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=4
Il
Daniel Deutsch
447
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i=4
Ilv
Daniel Deutsch
448
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i = 10
Ilv aa
Fast forward…
Daniel Deutsch
449
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i = 10
Ilv aa
Daniel Deutsch
450
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i = 12
Ilv aa
Daniel Deutsch
451
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i = 12
Ilv aa
Daniel Deutsch
452
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
i = 12
Ilv aa
i is not less than 11, the length of the String
Daniel Deutsch
453
For Loops
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Ilv aa
Daniel Deutsch
454
For Loops
for (int i = 10; i < 0; i--)
System.out.print(i + “ “);
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Ilv aa
Daniel Deutsch
455
For Loops
for (int i = 10; i < 0; i--)
System.out.print(i + “ “);
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
This for loop will never execute. It starts off
with i = 10 and makes sure that i < 0. Since
that’s false, it skips it.
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Ilv aa
Daniel Deutsch
456
For Loops
for (int i = 10; i > 0; i--)
System.out.print(i + “ “);
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
If you flip the sign, this will print
10 9 8 7 6 5 4 3 2 1
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Ilv aa
Daniel Deutsch
457
For Loops
for (int i = 10; i > 0; i--)
System.out.print(i + “ “);
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
10 9 8 7 6 5 4 3 2 1
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Ilv aa
Daniel Deutsch
458
For Loops
for (int i = 10; i > 0; i--)
System.out.print(i + “ “);
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
10 9 8 7 6 5 4 3 2 1
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Just like if statements, curly braces here are important.
Ilv aa
Daniel Deutsch
459
For Loops
for (int i = 10; i > 0; i--)
System.out.print(i + “ “);
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
10 9 8 7 6 5 4 3 2 1
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Just like if statements, curly braces here are important.
for (int i = 0; i < 10; i++)
System.out.println((i * 2) + “ “);
System.out.println(“finished body”);
Ilv aa
Daniel Deutsch
460
For Loops
for (int i = 10; i > 0; i--)
System.out.print(i + “ “);
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
10 9 8 7 6 5 4 3 2 1
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Ilv aa
Just like if statements, curly braces here are important.
for (int i = 0; i < 10; i++)
System.out.println((i * 2) + “ “);
System.out.println(“finished body”);
Despite the indentation, this is equivalent to:
Daniel Deutsch
461
For Loops
for (int i = 10; i > 0; i--)
System.out.print(i + “ “);
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
10 9 8 7 6 5 4 3 2 1
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Ilv aa
Just like if statements, curly braces here are important.
for (int i = 0; i < 10; i++)
System.out.println((i * 2) + “ “);
System.out.println(“finished body”);
Despite the indentation, this is equivalent to:
for (int i = 0; i < 10; i++)
System.out.println((i * 2) + “ “);
System.out.println(“finished body”);
Daniel Deutsch
462
For Loops
for (int i = 10; i > 0; i--)
System.out.print(i + “ “);
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
10 9 8 7 6 5 4 3 2 1
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Ilv aa
Just like if statements, curly braces here are important.
for (int i = 0; i < 10; i++) {
System.out.println((i * 2) + “ “);
System.out.println(“finished body”);
}
But if we add curly braces, it will do what we want.
Daniel Deutsch
463
For Loops
for (int i = 10; i > 0; i--)
System.out.print(i + “ “);
for (int i = 0; i < 10; i++) {
System.out.print(i + “ “);
}
10 9 8 7 6 5 4 3 2 1
0123456789
012345678910
String s = “I love Java”;
for (int i = 0; i < s.length(); i += 2)
System.out.print(s.charAt(i));
Ilv aa
Just like if statements, curly braces here are important.
for (int i = 0; i < 10; i++) {
System.out.println((i * 2) + “ “);
System.out.println(“finished body”);
0
finished body }
But if we add curly braces, it will do what we want.
2
finished body
4
…
18
finished body
Daniel Deutsch
464
For Loops
• Write a program to print a triangle whose base and height is given by
user input
Daniel Deutsch
465
For Loops
• Write a program to print a triangle whose base and height is given by
user input
The first thing to do
is write the class
definition and the
main method
header.
Daniel Deutsch
466
For Loops
• Write a program to print a triangle whose base and height is given by
user input
The first thing to do public class Triangle {
public static void main(String[] args) {
is write the class
definition and the
main method
header.
}
}
Daniel Deutsch
467
For Loops
• Write a program to print a triangle whose base and height is given by
user input
Now we are going
to have to ask the
user for input. That
requires using the
Scanner class, so
add the correct
import
public class Triangle {
public static void main(String[] args) {
}
}
Daniel Deutsch
468
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Now we are going
to have to ask the
user for input. That
requires using the
Scanner class, so
add the correct
import
public class Triangle {
public static void main(String[] args) {
}
}
Daniel Deutsch
469
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
Now let’s create
public static void main(String[] args) {
the Scanner to read
from the keyboard
}
}
Daniel Deutsch
470
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
Now let’s create
public static void main(String[] args) {
the Scanner to read
Scanner userInput = new Scanner(System.in);
from the keyboard
}
}
Daniel Deutsch
471
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Prompt the user for public class Triangle {
public static void main(String[] args) {
what they are
Scanner userInput = new Scanner(System.in);
supposed to input.
We are going to
assume that the
base == height
}
}
Daniel Deutsch
472
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Prompt the user for public class Triangle {
public static void main(String[] args) {
what they are
Scanner userInput = new Scanner(System.in);
supposed to input.
System.out.print(“Please enter the height/base: “);
We are going to
assume that the
base == height
}
}
Daniel Deutsch
473
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Now let’s actually
read in the value
public class Triangle {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
}
}
Daniel Deutsch
474
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Now let’s actually
read in the value
public class Triangle {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
}
}
Daniel Deutsch
475
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Since we have all of public class Triangle {
public static void main(String[] args) {
the information
Scanner userInput = new Scanner(System.in);
from the user, all
System.out.print(“Please enter the height/base: “);
we need to do is
int length = userInput.nextInt();
print the triangle
}
}
Daniel Deutsch
476
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Here is an example
public class Triangle {
triangle with the
public static void main(String[] args) {
rows labeled. The
Scanner userInput = new Scanner(System.in);
fact that the picture
System.out.print(“Please enter the height/base: “);
is two dimensional
int length = userInput.nextInt();
is a good indication
that we’re going to
0 *
need nested for
1 **
loops
2 ***
3 ****
}
}
Daniel Deutsch
477
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
The outer for loop
will print one row.
That is, after 1
iteration of the
outer for loop, a
single row will have
been printed
public class Triangle {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
0
1
2
3
for (int i = 0; i < length; i++) {
*
**
***
****
}
}
}
Daniel Deutsch
478
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
The inner for loop
public class Triangle {
needs to print the
public static void main(String[] args) {
correct number of
Scanner userInput = new Scanner(System.in);
*, then move to the
System.out.print(“Please enter the height/base: “);
next line. If you
int length = userInput.nextInt();
notice, the number
of stars on each
for (int i = 0; i < length; i++) {
0 *
row is the row
1 **
number + 1
2 ***
3 ****
}
}
}
Daniel Deutsch
479
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Therefore, the
bounds on the
inner for loop
should start at 0
and run i + 1 times
where i is the row
number
public class Triangle {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
0
1
2
3
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
*
**
***
****
}
}
}
}
Daniel Deutsch
480
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Then we need to
print a * without
going to the next
line in the inner for
loop
public class Triangle {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
0
1
2
3
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
*
**
***
****
}
}
}
}
Daniel Deutsch
481
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Then we need to
print a * without
going to the next
line in the inner for
loop
public class Triangle {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
0
1
2
3
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
*
**
***
****
}
}
}
Daniel Deutsch
482
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Finally, after all of
public class Triangle {
the * for one row
public static void main(String[] args) {
have been printed,
Scanner userInput = new Scanner(System.in);
we need to end the
System.out.print(“Please enter the height/base: “);
line to prepare for
int length = userInput.nextInt();
the next row. We
do that by printing
for (int i = 0; i < length; i++) {
0 *
“\n” or
for (int j = 0; j < i + 1; j++) {
System.out.println() 1 **
System.out.print(“*”);
2 ***
}
3 ****
}
}
}
Daniel Deutsch
483
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
Finally, after all of
public class Triangle {
the * for one row
public static void main(String[] args) {
have been printed,
Scanner userInput = new Scanner(System.in);
we need to end the
System.out.print(“Please enter the height/base: “);
line to prepare for
int length = userInput.nextInt();
the next row. We
do that by printing
for (int i = 0; i < length; i++) {
0 *
“\n” or
for (int j = 0; j < i + 1; j++) {
System.out.println() 1 **
System.out.print(“*”);
2 ***
}
3 ****
System.out.println();
}
}
Daniel Deutsch
484
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
}
Daniel Deutsch
485
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
}
Daniel Deutsch
486
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
}
Daniel Deutsch
487
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base:
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
}
Daniel Deutsch
488
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base:
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
}
Daniel Deutsch
489
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
}
Daniel Deutsch
490
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
}
}
Daniel Deutsch
491
4
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
}
}
Daniel Deutsch
492
4
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
}
}
Daniel Deutsch
493
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
}
}
Daniel Deutsch
494
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
j
0
}
}
Daniel Deutsch
495
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
j
0
}
}
Daniel Deutsch
496
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
The red arrow will be
where we will print
the next character
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
j
0
}
}
Daniel Deutsch
497
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
j
0
}
}
Daniel Deutsch
498
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
j
0
}
}
Daniel Deutsch
499
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
j
1
}
}
Daniel Deutsch
500
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
j
1
}
}
Daniel Deutsch
501
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
False!
System.out.println();
1<0+1
}
length
4
i
0
j
1
}
}
Daniel Deutsch
502
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
}
}
Daniel Deutsch
503
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
0
}
}
Daniel Deutsch
504
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
}
}
Daniel Deutsch
505
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
}
}
Daniel Deutsch
506
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
}
}
Daniel Deutsch
507
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
0
}
}
Daniel Deutsch
508
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
0
}
}
Daniel Deutsch
509
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
*
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
0
}
}
Daniel Deutsch
510
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
*
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
0
}
}
Daniel Deutsch
511
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
*
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
1
}
}
Daniel Deutsch
512
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
*
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
1
}
}
Daniel Deutsch
513
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
*
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
1
}
}
Daniel Deutsch
514
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
1
}
}
Daniel Deutsch
515
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
1
}
}
Daniel Deutsch
516
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
2
}
}
Daniel Deutsch
517
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
j
2
}
}
Daniel Deutsch
518
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
False!
}
2<1+1
length
4
i
1
j
2
}
}
Daniel Deutsch
519
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
}
}
Daniel Deutsch
520
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
length
4
i
1
}
}
Daniel Deutsch
521
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
Let’s fast forward a bit
length
4
i
1
}
}
Daniel Deutsch
522
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
***
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
Let’s fast forward a bit
}
System.out.println();
j 2
}
}
Daniel Deutsch
523
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
***
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
}
System.out.println();
j 3
}
}
Daniel Deutsch
524
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
***
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
}
System.out.println();
j 3
}
}
Daniel Deutsch
525
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
***
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
}
System.out.println();
j 3
}
}
Daniel Deutsch
526
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
}
System.out.println();
j 3
}
}
Daniel Deutsch
527
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
}
System.out.println();
j 4
}
}
Daniel Deutsch
528
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
}
System.out.println();
j 4
}
}
Daniel Deutsch
529
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
}
False!
System.out.println();
j 4
4<3+1
}
}
Daniel Deutsch
530
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
}
System.out.println();
}
}
Daniel Deutsch
531
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
}
System.out.println();
}
}
Daniel Deutsch
532
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
3
i
System.out.print(“*”);
}
System.out.println();
}
}
Daniel Deutsch
533
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
4
i
System.out.print(“*”);
}
System.out.println();
}
}
Daniel Deutsch
534
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
4
i
System.out.print(“*”);
}
System.out.println();
}
}
Daniel Deutsch
535
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
4
i
System.out.print(“*”);
}
False!
System.out.println();
4<4
}
}
Daniel Deutsch
536
}
For Loops
• Write a program to print a triangle whose base and height is given by
user input import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Please enter the height/base: 4
Scanner userInput = new Scanner(System.in);
*
System.out.print(“Please enter the height/base: “);
**
int length = userInput.nextInt();
***
****
length 4
for (int i = 0; i < length; i++) {
for (int j = 0; j < i + 1; j++) {
4
i
System.out.print(“*”);
}
System.out.println();
}
}
Daniel Deutsch
537
}
While and Do-While Loops
• While loops are useful when you don’t know how many times the
loop will run. Could be based on user input
• Do-while loops are useful when you don’t know how many times it
will run, but it will run at least once
Daniel Deutsch
538
While and Do-While Loops
• While loops are useful when you don’t know how many times the
loop will run. Could be based on user input
• Do-while loops are useful when you don’t know how many times it
will run, but it will run at least once
int num = 0;
while (num < 5) {
if (kb.next().equals(“y”))
num++;
}
Daniel Deutsch
539
While and Do-While Loops
• While loops are useful when you don’t know how many times the
loop will run. Could be based on user input
• Do-while loops are useful when you don’t know how many times it
will run, but it will run at least once
int num = 0;
while (num < 5) {
if (kb.next().equals(“y”))
num++;
}
It will first check the condition. If it’s true, it will
execute the body, then repeat
Daniel Deutsch
540
While and Do-While Loops
• While loops are useful when you don’t know how many times the
loop will run. Could be based on user input
• Do-while loops are useful when you don’t know how many times it
will run, but it will run at least once
Unless the value of num changes in
int num = 0;
the body, this will run forever.
while (num < 5) {
if (kb.next().equals(“y”))
num++;
}
It will first check the condition. If it’s true, it will
execute the body, then repeat
Daniel Deutsch
541
While and Do-While Loops
• While loops are useful when you don’t know how many times the
loop will run. Could be based on user input
• Do-while loops are useful when you don’t know how many times it
will run, but it will run at least once
int num = 0;
while (num < 5) {
if (kb.next().equals(“y”))
num++;
}
do {
char choice = kb.next().charAt(0);
// display menu
} while (choice != ‘q’);
Daniel Deutsch
542
While and Do-While Loops
• While loops are useful when you don’t know how many times the
loop will run. Could be based on user input
• Do-while loops are useful when you don’t know how many times it
will run, but it will run at least once
int num = 0;
while (num < 5) {
if (kb.next().equals(“y”))
num++;
}
do {
char choice = kb.next().charAt(0);
// display menu
} while (choice != ‘q’);
The do while first execute the body, then checks the condition
after. It is guaranteed to run at least once
Daniel Deutsch
543
While and Do-While Loops
• While loops are useful when you don’t know how many times the
loop will run. Could be based on user input
• Do-while loops are useful when you don’t know how many times it
will run, but it will run at least once
int num = 0;
while (num < 5) {
if (kb.next().equals(“y”))
num++;
}
do {
char choice = kb.next().charAt(0);
// display menu
} while (choice != ‘q’);
Notice the semicolon here. It is important
Daniel Deutsch
544
File Structure
Daniel Deutsch
545
File Structure
• The class declaration goes first
class declaration
public class MyClass {
}
Daniel Deutsch
546
File Structure
• The class declaration goes first
class declaration
public class MyClass {
}
Daniel Deutsch
The class name must match the name of
the Java file. This would be saved in
MyClass.java
547
File Structure
• The class declaration goes first
• Method declarations of the class go in the body
class declaration
public class MyClass {
public static void main(String[] args) {
main method
// code goes here
}
public static int add(a, int b) {
return a + b;
}
other methods
}
Daniel Deutsch
548
File Structure
• The class declaration goes first
• Method declarations of the class go in the body
class declaration
public class MyClass {
Memorize this line!
public static void main(String[] args) {
main method
// code goes here
}
public static int add(a, int b) {
return a + b;
}
other methods
}
Daniel Deutsch
549
File Structure
• The class declaration goes first
• Method declarations of the class go in the body
• Any imports/package declarations go outside of the class
imports
class declaration
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
main method
// code goes here
}
public static int add(a, int b) {
return a + b;
}
other methods
}
Daniel Deutsch
550
Testing
• White box testing: every line of code get executed.
Daniel Deutsch
551
Testing
• White box testing: every line of code get executed.
• The white box means you can see the code and make the test cases based on
the code
• Test boundary cases
if (0 <= a && a <= 10)
• Test invalid and valid cases
Test -1, 0, 1, 9, 10, and 11.
Probably values in between
Daniel Deutsch
552
Testing
• White box testing: every line of code get executed.
• The white box means you can see the code and make the test cases based on
the code
• Test boundary cases
if (0 <= a && a <= 10)
• Test invalid and valid cases
Test -1, 0, 1, 9, 10, and 11.
Probably values in between
• Regression testing: rewriting code or adding code and making sure
that everything that worked before works now
Daniel Deutsch
553
Testing
• White box testing: every line of code get executed.
• The white box means you can see the code and make the test cases based on
the code
• Test boundary cases
if (0 <= a && a <= 10)
• Test invalid and valid cases
Test -1, 0, 1, 9, 10, and 11.
Probably values in between
• Regression testing: rewriting code or adding code and making sure
that everything that worked before works now
• Black box testing: try many values that are valid/invalid. You don’t
know what the program is doing, so you try many cases
Daniel Deutsch
554