Chapter 1 Introduction to Java

Download Report

Transcript Chapter 1 Introduction to Java

Lecture #4
By
Dr. Basheer M. Nasef
L4:CSC210 2014-2015 ©
Dr. Basheer M. Nasef
Control Statements I
L4:CSC210 2014-2015 ©
3
Dr. Basheer M. Nasef
Home work
L4:CSC210 2014-2015 ©
4
Dr. Basheer M. Nasef
Home work
L4:CSC210 2014-2015 ©
5
Dr. Basheer M. Nasef
Home work
L4:CSC210 2014-2015 ©
6
Dr. Basheer M. Nasef
Motivations
If you assigned a negative value for radius in
ComputeArea.java, the program would print an invalid
result.
If the radius is negative, you don't want the program to
compute the area.
How can you deal with this situation?
Compute Area
L4:CSC210 2014-2015 ©
7
Dr. Basheer M. Nasef
 To
declare boolean type and use Boolean values true and
false .
 To
apply relational operators (<, <=, ==, !=, >, >=) and logic
operators (!, &&, ||, ^) to write Boolean expressions.
 To
 To
use Boolean expressions to control selection statements.
implement selection control using if and nested if
statements.
L4:CSC210 2014-2015 ©
Dr. Basheer M. Nasef
8
The boolean Type and Operators
Often in a program you need to compare two values, such as
whether i is greater than j.
Java provides six comparison operators (also known as
relational operators) that can be used to compare two values.
The result of the comparison is a Boolean value: true or false.
boolean b = (1 > 2);
L4:CSC210 2014-2015 ©
9
Dr. Basheer M. Nasef
Comparison Operators
Operator Name
<
less than
<=
less than or equal to
>
greater than
>=
greater than or equal to
==
equal to
!=
not equal to
L4:CSC210 2014-2015 ©
10
Dr. Basheer M. Nasef
Boolean Operators
Operator Name
!
not
&&
and
||
or
^
exclusive or
L4:CSC210 2014-2015 ©
11
Dr. Basheer M. Nasef
Truth Table for Operator !
p
!p
true
false
!(1 > 2) is true, because (1 > 2) is false.
false
true
!(1 > 0) is false, because (1 > 0) is true.
L4:CSC210 2014-2015 ©
Example
12
Dr. Basheer M. Nasef
Truth Table for Operator &&
p1
p2
p1 && p2
false
false
false
false
true
false
true
false
false
true
true
true
L4:CSC210 2014-2015 ©
13
Example
(3 > 2) && (5 >= 5) is true, because (3 >
2) and (5 >= 5) are both true.
(3 > 2) && (5 > 5) is false, because (5 >
5) is false.
Dr. Basheer M. Nasef
Truth Table for Operator ||
p1
p2
p1 || p2
false
false
false
false
true
true
true
false
true
true
true
true
L4:CSC210 2014-2015 ©
14
Example
(2 > 3) || (5 > 5) is false, because (2 > 3)
and (5 > 5) are both false.
(3 > 2) || (5 > 5) is true, because (3 > 2)
is true.
Dr. Basheer M. Nasef
Truth Table for Operator ^
p1
p2
p1 ^ p2
false
false
false
false
true
true
true
false
true
true
true
false
L4:CSC210 2014-2015 ©
15
Example
(2 > 3) ^ (5 > 1) is true, because (2 > 3)
is false and (5 > 1) is true.
(3 > 2) ^ (5 > 1) is false, because both (3
> 2) and (5 > 1) are true.
Dr. Basheer M. Nasef
Examples
System.out.println("Is " + number + " divisible by 2 and 3? " +
((number % 2 == 0) && (number % 3 == 0)));
System.out.println("Is " + number + " divisible by 2 or 3? " +
((number % 2 == 0) || (number % 3 == 0)));
System.out.println("Is " + number +
" divisible by 2 or 3, but not both? " +
((number % 2 == 0) ^ (number % 3 == 0)));
L4:CSC210 2014-2015 ©
16
Dr. Basheer M. Nasef
Problem:
Determining Leap Year?
This program first prompts the user to enter a year as
an int value and checks if it is a leap year.
A year is a leap year if it is divisible by 4 but not by
100, or it is divisible by 400.
(year % 4 == 0 && year % 100 != 0) || (year % 400
== 0)
LeapYear
L4:CSC210 2014-2015 ©
17
Run
Dr. Basheer M. Nasef
Problem:
A Simple Math Learning Tool
This example creates a program to let a first grader
practice additions. The program randomly
generates two single-digit integers number1 and
number2 and displays a question such as “What is
7 + 9?” to the student. After the student types the
answer, the program displays a message to indicate
whether the answer is true or false.
AdditionQuiz
L4:CSC210 2014-2015 ©
18
Run
Dr. Basheer M. Nasef
Control Statements

if Statements

switch Statements

