×

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 String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Array Slicing in Java

Array slicing is a method in Java for obtaining a subarray of a specified array. Assume a[] is an array. It contains eight items indexed from a[0] to a[7].a[] =...

3 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.

If Condition in Lambda Expression Java

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single-method interface using an expression....

4 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Balanced Parentheses in Java

One of the frequent programming issues, commonly referred to as the Balanced brackets issue, is the problem with the balanced parenthesis. Interviewers frequently provide this task, in which we must...

5 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.