×

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 Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years to the local or current date in Java language. We will begin our topic with basic concepts and would...

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

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Model Class in Java

In this section, we will be acknowledged about the model class in Java, its purpose and its uses. Also, we will learn how is this created and leveraged in java. Model...

4 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

JIT in Java

What is JIT ? JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation...

5 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

Java Pass-by-Reference

In Java, passing parameters can be done using one of two fundamental methods. Pass-by-value is used for the first and pass-by-reference is used for the second. One thing to keep...

2 minutes read.

Types of Garbage Collector in Java

Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We...

3 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

Recursion Program in Java

The recursion program in Java demonstrates the usage of recursion. The process by which a function/ method calls itself, again and again, is called recursion. Each recursive call is pushed...

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

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.