×

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.


Related Topics

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Java.net.SocketException

Exception The problem occurred during the execution of the program. If an exception occurs in the program, the program gets terminated. To skip the exception occurring statements, we have to handle...

4 minutes read.

Java Strictfp Keyword

Strictfp is used to impose limits on floating-point calculation. It ensures that we will get the same result on every platform while performing an operation with the floating-point variable. The floating-point calculation is platform-dependent due...

1 minute read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Sleeping Barber Problem in Java

The barbershop in this issue has one barber, one barber chair, and N chairs for customers in the waiting area. We may demonstrate the issue by keeping with the original...

6 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Second Smallest Number in an Array in Java

By sorting the arrays and returning the second element, we can use Java to discover the second-smallest number in the array. Input:  arr[] = {10, 11, 13, 15, 34, 51} Output: The...

6 minutes read.

Java StringBuffer vs StringBuilder

Java StringBuffer vs StringBuilder StringBuffer The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences. Java StringBuffer program FileName: StringBufferExample.java // A basic program that demonstrates the working...

2 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

String to JSON in Java

Nowadays, receiving data in JSON String format rather than XML is quite frequent. Java does not transform JSON String to JSON Object when dealing with JSON String. However, using the...

3 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

How to Create a Generic List in Java?

Generics are types that have parameters. The goal is to make it possible for methods, classes, and interfaces to take type (Integer, String, etc., and user-defined types) as a parameter....

4 minutes read.