EXAMPLE OF MENU DRIVEN PROGRAM IN JAVA
-----------------------------------------------------------------------
//To implement Menu driven Program
import java.util.*; // Include Java Packages
public class menu // Declare the class
{
public static void main(String args[]) //Main method
{
Scanner ob=new Scanner(System.in);
int a,b,c; //Declare the variables
System.out.println(" MAIN MENU "); // Main menu
System.out.println("\t ------------------------------------------------------------");
System.out.println("\t 1.TO ADD 2 NUMBERS AND DISPLAY THE RESULT");
System.out.println("\t 2.TO MULTIPLY 2 NUMBERS AND DISPLAY THE RESULT");
System.out.println("\t 3.TO CALCULATE AVERAGE OF 2 NUMBERS AND DISPLAY THE RESULT");
System.out.println("\t 4.EXIT");
System.out.println("\t ENTER YOUR CHOICE"); //input your choice
int ch=ob.nextInt();
switch(ch)
{
case 1: //To add 2 numbers
System.out.println("\n\n");
System.out.println("\t Enter the numbers");
a=ob.nextInt();
b=ob.nextInt();
c=a+b;
System.out.println("\n\n");
System.out.println("\t SUM IS:"+c); //Display the answer
break;
case 2:
System.out.println("\n\n");
System.out.println("\t Enter the numbers"); //To Multiply 2 numbers
a=ob.nextInt();
b=ob.nextInt();
c=a*b;
System.out.println("\n\n");
System.out.println("\t PRODUCT IS:"+c); //Display the answer
break;
case 3:
System.out.println("\n\n");
System.out.println("\t Enter the numbers"); //To divide 2 numbers
a=ob.nextInt();
b=ob.nextInt();
int k1=0;
int k2=0;
if(a==b)
{
k1=1;
k2=0;
}
if(a>b)
{
k1=a/b;
k2=a%b;
}
else
{
k1=b/a;
k2=b%a;
}
System.out.println("\n\n");
System.out.println("\t QUOTIENT IS:"+k1); //Display the answers
System.out.println("\t REMAINDER IS:"+k2);
case 4:
System.exit(1);
default :
System.out.println("\t INVALID CHOICE"); //Invalid case
} //End of Switch
} //End of main
} //End of class
-----------------------------------------------------------------------
//To implement Menu driven Program
import java.util.*; // Include Java Packages
public class menu // Declare the class
{
public static void main(String args[]) //Main method
{
Scanner ob=new Scanner(System.in);
int a,b,c; //Declare the variables
System.out.println(" MAIN MENU "); // Main menu
System.out.println("\t ------------------------------------------------------------");
System.out.println("\t 1.TO ADD 2 NUMBERS AND DISPLAY THE RESULT");
System.out.println("\t 2.TO MULTIPLY 2 NUMBERS AND DISPLAY THE RESULT");
System.out.println("\t 3.TO CALCULATE AVERAGE OF 2 NUMBERS AND DISPLAY THE RESULT");
System.out.println("\t 4.EXIT");
System.out.println("\t ENTER YOUR CHOICE"); //input your choice
int ch=ob.nextInt();
switch(ch)
{
case 1: //To add 2 numbers
System.out.println("\n\n");
System.out.println("\t Enter the numbers");
a=ob.nextInt();
b=ob.nextInt();
c=a+b;
System.out.println("\n\n");
System.out.println("\t SUM IS:"+c); //Display the answer
break;
case 2:
System.out.println("\n\n");
System.out.println("\t Enter the numbers"); //To Multiply 2 numbers
a=ob.nextInt();
b=ob.nextInt();
c=a*b;
System.out.println("\n\n");
System.out.println("\t PRODUCT IS:"+c); //Display the answer
break;
case 3:
System.out.println("\n\n");
System.out.println("\t Enter the numbers"); //To divide 2 numbers
a=ob.nextInt();
b=ob.nextInt();
int k1=0;
int k2=0;
if(a==b)
{
k1=1;
k2=0;
}
if(a>b)
{
k1=a/b;
k2=a%b;
}
else
{
k1=b/a;
k2=b%a;
}
System.out.println("\n\n");
System.out.println("\t QUOTIENT IS:"+k1); //Display the answers
System.out.println("\t REMAINDER IS:"+k2);
case 4:
System.exit(1);
default :
System.out.println("\t INVALID CHOICE"); //Invalid case
} //End of Switch
} //End of main
} //End of class