From 6c9d335f05956d05dae5149d70cc03c2198db172 Mon Sep 17 00:00:00 2001 From: Christopher Bohn <bohn@unl.edu> Date: Wed, 23 Sep 2020 22:15:17 -0500 Subject: [PATCH] Added more switch examples Shows switching on integers, strings, and enums. Shows all cases with break, shows intentional fall-through, and shows idiomatic uncommented fall-through for two identical cases. --- .../unl/cse/soft160/switches/IfSwitch.java | 2 + .../cse/soft160/switches/SwitchExamples.java | 90 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/main/java/edu/unl/cse/soft160/switches/SwitchExamples.java diff --git a/src/main/java/edu/unl/cse/soft160/switches/IfSwitch.java b/src/main/java/edu/unl/cse/soft160/switches/IfSwitch.java index b19bda3..e3a90a7 100644 --- a/src/main/java/edu/unl/cse/soft160/switches/IfSwitch.java +++ b/src/main/java/edu/unl/cse/soft160/switches/IfSwitch.java @@ -18,6 +18,7 @@ public class IfSwitch { System.out.println("You entered " + outputString + "."); } + // This version of convertToWord uses a chained conditional private static String convertToWord(int value) { String numberText; if (value == 1) { @@ -33,6 +34,7 @@ public class IfSwitch { } /* + // This version of convertToWord uses a switch statement private static String convertToWord(int value) { String numberText; switch (value) { diff --git a/src/main/java/edu/unl/cse/soft160/switches/SwitchExamples.java b/src/main/java/edu/unl/cse/soft160/switches/SwitchExamples.java new file mode 100644 index 0000000..11d6364 --- /dev/null +++ b/src/main/java/edu/unl/cse/soft160/switches/SwitchExamples.java @@ -0,0 +1,90 @@ +package edu.unl.cse.soft160.switches; + +public class SwitchExamples { + public enum Temperature { + SUMMER, AUTUMN, WINTER, SPRING + } + + // This example shows that switch statements can be used with integers + // This example also shows the typical situation in which each case ends with break + public static String digitToWord(int value) { + String numberWord; + switch (value) { + case 1: + numberWord = "one"; + break; + case 2: + numberWord = "two"; + break; + case 3: + numberWord = "three"; + break; + case 4: + numberWord = "four"; + break; + case 5: + numberWord = "five"; + break; + case 6: + numberWord = "six"; + break; + case 7: + numberWord = "seven"; + break; + case 8: + numberWord = "eight"; + break; + case 9: + numberWord = "nine"; + break; + case 10: + numberWord = "ten"; + break; + default: /* The default code should NOT be used for a normal case */ + numberWord = "an out-of-range value"; + } + return numberWord; + } + + // This example shows that switch statements can be used with strings (in Java) + // This example also shows that you should add a "fall-through" comment when you intentionally omit a break + public static void findVehicleType(String vehicle) { + switch (vehicle) { + case "Silverado": + System.out.println("pickup truck"); + break; + case "Traverse": + System.out.println("SUV"); + break; + case "Corvette": /* The "Corvette" case will output "fast car" */ + System.out.print("fast "); + // fall-through + case "Cruze": + System.out.println("car"); + break; + default: + System.out.println("unrecognized vehicle"); + } + } + + // This example shows that switch statements can be used with enumerated types + // This example also shows the idiom where it's okay not to add a "fall-through" comment + public static String getSeasonalTemperature(Temperature season) { + String seasonalTemperature; + switch (season) { + case SUMMER: + seasonalTemperature = "hot"; + break; + case WINTER: + seasonalTemperature = "cold"; + break; + case SPRING: /* When two cases are identical, this is the idiom */ + case AUTUMN: + seasonalTemperature = "warm"; + break; + default: + seasonalTemperature = "indeterminate"; + } + return seasonalTemperature; + } +} -- GitLab