Menu Driven Program in Java
Menu Driven Program in Java
The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed menu. The output is given by the program based on the option selected by the user. This type of program interacts a lot with the user, and hence, it is user-friendly.
The following menu-driven Java program calculates the area of the various geometrical figures, using Java switch-case, on the basis of choice given by the user.
Area of different Geometrical Shapes
Filename: MenuDrivenExample.java
// importing the class Scanner import java.util.Scanner; public class MenuDrivenExample { public static void main(String argvs[]) { int ch; // for storing user's choice double ar; // for storing area // variable for storing various dimensions of // different geometrical figure int length, breadth, height, diagonal1, diagonal2; int side; int radius; Scanner sc = new Scanner(System.in); // creating object of Scanner class // displaying the menu System.out.println("1: Right Angle Triangle"); System.out.println("2: Area of Equilateral Triangle"); System.out.println("3: Area of Square"); System.out.println("4: Area of Rectangle"); System.out.println("5: Area of Circle"); System.out.println("6: Area of Rhombus"); System.out.println("7: Program termination"); lp : while(true) // labeling the while loop { System.out.print("Make your choice: "); ch = sc.nextInt(); // reading user's choice switch (ch) { case 1: // for Right Angled Triangle System.out.print("Enter the height of Right Angle Triangle \n"); height = sc.nextInt(); System.out.print("Enter the base of Right Angle Triangle \n"); breadth = sc.nextInt(); ar = (height * breadth)/2; System.out.println("Area of the Right Angle Triangle is " + ar + "\n\n"); break; case 2: System.out.print("Enter the side of an Equilateral Triangle \n"); side = sc.nextInt(); ar=(side * side * Math.sqrt(3)) / 4; System.out.println("Area of the Equilateral Triangle is " + ar + "\n\n"); break; case 3: System.out.print("Enter the side of a Square \n"); side = sc.nextInt(); ar = side * side; System.out.println("Area of the Square is "+ ar + "\n\n"); break; case 4: System.out.print("Enter the length of a Rectangle \n"); length = sc.nextInt(); System.out.print("Enter the breadth of the Rectangle \n"); breadth = sc.nextInt(); ar = length * breadth; System.out.println("Area of the Rectangle is " + ar + "\n\n"); break; case 5: System.out.print("Enter the radius of a Circle \n"); radius = sc.nextInt(); ar = radius * radius * 22 / 7; System.out.println("Area of the Circle is " + ar + "\n\n"); break; case 6: System.out.print("Enter the first diagonal of a Rhombus \n"); diagonal1 = sc.nextInt(); System.out.print("Enter the second diagonal of the Rhombus \n"); diagonal2 = sc.nextInt(); ar = diagonal1 * diagonal2 * 1 / 2; System.out.println("Area of the Rhombus is " + ar + "\n\n"); break; case 7: // exiting from the switch-case as well as from the while loop using its label lp break lp; default: System.out.println("Invalid choice! Please make a valid choice. \n\n"); } } } }
Output:
1: Right Angle Triangle 2: Area of Equilateral Triangle 3: Area of Square 4: Area of Rectangle 5: Area of Circle 6: Area of Rhombus 7: Program termination
Output 1:
Make your choice: 1 Enter the height of Right Angle Triangle 4 Enter the base of Right Angle Triangle 5 Area of the Right Angle Triangle is 10.0
Output 2:
Make your choice: 2 Enter the side of an Equilateral Triangle 14 Area of the Equilateral Triangle is 84.87048957087498
Output 3:
Make your choice: 3 Enter the side of a Square 5 Area of the Square is 25.0
Explanation: In the above program, we have used a Java while loop whose condition statement always evaluates to true. In the loop, we are displaying the menu and reading the choice entered by the user. As per the user’s choice, we are calculating the area of the geometric shapes on the basis of its dimensions (which is also entered by the user). Since we are using the while loop whose condition always evaluates to true hence, labelling of the loop is done to force the loop termination once the user enters the number 7. In the 7th case block, we are using the break statement to exit not only from the switch-case block but also from the while loop using its loop label lp.
Note: To reduce the complexity of the above program, it is assumed that the user will only enter numeric values. In order to give liberty to the user to enter anything as per his/her choice, it is advisable to use try-catch blocks so that an appropriate message is displayed when the program receives any absurd input from the user.