12
Intro to Java Week 10 Menu-driven programs

Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

Embed Size (px)

Citation preview

Page 1: Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

Intro to Java

Week 10

Menu-driven programs

Page 2: Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

Contents

• Multi-way selection

• Loop with post-condition

• Menu program

• Sub-procedures

• Alarm function

Page 3: 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 ) ;

Page 4: 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: 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");

}

Page 5: Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

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

Page 6: Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

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

Page 7: Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

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

An example of a menu driven program

Page 8: Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

Interacting

with a menu

driven

program

[1] TIME FOR GROUP A

[2] TIME FOR GROUP B[3] TIME FOR GROUP C[4] QUIT PROGRAMenter 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 PROGRAMenter 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 PROGRAMenter choice [1,2,3,4]: 4

Goodbye

Page 9: Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

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

Page 10: Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

Alarm function menu loop

do {   option = offerMenu();   // switch control as above } while ( option != 'c' );

// ‘c’ is the exit option

Page 11: Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

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

Page 12: Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function

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