calista-schneider
Uploaded by
12 SLIDES
263 VIEWS
120LIKES

Understanding Menu-Driven Programs in Java: Multi-Way Selection and Loops

DESCRIPTION

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.

1 / 12

Download Presentation

Understanding Menu-Driven Programs in Java: Multi-Way Selection and Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to Java Week 10 Menu-driven programs

  2. Contents • Multi-way selection • Loop with post-condition • Menu program • Sub-procedures • Alarm function

  3. 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 ) ;

  4. 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"); }

  5. 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 */ );

  6. 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');

  7. 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');

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

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

  10. Alarm function menu loop do {  option = offerMenu();  // switch control as above} while ( option != 'c' ); // ‘c’ is the exit option

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

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

More Related