/******************************* MAIN HEADER ********************************** Title: Days in Month Calculator (version 2) Author: James D. Skrentny, skrentny@cs.wisc.edu copyright 2009-2012 all rights reserved Course: CS302, Lectures 1 & 2 **************************** 80 columns wide *********************************/ import java.util.Scanner; /** * This code sample demonstrates good and bad style with switch statements. * * Bugs: None known **/ public class DaysInMonthCalculator2 { public static void main(String[] args) { //Use input to make our program GENERAL. Scanner stdIn = new Scanner(System.in); int month; //Use clear prompts to make our program USER-FRIENDLY. System.out.print("Please enter a month [1-12]: "); month = stdIn.nextInt(); // PASTE EXAMPLES HERE } } /* //EXAMPLE 1A: //*********** //Doesn't WORK and very poor STRUCTURE. //-->Can you determine why? switch (month) { case 1: System.out.println(month + " is a 31 day month."); case 2: System.out.println(month + " is usually a 28 day month."); case 3: System.out.println(month + " is a 31 day month."); case 4: System.out.println(month + " is a 30 day month."); case 5: System.out.println(month + " is a 31 day month."); case 6: System.out.println(month + " is a 30 day month."); case 7: System.out.println(month + " is a 31 day month."); case 8: System.out.println(month + " is a 31 day month."); case 9: System.out.println(month + " is a 30 day month."); case 10: System.out.println(month + " is a 31 day month."); case 11: System.out.println(month + " is a 30 day month."); case 12: System.out.println(month + " is a 31 day month."); } //end switch //EXAMPLE 1B: //*********** //WORKS BUT still very poor STRUCTURE. switch (month) { case 1: System.out.println(month + " is a 31 day month."); break; case 2: System.out.println(month + " is usually a 28 day month."); break; case 3: System.out.println(month + " is a 31 day month."); break; case 4: System.out.println(month + " is a 30 day month."); break; case 5: System.out.println(month + " is a 31 day month."); break; case 6: System.out.println(month + " is a 30 day month."); break; case 7: System.out.println(month + " is a 31 day month."); break; case 8: System.out.println(month + " is a 31 day month."); break; case 9: System.out.println(month + " is a 30 day month."); break; case 10: System.out.println(month + " is a 31 day month."); break; case 11: System.out.println(month + " is a 30 day month."); break; case 12: System.out.println(month + " is a 31 day month."); break; } //end switch //EXAMPLE 2A: //*********** //Doesn't WORK! Invalid way to combine cases! switch (month) { case 1,3,5,7,8,10,12: System.out.println(month + " is a 31 day month."); case 4,6,9,11: System.out.println(month + " is a 30 day month."); case 2: System.out.println(month + " is usually a 28 day month."); default: System.out.println(month + " is invalid."); } //end switch //EXAMPLE 2B: //*********** //Doesn't WORK! Another invalid way to combine cases! switch (month) { case 1 || 3 || 5 || 7 || 8 || 10 || 12: System.out.println(month + " is a 31 day month."); case 4 || 6 || 9 || 11: System.out.println(month + " is a 30 day month."); case 2: System.out.println(month + " is usually a 28 day month."); default: System.out.println(month + " is invalid."); } //end switch //YOU ALSO CANNOT USE relational operators such as < > <= >= //as in this unrelated but code example: switch (month) { case >= 1 && <= 6: System.out.println(month + " is in the first half of the year."); case >= 7 && <=12: System.out.println(month + " is in the last half of the year."); default: System.out.println(month + " is invalid."); } //end switch //EXAMPLE 3A: //*********** //WORKS but can still be improved! //-->Can you determine how? switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println(month + " is a 31 day month."); break; case 4: case 6: case 9: case 11: System.out.println(month + " is a 30 day month."); break; case 2: System.out.println(month + " is usually a 28 day month."); break; } //end switch // Note: putting break on last case is good style //EXAMPLE 3B: //*********** //Improvement: Make code more ROBUST by adding invalid month case. default: System.out.println(month + " is invalid."); break; //EXAMPLE 4: //*********** //WORKS and more compact but not as readable as above example switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println(month + " is a 31 day month."); break; case 4: case 6: case 9: case 11: System.out.println(month + " is a 30 day month."); break; case 2: System.out.println(month + " is usually a 28 day month."); break; default: System.out.println(month + " is invalid."); break; } //end switch */