Menu Driven Program
Menu-driven program in Java is a type of program in which various options are provided to the user and some functionality is provided based on these options. These programs are used to provide a simple yet effective user interface and deliver the requested functionalities to the user. This article provides an overview of menu-driven programs in Java.
This code is an example of a menu driven program that calculates the area and perimeter of shapes like:
Square
Rectangle
Triangle
Circle
This code is an example of a menu driven program that calculates the area and perimeter of shapes like:
Square
Rectangle
Triangle
Circle
Algorithm
- The driver program contains switch case that lets us choose an operation
- Circle gives area and perimeter of a circle
- Square gives area and perimeter of a square
- Rectangle gives area and perimeter of a rectangle
- Triangle gives area and perimeter of a triangle
Code
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("1. Area of Square.");
System.out.println("2. Area of a Rectangle.");
System.out.println("3. Area of a circle.");
System.out.print("\nEnter your choice: ");
switch(sc.nextInt()){
case 1:
System.out.print("Enter side length: ");
double s=sc.nextDouble();
s*=s;
System.out.println("Area of the Square is: "+s);
break;
case 2:
System.out.print("Enter length of rectangle: ");
double l=sc.nextDouble();
System.out.print("Enter breadth of rectangle: ");
double b=sc.nextDouble();
System.out.println("Area of the rectangle is: "+l*b);
break;
case 3:
System.out.print("Enter radius of the circle: ");
double rad=sc.nextDouble();
double ar=Math.PI*rad*rad;
System.out.println("Area of the circle is: "+ar);
break;
default:
System.out.println("Invalid Choice.");
}
sc.close();
}
}
Output
1. Area of Square. 2. Area of a Rectangle. 3. Area of a circle. Enter your choice: 1 Enter side length: 8 Area of the Square is: 64.0 1. Area of Square. 2. Area of a Rectangle. 3. Area of a circle. Enter your choice: 2 Enter length of rectangle: 5 Enter breadth of rectangle: 6 Area of the rectangle is: 30.0 1. Area of Square. 2. Area of a Rectangle. 3. Area of a circle. Enter your choice: 2 Enter length of rectangle: 5 Enter breadth of rectangle: 6 Area of the rectangle is: 30.0