Understanding Menu-Driven Programs in Java: Multi-Way Selection and Loops
This guide covers the fundamentals of menu-driven programs in Java. It explores multi-way selection using `switch` statements, the `do...while` loop for post-condition iterations, and the structure of menu programs. An example demonstrates how to manage user input for group timings, allowing options for quitting. Additionally, it includes an alarm function using a switch statement to set or turn off alarms. Examples of control flow and interactions are provided to enhance understanding of Java program structure and logic.
Understanding Menu-Driven Programs in Java: Multi-Way Selection and Loops
E N D
Presentation Transcript
Intro to Java Week 10 Menu-driven programs
Contents • Multi-way selection • Loop with post-condition • Menu program • Sub-procedures • Alarm function
The ‘switch’ statement • A switch statement may be used when • only one variable is being checked in each condition • the check involves specific values of that variable (e.g. 'A', 'B') and not ranges (e.g. >39 ) ;
The ‘switch’statement: an example char group; System.out.println("Enter your group (A,B,C)"); group = EasyIn.getChar(); switch(group) { case 'A': System.out.print("10.00 a.m "); break; case 'B': System.out.print("1.00 p.m "); break; case 'C': System.out.print("11.00 a.m "); break; default: System.out.print("No such group"); }
The ‘do…while’ loop • The do…while loop has its while condition at the end of the loop rather than at the beginning. If the while condition is at the end of the loop, the loop will iterate at least once. do { // instruction(s) to be repeated go here }while ( /* test goes here */ );
The ‘do...while’ loop: an example char response; do { // program instructions go here System.out.println("another go (y/n)?"); response = EasyIn.getChar(); } while (response == 'y');
An example of a menu driven program char response; System.out.println("***Lab Times***"); do { System.out.println(); System.out.println("[1] TIME FOR GROUP A"); System.out.println("[2] TIME FOR GROUP B"); System.out.println("[3] TIME FOR GROUP C"); System.out.println("[4] QUIT PROGRAM"); System.out.print("enter choice [1,2,3,4]: "); response = EasyIn.getChar(); System.out.println(); switch(response) { case '1': System.out.println("10.00 a.m ");break; case '2': System.out.println("1.00 p.m ");break; case '3': System.out.println("11.00 a.m ");break; case '4': System.out.println("Goodbye ");break; default: System.out.println("Options 1-4 only!"); } } while (response != '4');
Interacting with a menu driven program [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: 5 Options 1-4 only! [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: 1 10.00 a.m [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: 4 Goodbye
Alarm function switch • switch (option) { case 'a': setAlarm(theAlarm); break; case 'b': theAlarm.switchOff(); break; case 'c': System.out.println (“Exit” ); break; default: System.out.println("No such option");} // end switch
Alarm function menu loop do { option = offerMenu(); // switch control as above} while ( option != 'c' ); // ‘c’ is the exit option
Alarm sub-procedure setAlarm private static void setAlarm() { System.out.print("Set alarm time (int): "); int intTime = EasyIn.getInt(); theAlarm.setAlarmTime(intTime); theAlarm.switchOn(); } // end setAlarm
OfferMenu() worker method private static char offerMenu() { System.out.println("\nEnter a.Set Alarm");System.out.println("Enter b.Switch Off");System.out.println("Enter c.Exit"); return EasyIn.getChar(); } // end offerMenu