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 b19bda3305795912990d6cacada794b578a2e971..e3a90a77585f831f00974e72b93e4342b900262a 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 0000000000000000000000000000000000000000..11d6364cb064ca47b9838a9e184c3a985df75785 --- /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; + } +}