290 likes | 733 Views
***** SWTJC STEM *****. Selection Construct Choices. One-way selection - Use simple if . Two-way selection - Use if - else . if blocks Nested if blocks Multi-way selection - Use either: Differing conditions - Use if - else if - else if - .... - else .
E N D
***** SWTJC STEM ***** Selection Construct Choices • One-way selection - Use simple if. • Two-way selection - Use if - else. • if blocks • Nested if blocks • Multi-way selection - Use either: • Differing conditions - Useif - else if - else if - .... - else. • Same variable or expression with multiple choices - Useswitch. Chapter 5 cg 57
***** SWTJC STEM ***** Multi-WaySelection Exampleelse if • Useelse 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.” Chapter 5 cg 57
***** SWTJC STEM ***** Entry Point Multi-Way Selection Flowchartelse if true x<200? false true “low” x<500? false true “medium” x<800? false “medium-high” “high” Exit Point Chapter 5 cg 57
***** SWTJC STEM ***** MultiWayElseIf.java Multi-way Selection Codeelse 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"); Chapter 5 cg 57
***** SWTJC STEM ***** Multi-Way Selection Exampleswitch • Useswitch 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.” Chapter 5 cg 60
***** SWTJC STEM ***** Entry Point Multi-Way Selection Flowchartswitch true x=1? false true “residential” x=2? false true “commercial” x=3? false “nonprofit” “unclassified” Exit Point Chapter 5 cg 60
***** SWTJC STEM ***** MultiWaySwitch.java Multi-way Selection Codeswitch 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 Chapter 5 cg 60
***** SWTJC STEM ***** • Given a value, the MultiWayCalculator applies two factors to calculate a final value. • The two factors are as follows: • The type factor is based on a the value typeType Type Factor0 1.51 2.03 2.5 • 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 • The final value is equal to value * type factor * range factor MultiWayCalculator Example Chapter 5 cg 54
***** SWTJC STEM ***** MultiWayCaculator ---------------------------------------------------- value: double type: double ---------------------------------------------------- setValue (double value): void getValue( ): double setType (int value): void getType ( ): int -getTypeFactor ( ): double -getRangeFactor ( ): double getFinalValue: double) toString( ): String MultiWayCalculator (UML) Chapter 5 cg 60
***** SWTJC STEM ***** MultiWayCalculator.java • public class MultiWayCalculator { • private double value; • private int type; • public MultiWayCalculator(double value, int type) { • this.value = value; • this.type = type; • } Declarations and Constructor Chapter 5 cg 54
***** SWTJC STEM ***** 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; • } Attribute Setters and Getters Chapter 5 cg 54
***** SWTJC STEM ***** 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; • } • } Private Getter for Type Factor Chapter 5 cg 54
***** SWTJC STEM ***** 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; • } Private Getter for Range Factor Chapter 5 cg 54
***** SWTJC STEM ***** 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 Calculated Getter and toString Chapter 5 cg 54
***** SWTJC STEM ***** • Logical expressionscombine relational expressions using logical operators. The result is Boolean! • Logical operators areNOT, 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. Conditional Expressions Chapter 5 cg 54
A !A false true true false ***** SWTJC STEM ***** Truth table for NOT (Java symbol !) Truth Table NOT Chapter 5 cg 54
A B A || B false false false true false true false true true true true true ***** SWTJC STEM ***** Truth table for OR (Java symbol ||) Truth Table OR Chapter 5 cg 54
A B A && B false false false true false false false true false true true true ***** SWTJC STEM ***** Truth table for AND (Java symbol &&) Truth Table AND Chapter 5 cg 54
***** SWTJC STEM ***** 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.” Chapter 5 cg 54
***** SWTJC STEM ***** Entry Point Conditional Expression Flowchart true x<200? false true “low” x200 AND x<500? false “medium” true x>=500 AND x<800? false “medium-high” “high” Exit Point Chapter 5 cg 54
***** SWTJC STEM ***** LogicalExpressionAnd.java 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"); Chapter 5 cg 54
***** SWTJC STEM ***** 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.” Chapter 5 cg 54
***** SWTJC STEM ***** Conditional Expression Flowchart Entry Point true x<200 OR x>400? false “out of range” “within range” Exit Point Chapter 5 cg 54
***** SWTJC STEM ***** Conditional Expression Code LogicalExpressionOr.java 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"); Chapter 5 cg 54
***** SWTJC STEM ***** 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 • “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. • “sb” is a variable set by a seatbelt switch that is “true” only if the seat belt of the driver is latched. Chapter 5 cg 54
***** SWTJC STEM ***** Conditional Expression Flowchart Entry Point true ig AND NOT(sb) false “beep, beep” Exit Point Chapter 5 cg 54
***** SWTJC STEM ***** LogicalExpressionSeatBelt.java Conditional Expression Code boolean ignitionSwitch = true; boolean seatBelt = false; if (ignitionSwitch && !(seatBelt)) // ig AND NOT (sb) System.out.println(“beep, beep"); Chapter 5 cg 54
***** SWTJC STEM ***** 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 Chapter 5 cg 54