Presentation is loading. Please wait.

Presentation is loading. Please wait.

***** 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.

Similar presentations


Presentation on theme: "***** 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."— Presentation transcript:

1 ***** 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.

2 ***** SWTJC STEM ***** Chapter 5 cg 57 Multi-Way Selection Example else if Use else if construct for multiple conditions. Consider this example: “A value x is classified as x < 200low 200  x < 500medium 500  x < 800medium-high 800  xhigh Program classification output for a given value of x.”

3 ***** SWTJC STEM ***** Chapter 5 cg 57 Multi-Way Selection Flowchart else if x<200? true false Entry Point Exit Point “low” x<500? false x<800? false “medium” “medium-high” “high” true

4 ***** 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

5 ***** SWTJC STEM ***** Chapter 5 cg 60 Multi-Way Selection Example switch Use switch when the decision is based on multiple values of a single variable. Consider this example: “A value x is classified as x = 1residential x = 2commercial x = 3non-profit otherwiseunclassified Code a program to output the classification for a given value of x.”

6 ***** SWTJC STEM ***** Chapter 5 cg 60 Multi-Way Selection Flowchart switch x=1? true false Entry Point Exit Point “residential” x=2? false x=3? false “commercial” “nonprofit” “unclassified” true

7 ***** 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

8 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 TypeType Factor 01.5 12.0 32.5 2.The range factor is based on ranges of value Range Range Factor x < 2000.25 200  x < 5000.375 500  x < 8000.55 800  x0.85 3.The final value is equal to value * type factor * range factor ***** SWTJC STEM ***** Chapter 5 cg 54 MultiWayCalculator Example

9 ***** SWTJC STEM ***** Chapter 5 cg 60 MultiWayCalculator (UML) MultiWayCaculator ---------------------------------------------------- value: double type: double ---------------------------------------------------- setValue (double value): void get Value ( ): double setType (int value): void getType ( ): int -getTypeFactor ( ): double -getRangeFactor ( ): double getFinalValue: double) toString( ): String

10 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

11 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

12 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

13 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

14 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

15 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 2 (result is TRUE because both are TRUE) x 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

16 ***** SWTJC STEM ***** Chapter 5 cg 54 Truth Table NOT A!A falsetrue false Truth table for NOT (Java symbol ! )

17 ***** SWTJC STEM ***** Chapter 5 cg 54 Truth Table OR Truth table for OR (Java symbol || ) ABA || B false truefalsetrue falsetrue

18 ***** SWTJC STEM ***** Chapter 5 cg 54 Truth Table AND Truth table for AND (Java symbol &&) ABA && B false truefalse truefalse true

19 ***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Example Consider this example (as before): “A value x is classified as x < 200low 200  x < 500medium 500 x < 800medium-high 800  xhigh Code a program to output the classification for a given value of x.”

20 ***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Flowchart x<200? true false Entry Point Exit Point “low” x  200 AND x<500? false x>=500 AND x<800? false “medium” “medium-high” “high” true

21 ***** 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

22 ***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Example Consider another example: “A value x is classified as x 400out of range otherwisewithin range Code a program to output the classification for a given value of x.”

23 ***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Flowchart Entry Point Exit Point x 400? false “out of range” “within range” true

24 ***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Code int x = 350; if (x 400) // x 400 System.out.println(“out of range"); else System.out.println(“within range"); LogicalExpressionOr.java

25 ***** 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.

26 ***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Flowchart Entry Point Exit Point ig AND NOT(sb) false “beep, beep” true

27 ***** 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

28 ***** SWTJC STEM ***** Chapter 5 cg 54 Logical Expression Summary Use OR for “out of range” condition: x ≤ a or b ≤ x becomes if ( x = b)..... Use AND for “in range” condition: a ≤ x ≤ b becomes if ( x ≥ a && x ≤ b)..... -x x a b x a b


Download ppt "***** 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."

Similar presentations


Ads by Google