Menu Driven programs in Java

LakshmiSarvani1 471 views 11 slides Jan 08, 2021
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Menu Driven programs in Java, infinte loop, do while loop, calculator program in java


Slide Content

Intro to Java
Session 17,18
Menu-driven programs

Contents
•Multi-way selection
•Infinite loop
•Loop with post-condition
•Menu program

The ‘switch’ statement
Aswitchstatementmaybeusedwhen
 onlyonevariableisbeingcheckedineachcondition
 thecheckinvolvesspecificvaluesofthatvariable
(e.g.'A','B')andnotranges(e.g.>39);

The ‘switch’statement: an example
chargroup;
System.out.println("Enteryourgroup(A,B,C)");
group=EasyIn.getChar();
switch(group)
{
case'A':System.out.print("10.00a.m");
break;
case'B':System.out.print("1.00p.m");
break;
case'C':System.out.print("11.00a.m");
break;
default:System.out.print("Nosuchgroup");
}

Infinite loop
•An infinte loop is a loop that will execute indefinitely because
the loop's expression is always true
Syntax:
while(true)
{
….
statements to be repeated;
break;
}
•Make sure you there is break; statement of System.exit(0);
inside the body of the loop.
•These statements will let you come out of the loop. Otherwise,
the loop will execute indefinitely.

import java.util.Scanner;
public class Menu
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("::Welcome to Menu driven code::");
while(true)
{
System.out.println("To execute case 1, Enter value 1");
System.out.println("To execute case 2, Enter value 2");
System.out.println("To Exit, Enter value 9");
System.out.println("Enter your choice::");
int choice = scan.nextInt();
switch(choice)
{
case 1: System.out.println("I am case 1");
break;
case 2: System.out.println("I am case 2");
break;
case 9: System.out.println("Exiting the application");
System.exit(0);
default: System.out.println("Incorrect input!!! Please re-enter");
}
}
}
}
Template for writing any
menu driven program using infinite loops:

import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
while(true)
{
System.out.print("Enter two numbers: ");
double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.print(“Enter (+, -, *, /): or # to exit ");
char operator = reader.next().charAt(0);
double result;
switch (operator)
{
case '+':
result = first + second;
break;
case '-':
result = first -second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
case '#':
System.exit(0);
// operator doesn't match any case constant (+, -, *,
/)
default:
System.out.printf("operator is not correct");
return;
}
System.out.println(first + " " + operator + " " +
second + " = " + result);
}
}
}
Calculator Program

The‘do…while’loop
Thedo…whileloophasitswhileconditionatthe
endoftheloopratherthanatthebeginning.
If the while condition is at the end of the loop, the loop
will iterate at least once.
do
{
//instruction(s)toberepeatedgohere
}while(/*testgoeshere*/);

The ‘do...while’ loop: an example
charresponse;
do
{
//programinstructionsgohere
System.out.println("another go(y/n)?");
response=EasyIn.getChar();
}while(response=='y');

charresponse;
System.out.println("***Lab Times***");
do
{
System.out.println();
System.out.println("[1]TIMEFORGROUPA");
System.out.println("[2]TIMEFORGROUPB");
System.out.println("[3]TIMEFORGROUPC");
System.out.println("[4]QUITPROGRAM");
System.out.print("enterchoice[1,2,3,4]:");
response=EasyIn.getChar();
System.out.println();
switch(response)
{
case'1':System.out.println("10.00a.m");break;
case'2':System.out.println("1.00p.m");break;
case'3':System.out.println("11.00a.m");break;
case'4':System.out.println("Goodbye ");break;
default:System.out.println("Options 1-4only!");
}
}while(response!='4');
An example of a menu driven program using do while loop

Interacting
with a menu
driven
program
[1]TIMEFORGROUPA
[2]TIMEFORGROUPB
[3]TIMEFORGROUPC
[4]QUITPROGRAM
enterchoice[1,2,3,4]:5
Options1-4only!
[1]TIMEFORGROUPA
[2]TIMEFORGROUPB
[3]TIMEFORGROUPC
[4]QUITPROGRAM
enterchoice[1,2,3,4]:1
10.00a.m
[1]TIMEFORGROUPA
[2]TIMEFORGROUPB
[3]TIMEFORGROUPC
[4]QUITPROGRAM
enterchoice[1,2,3,4]:4
Goodbye