***** swtjc stem ***** chapter 5 cg 57 selection construct choices 1.one-way selection - use simple...
TRANSCRIPT
***** SWTJC STEM *****
Chapter 5 cg 57
Selection Construct Choices
1. One-way selection - Use simple if.
2. Two-way selection - Use if - else.
3. if blocks
4. Nested if blocks 5. Multi-way selection - Use either:
1. Differing conditions - Use if - else if - else if - .... - else.
2. Same variable or expression with multiple choices - Use switch.
***** SWTJC STEM *****
Chapter 5 cg 57
Multi-Way Selection Exampleelse if
Use else if construct for multiple conditions.
Consider this example:
“A value x is classified as
x < 200 low
200 x < 500 medium
500 x < 800 medium-high
800 x high
Program classification output for a given value of x.”
***** SWTJC STEM *****
Chapter 5 cg 57
Multi-Way Selection Flowchartelse if
x<200?true
false
Entry Point
Exit Point
“low” x<500?
false
x<800?
false
“medium”
“medium-high”
“high”
true
true
***** SWTJC STEM *****
Chapter 5 cg 57
Multi-way Selection Code else if
int x = 250;
if (x < 200)
System.out.println("low");
else if (x < 500)
System.out.println("medium");
else if (x < 800)
System.out.println("medium-high");
else
System.out.println("high");
MultiWayElseIf.java
***** SWTJC STEM *****
Chapter 5 cg 60
Multi-Way Selection Exampleswitch
Use switch when the decision is based on multiple values of a single variable.
Consider this example:
“A value x is classified as
x = 1 residential
x = 2 commercial
x = 3 non-profit
otherwise unclassified
Code a program to output the classification for a given value of x.”
***** SWTJC STEM *****
Chapter 5 cg 60
Multi-Way Selection Flowchartswitch
x=1?true
false
Entry Point
Exit Point
“residential” x=2?
false
x=3?
false
“commercial”
“nonprofit”
“unclassified”
true
true
***** SWTJC STEM *****
Chapter 5 cg 60
Multi-way Selection Code switch
int x =2; switch (x) { case 1: System.out.println("residential"); break; // or return a value case 2: System.out.println("commercial"); break; // or return a value case 3: System.out.println("non-profit"); break; // or return a value default: System.out.println("unclassified"); // or return a value } . . . Continue Here
MultiWaySwitch.java
• Given a value, the MultiWayCalculator applies two factors to calculate a final value.
• The two factors are as follows:1. The type factor is based on a the value type
Type Type Factor0 1.51 2.03 2.5
2. The range factor is based on ranges of valueRange Range Factor x < 200 0.25200 x < 500 0.375500 x < 800 0.55800 x 0.85
3. The final value is equal to value * type factor * range factor
***** SWTJC STEM *****
Chapter 5 cg 54
MultiWayCalculator Example
***** SWTJC STEM *****
Chapter 5 cg 60
MultiWayCalculator (UML)
MultiWayCaculator ----------------------------------------------------value: doubletype: double----------------------------------------------------setValue (double value): voidgetValue ( ): doublesetType (int value): voidgetType ( ): int-getTypeFactor ( ): double-getRangeFactor ( ): doublegetFinalValue: double)toString( ): String
public class MultiWayCalculator {
private double value; private int type;
public MultiWayCalculator(double value, int type) { this.value = value; this.type = type; }
***** SWTJC STEM *****
Chapter 5 cg 54
Declarations and Constructor
MultiWayCalculator.java
public void setValue(double value) { this.value = value; } public double getValue() { return value; }
public void setType(int type) { this.type = type; } public int getType() { return type; }
***** SWTJC STEM *****
Chapter 5 cg 54
Attribute Setters and Getters
MultiWayCalculator.java
private double getTypeFactor () { switch (type) { case 0: return 1.5; case 1: return 2.0; case 2: return 2.5; default: return 0.0; } }
***** SWTJC STEM *****
Chapter 5 cg 54
Private Getter for Type Factor
MultiWayCalculator.java
private double getRangeFactor () { if (value < 200.) return 0.25; else if (value < 500.) return 0.375; else if (value < 800.) return 0.55; else return 0.85; }
***** SWTJC STEM *****
Chapter 5 cg 54
Private Getter for Range Factor
MultiWayCalculator.java
public double getFinalValue() { return value * getTypeFactor () * getRangeFactor
(); }
public String toString() { return "Value = " + value + "\n" + "Type = " + type + "\n" + "Type Factor = " + getTypeFactor () + "\n" + "Range Factor = " + getRangeFactor () + "\n" + "Final Value = " + getFinalValue () + "\n"; }} // Class block end
***** SWTJC STEM *****
Chapter 5 cg 54
Calculated Getter and toString
MultiWayCalculator.java
• Logical expressions combine relational expressions using logical operators. The result is Boolean!
• Logical operators are NOT, OR, and AND.
• Conditional expression takes the form:
rel-expr1 logical-operator rel-expr2
(x > y +3) OR (y < 5)
• Examples:Assume x is 2 and y is 4:x < 3 AND y > 2 (result is TRUE because both are TRUE)x < 1 OR y > 2 (result is TRUE because one is TRUE)NOT(x < 4) (result is FALSE because x < 4 is TRUE)
• A truth table is used to define logical operator results.
***** SWTJC STEM *****
Chapter 5 cg 54
Conditional Expressions
***** SWTJC STEM *****
Chapter 5 cg 54
Truth Table NOT
A !A
false true
true false
Truth table for NOT (Java symbol ! )
***** SWTJC STEM *****
Chapter 5 cg 54
Truth Table OR
Truth table for OR (Java symbol || )
A B A || B
false false false
true false true
false true true
true true true
***** SWTJC STEM *****
Chapter 5 cg 54
Truth Table AND
Truth table for AND (Java symbol &&)
A B A && B
false false false
true false false
false true false
true true true
***** SWTJC STEM *****
Chapter 5 cg 54
Conditional Expression Example
Consider this example (as before):
“A value x is classified as
x < 200 low
200 x < 500 medium
500 x < 800 medium-high
800 x high
Code a program to output the classification for a given value of x.”
***** SWTJC STEM *****
Chapter 5 cg 54
Conditional Expression Flowchart
x<200?true
false
Entry Point
Exit Point
“low” x200 AND x<500?
false
x>=500 AND x<800?
false
“medium”
“medium-high”
“high”
true
true
***** SWTJC STEM *****
Chapter 5 cg 54
Conditional Expression Code
int x = 450;
if (x < 200) // x < 200
System.out.println("low");
if (x >= 200 && x < 500) // 200 ≤ x < 500
System.out.println("medium");
if (x >= 500 && x < 800) // 500 ≤ x < 800
System.out.println("medium-high");
if (x >= 800) // x ≥ 800
System.out.println("high");
LogicalExpressionAnd.java
***** SWTJC STEM *****
Chapter 5 cg 54
Conditional Expression Example
Consider another example:
“A value x is classified as
x < 200 or x > 400 out of range
otherwise within range
Code a program to output the classification for a given value of x.”
***** SWTJC STEM *****
Chapter 5 cg 54
Conditional Expression Flowchart
Entry Point
Exit Point
x<200 OR x>400?
false
“out of range”“within range”
true
***** SWTJC STEM *****
Chapter 5 cg 54
Conditional Expression Code
int x = 350;
if (x < 200 || x > 400) // x < 200 OR x > 400
System.out.println(“out of range");
else
System.out.println(“within range");
LogicalExpressionOr.java
***** SWTJC STEM *****
Chapter 5 cg 54
Conditional Expression Example
Consider another example:
“If the driver’s seat belt is not fastened and the ignition switch is turned on, sound warning tone. Otherwise, do nothing.”
Assume
(1) “ig” is a variable set by the ignition switch that is “true” only if the key is in the ignition and it is turned on.
(2) “sb” is a variable set by a seatbelt switch that is “true” only if the seat belt of the driver is latched.
***** SWTJC STEM *****
Chapter 5 cg 54
Conditional Expression Flowchart
Entry Point
Exit Point
ig AND NOT(sb)
false
“beep, beep”
true
***** SWTJC STEM *****
Chapter 5 cg 54
Conditional Expression Code
boolean ignitionSwitch = true; boolean seatBelt = false;
if (ignitionSwitch && !(seatBelt)) // ig AND NOT (sb)
System.out.println(“beep, beep");
LogicalExpressionSeatBelt.java
***** SWTJC STEM *****
Chapter 5 cg 54
Logical Expression Summary
Use OR for “out of range” condition: x ≤ a or b ≤ x
becomes
if ( x <= a || x >= b) .....
Use AND for “in range” condition: a ≤ x ≤ b
becomes
if ( x ≥ a && x ≤ b) .....
-x x a b
-x x a b