Conditional Operators
L4:CSC210 2014-2015 ©
21
Dr. Basheer M. Nasef
Simple if Statements
if (booleanExpression) {
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
area = radius * radius * PI;
System.out.println("The area for the circle of " +
"radius " + radius + " is " + area);
Statement(s)
(A)
L4:CSC210 2014-2015 ©
(B)
22
Dr. Basheer M. Nasef
Note
Outer parentheses required
Braces can be omitted if the block contains a single
statement
if ((i > 0) && (i < 10)) {
System.out.println("i is an " +
+ "integer between 0 and 10");
}
(a)
L4:CSC210 2014-2015 ©
Equivalent
if ((i > 0) && (i < 10))
System.out.println("i is an " +
+ "integer between 0 and 10");
(b)
23
Dr. Basheer M. Nasef
Caution
Adding a semicolon at the end of an if clause is a common
mistake.
if (radius >= 0);
Wrong
{
area = radius*radius*PI;
System.out.println(
"The area for the circle of radius " +
radius + " is " + area);
}
This mistake is hard to find, because it is not a compilation
error or a runtime error, it is a logic error.
This error often occurs when you use the next-line block style.
L4:CSC210 2014-2015 ©
24
Dr. Basheer M. Nasef
The if...else Statement
if (booleanExpression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
true
Statement(s) for the true case
L4:CSC210 2014-2015 ©
25
Boolean
Expression
false
Statement(s) for the false case
Dr. Basheer M. Nasef
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");
}
L4:CSC210 2014-2015 ©
26
Dr. Basheer M. Nasef
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';
L4:CSC210 2014-2015 ©
27
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';
Dr. Basheer M. Nasef
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';
L4:CSC210 2014-2015 ©
28
Dr. Basheer M. Nasef
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';
L4:CSC210 2014-2015 ©
29
Dr. Basheer M. Nasef
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';
L4:CSC210 2014-2015 ©
30
Dr. Basheer M. Nasef
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';
L4:CSC210 2014-2015 ©
31
Dr. Basheer M. Nasef
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';
L4:CSC210 2014-2015 ©
32
Dr. Basheer M. Nasef
Note
The else clause matches the most recent if clause in
the same block.
int i = 1;
int j = 2;
int k = 3;
int i = 1;
int j = 2;
int k = 3;
Equivalent
if (i > j)
if (i > k)
System.out.println("A");
else
System.out.println("B");
(a)
L4:CSC210 2014-2015 ©
if (i > j)
if (i > k)
System.out.println("A");
else
System.out.println("B");
(b)
33
Dr. Basheer M. Nasef
Note
Nothing is printed from the preceding statement. To
force the else clause to match the first if clause, you
must add a pair of braces:
int i = 1;
int j = 2;
int k = 3;
if (i > j) {
if (i > k)
System.out.println("A");
}
else
System.out.println("B");
This statement prints B.
L4:CSC210 2014-2015 ©
34
Dr. Basheer M. Nasef
TIP
if (number % 2 == 0)
even = true;
else
even = false;
boolean even
= number % 2 == 0;
(b)
(a)
L4:CSC210 2014-2015 ©
Equivalent
35
Dr. Basheer M. Nasef
CAUTION
if (even == true)
System.out.println(
"It is even.");
if (even)
System.out.println(
"It is even.");
(b)
(a)
L4:CSC210 2014-2015 ©
Equivalent
36
Dr. Basheer M. Nasef
Problem:
An Improved Math Learning Tool
This example creates a program to teach a
first grade child how to learn subtractions.
The program randomly generates two
single-digit integers number1 and number2
with number1 > number2 and displays a
question such as “What is 9 – 2?” to the
student. After the student types the answer
in the input dialog box, the program
displays a message dialog box to indicate
whether the answer is correct.
SubtractionQuiz
L4:CSC210 2014-2015 ©
37
Run
Dr. Basheer M. Nasef
Problem:
Computing Taxes
The US federal personal income tax is calculated based
on the filing status and taxable income. There are four
filing statuses: single filers, married filing jointly,
married filing separately, and head of household. The
tax rates for 2002 are shown in Table 3.1.
L4:CSC210 2014-2015 ©
40
Dr. Basheer M. Nasef
Problem: Computing Taxes, cont.
if (status == 0) {
// Compute tax for single filers
}
else if (status == 1) {
// Compute tax for married file jointly
}
else if (status == 2) {
// Compute tax for married file separately
}
else if (status == 3) {
// Compute tax for head of household
}
else {
// Display wrong status
}
ComputeTax
L4:CSC210 2014-2015 ©
41
Run
Dr. Basheer M. Nasef
Problem: Guessing Birth Date
The program can guess your birth date.
Run to see how it works.
= 19
+
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
2
10
18
26
Set1
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
GuessBirthDate
L4:CSC210 2014-2015 ©
42
16
20
24
28
17
21
25
29
18
22
26
30
Set5
Run
Dr. Basheer M. Nasef
19
23
27
31
Problem
Suppose, when you run the program, you enter input 2 3 6 from the console. What
is the output?
public class Test {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
double x = input.nextDouble();
double y = input.nextDouble();
double z = input.nextDouble();
System.out.println("(x < y && y < z) is " + (x < y && y < z));
System.out.println("(x < y || y < z) is " + (x < y || y < z));
System.out.println("!(x < y) is " + !(x < y));
System.out.println("(x + y < z) is " + (x + y < z));
System.out.println("(x + y < z) is " + (x + y < z));
}
}
L4:CSC210 2014-2015 ©
43
Dr. Basheer M. Nasef
HW
L4:CSC210 2014-2015 ©
44
Dr. Basheer M. Nasef