In this tutorial, we will write a program on menu driven in java using switch statement along with source code. The switch case is mostly used when it is necessary to give options to the users. If you do not know the working process of the switch case statement then click the link below.
Java Menu-driven Program using switch case:
This is a menu program in java that displays a menu and takes the input from the user. The choice made by the user is among the option displayed in the menu. And accordingly, the output is displayed on the screen. It is a user-friendly program in java.
Menu Driven Program in Java.
The below menu-driven Java program calculates the area of different geometrical figures.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | //area of different geometric figures using switch case in java import java.util.Scanner; public class MenuDriven { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("To find the areas of following press:"); System.out.println("1 for the Area of a Circle"); System.out.println("2 for the Area of a Square"); System.out.println("3 for the Area of a Right Angled Triangle"); System.out.println("4 for the Area of a Rectangle"); System.out.println("5: Area of Rhombus"); System.out.print("\nEnter your Choice: "); int ch = sc.nextInt(); switch (ch) { case 1: System.out.println("Enter radius for the circle:"); float r = sc.nextFloat(); float area = 3.14f *r * r; System.out.println("Area:" + area); break; case 2: System.out.println("Enter side for square:"); int s = sc.nextInt(); int ae = s * s; System.out.println("Area:" + ae); break; case 3: System.out.println("Enter height and base for traingle:"); float h = sc.nextFloat(); float bs = sc.nextFloat(); float ar = 0.5f *h * bs; System.out.println("Area:" + ar); break; case 4: System.out.println("Enter length and breadth for rectangle:"); int l = sc.nextInt(); int b = sc.nextInt(); int aa = l * b; System.out.println("Area:" + aa); break; case 5: System.out.print("Enter the first diagonal of a Rhombus: "); float diagonal1 = sc.nextInt(); System.out.print("Enter the second diagonal of the Rhombus: "); float diagonal2 = sc.nextInt(); float aRhombus = (diagonal1 *diagonal2) / 2; System.out.println("Area of the Rhombus is: " + aRhombus); break; default: System.out.println("Invalid Input"); break; } } } |
Output 1:
1 2 3 4 5 6 7 8 9 10 11 | To find the areas of following press: 1 for the Area of a Circle 2 for the Area of a Square 3 for the Area of a Right Angled Triangle 4 for the Area of a Rectangle 5: Area of Rhombus Enter your Choice: 2 Enter side for square: 4 Area:16 |
Output 2:
1 2 3 4 5 6 7 8 9 10 11 12 | To find the areas of following press: 1 for the Area of a Circle 2 for the Area of a Square 3 for the Area of a Right Angled Triangle 4 for the Area of a Rectangle 5: Area of Rhombus Enter your Choice: 3 Enter height and base for traingle: 3 2 Area:3.0 |
Output 3:
1 2 3 4 5 6 7 8 9 10 11 | To find the areas of following press: 1 for the Area of a Circle 2 for the Area of a Square 3 for the Area of a Right Angled Triangle 4 for the Area of a Rectangle 5: Area of Rhombus Enter your Choice: 5 Enter the first diagonal of a Rhombus: 4 Enter the second diagonal of the Rhombus: 3 Area of the Rhombus is: 6.0 |
Using this logic you can create various other menu programs in java.