Download Chapter 3 Control Methods

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 3 Selections
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
1
Making decisions
Using IF statements
AreaOfFourSidedObject.java
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
2
The boolean Type and Operators
Can be used to make decisions
Boolean values: true or false.
boolean b = (1 > 2);
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
3
Comparison Operators
Java
Operator
Mathematics
Symbol
Name
Example
(radius is 5)
<
<
less than
radius < 0
false
<=
≤
less than or equal to
radius <= 0
false
>
>
greater than
radius > 0
true
>=
≥
greater than or equal to
radius >= 0
true
==
=
equal to
radius == 0
false
!=
≠
not equal to
radius != 0
true
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
Result
4
Using boolean
AdditionQuiz.java
import java.util.Scanner;
public class AdditionQuiz {
public static void main(String[] args) {
int number1 = (int)(System.currentTimeMillis() % 10);
int number2 = (int)(System.currentTimeMillis() / 7 % 10);
// Create a Scanner
Scanner input = new Scanner(System.in);
System.out.print(
"What is " + number1 + " + " + number2 + "? ");
int answer = input.nextInt();
System.out.println(
number1 + " + " + number2 + " = " + answer + " is " +
(number1 + number2 == answer));
}
}
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
5
Making decisions
Using IF statements
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
6
One-way if Statements
if (boolean-expression) {
statement(s);
}
Boolean
Expression
if (radius >= 0) {
area = radius * radius * PI;
System.out.println("The area"
+ " for the circle of radius "
+ radius + " is " + area);
}
false
false
(radius >= 0)
true
true
Statement(s)
(A)
area = radius * radius * PI;
System.out.println("The area for the circle of " +
"radius " + radius + " is " + area);
(B)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
7
Note
if i > 0 {
System.out.println("i is positive");
}
if (i > 0) {
System.out.println("i is positive");
}
(a) Wrong
if (i > 0) {
System.out.println("i is positive");
}
(b) Correct
Equivalent
if (i > 0)
System.out.println("i is positive");
(a)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
(b)
8
Simple if Demo.java
import java.util.Scanner;
public class SimpleIfDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
int number = input.nextInt();
if (number % 5 == 0)
System.out.println("HiFive");
if (number % 2 == 0)
System.out.println("HiEven");
}
}
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
9
X Problem: Guessing Birthday X
($5 to the first student who can explain this)
= 19
+
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
Set1
2
10
18
26
3
11
19
27
6
14
22
30
Set2
7
15
23
31
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31
Set3
Set4
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
16
20
24
28
17
21
25
29
18
22
26
30
Set5
10
19
23
27
31
Problem: Guessing Birthday
GuessBirthday.java
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
11
X Mathematics Basis for the Game X
19 is 10011 in binary. 7 is 111 in binary. 23 is 11101 in binary
10000
10
+
1
10011
10000
1000
100
+
1
11101
00110
10
+
1
00111
19
7
23
= 19
+
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
Set1
2
10
18
26
3
11
19
27
6
14
22
30
Set2
7
15
23
31
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31
Set3
Set4
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
16
20
24
28
17
21
25
29
18
22
26
30
Set5
12
19
23
27
31
The Two-way if Statement
if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
true
Statement(s) for the true case
Boolean
Expression
false
Statement(s) for the false case
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
13
if-else Example
if (radius >= 0) {
area = radius * radius * 3.14159;
System.out.println("The area for the “
+ “circle of radius " + radius +
" is " + area);
}
else {
System.out.println("Negative input");
}
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
14
Multi-Way if-else Statements
false
score >= 90
false
true
score >= 80
false
grade = 'A'
true
score >= 70
false
grade = 'B'
rue
grade = 'C'
score >= 60
true
grade = 'D'
grade = 'F'
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
15
Multiple Alternative if Statements
if (score >= 90.0)
grade = 'A';
else
if (score >= 80.0)
grade = 'B';
else
if (score >= 70.0)
grade = 'C';
else
if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Equivalent
if (score >= 90.0)
grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
16
Logical Operators
Operator Name
!
not
&&
and
||
or
^
exclusive or
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
17
Truth Table for Operator &&
p1
p2
p1 && p2
Example (assume age = 24, gender = 'F')
false
false
false
false
true
false
(age > 18) && (gender == 'F') is true, because (age
> 18) and (gender == 'F') are both true.
true
false
false
true
true
true
(age > 18) && (gender != 'F') is false, because
(gender != 'F') is false.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
18
Truth Table for Operator ||
p1
p2
p1 || p2
Example (assume age = 24, gender = 'F')
false
false
false
false
true
true
(age > 34) || (gender == 'F') is true, because (gender
== 'F') is true.
true
false
true
true
true
true
(age > 34) || (gender == 'M') is false, because (age >
34) and (gender == 'M') are both false.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
19
Truth Table for Operator ^
p1
p2
p1 ^ p2
Example (assume age = 24, gender = 'F')
false
false
false
false
true
true
(age > 34) ^ (gender == 'F') is true, because (age
> 34) is false but (gender == 'F') is true.
true
false
true
true
true
false
(age > 34) || (gender == 'M') is false, because (age
> 34) and (gender == 'M') are both false.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
20
animation
Trace if-else statement
Suppose score is 70.0
The condition is false
if (score >= 90.0)
grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
21
animation
Trace if-else statement
Suppose score is 70.0
The condition is false
if (score >= 90.0)
grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
22
animation
Trace if-else statement
Suppose score is 70.0
The condition is true
if (score >= 90.0)
grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
23
animation
Trace if-else statement
Suppose score is 70.0
grade is C
if (score >= 90.0)
grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
24
animation
Trace if-else statement
Suppose score is 70.0
Exit the if statement
if (score >= 90.0)
grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
25
Enough already!
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
26
Related documents