Module 6
Using Operators and Decision
Constructs
Overview
• Objectives:
– Identify relational and conditional operators
– Create if and if/else constructs
– Use the switch constructs
Using Relational and Conditional
Operators
Elevator Example
1
2 public class Elevator {
3
4 public boolean doorOpen=false; // Doors are closed by default
5 public int currentFloor = 1; // All elevators start on first floor
6 public final int TOP_FLOOR = 10;
7 public final int MIN_FLOORS = 1;
8
9 public void openDoor() {
10 [Link](“Opening door.”);
11 doorOpen = true;
12 [Link](“Door is open.”);
13 }
14
15 public void closeDoor() {
16 [Link](“Closing door.”);
17 doorOpen = false;
18 [Link](“Door is closed.”);
19 }
Elevator Example
20
21 public void goUp() {
22 [Link](“Going up one floor.”);
23 currentFloor++;
24 [Link](“Floor: “ + currentFloor);
25 }
26
27 public void goDown() {
28 [Link](“Going down one floor.”);
29 currentFloor--;
30 [Link](“Floor: “ + currentFloor);
31 }
32
33
34 }
35
The ElevatorTest
2 public class ElevatorTest {
3
4 public static void main(String args[]) {
5
6 Elevator myElevator = new Elevator();
7
8 [Link]();
9 [Link]();
10 [Link]();
11 [Link]();
12 [Link]();
13 [Link]();
14 [Link]();
15 [Link]();
16 [Link]();
17 [Link]();
18 [Link]();
19 [Link]();
20 }
21 }
Relational Operators
Testing Equality Between Strings
2 public class Employees {
3
4 public String name1 = “Fred Smith”;
5 public String name2 = “Joseph Smith”;
6
7 public void areNamesEqual() {
8
9 if ([Link](name2)) {
10 [Link](“Same name.”);
11 }
12 else {
13 [Link](“Different name.”);
14 }
15 }
16 }
Common Conditional Operators
The if Construct
• Syntax:
if (boolean_expression) {
code_block;
} // end of if construct
// program continues here
•Example of potential output:
Opening door.
Door is open.
Closing door.
Door is closed.
Going down one floor.
Floor: 0 <--- this is a error in logic
Going up one floor.
Floor: 1
Going up one floor.
Floor: 2
...
The if Construct
• Example of potential solution:
21 public void goUp() {
22 [Link](“Going up one floor.”);
23 currentFloor++;
24 [Link](“Floor: “ + currentFloor);
25 }
26
27 public void goDown() {
28
29 if (currentFloor == MIN_FLOORS) {
30 [Link](“Cannot Go down”);
31 }
32
33 if (currentFloor > MIN_FLOORS) {
34 [Link](“Going down one floor.”);
35 currentFloor--;
36 [Link](“Floor: “ + currentFloor);
37 }
38 }
The if Construct
• Example potential output:
Opening door.
Door is open.
Closing door.
Door is closed.
Cannot Go down <--- elevator logic prevents problem
Going up one floor.
Floor: 2
Going up one floor.
Floor: 3
...
Nested if Statements
21 public void goUp() {
22 [Link](“Going up one floor.”);
23 currentFloor++;
24 [Link](“Floor: “ + currentFloor);
25 }
26
27 public void goDown() {
28
29 if (currentFloor == MIN_FLOORS) {
30 [Link](“Cannot Go down”);
31 }
32
33 if (currentFloor > MIN_FLOORS) {
34
35 if (!doorOpen) {
37 [Link](“Going down one floor.”);
38 currentFloor--;
39 [Link](“Floor: “ + currentFloor);
40 }
41 }
42 }
The if/else Construct
• Syntax:
– if (boolean_expression) {
code_block;
} // end of if construct
else {
code_block;
} // end of else construct
// program continues here
The if/else Construct
27 public void goDown() {
28
29 if (currentFloor == MIN_FLOORS) {
30 [Link](“Cannot Go down”);
31 }
32
33 else {
34 [Link](“Going down one floor.”);
35 currentFloor--;
36 [Link](“Floor: “ + currentFloor);
37 }
38 }
Chaining if/else Constructs
• Syntax:
if (boolean_expression) {
code_block;
} else if (boolean_expression){
code_block;
} else {
code_block;
}
Chaining if/else Constructs
1
2 public class IfElseDate {
3
4 public int month = 10;
5
6 public void calculateNumDays() {
7
8 if (month == 1 || month == 3 || month == 5 || month == 7 ||
9 month == 8 || month == 10 || month == 12) {
10
11 [Link](“There are 31 days in that month.”);
12 }
13
14 else if (month == 2) {
15 [Link](“There are 28 days in that month.”);
16 }
17
Chaining if/else Constructs
18 else if (month == 4 || month == 6 || month == 9 || month == 11) {
19 [Link](“There are 30 days in that month.”);
20 }
21
22 else {
23 [Link](“Invalid month.”);
24 }
25 }
26 }
27
Exercise 1: Using if and if/else
Constructs
Using the switch Construct
• Syntax:
switch (variable) {
case literal_value:
code_block;
[break;]
• case another_literal_value:
code_block;
[break;]
• [default:]
code_block;
• }
Using the switch Construct
1
2 public class SwitchDate {
3
4 public int month = 10;
5
6 public void calculateNumDays() {
7
8 switch(month) {
9 case 1:
10 case 3:
11 case 5:
12 case 7:
13 case 8:
14 case 10:
15 case 12:
16 [Link](“There are 31 days in that month.”);
17 break.
Using the switch Construct
18 case 2:
19 [Link](“There are 28 days in that month.”);
20 break;
21 case 4:
22 case 6:
23 case 9:
24 case 11:
25 [Link](“There are 30 days in that month.”);
26 break;
27 default:
28 [Link](“Invalid month.”);
29 break;
30 }
31 }
32 }
33
When to Use switch Constructs
• Equality tests
• Tests against a single value, such as
customerStatus
• Tests against the value of an int, short, byte,
or char type
Exercise 2: Using the switch Construct