×

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

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that...

6 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Java BLOB

The two data types used in Java to store binary and big-character objects are BLOB and CLOB. In contrast to other types of data like float, int, double, etc., it...

4 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Creating a Custom Generic Class in Java

To indicate parameter types when creating generic classes, we utilize the <> symbol. The syntax used to generate objects of a generic class is as follows. // To create an instance...

4 minutes read.

Class vs Object in Java

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

7 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

How to Convert int to long in Java

How to Convert int to long in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.