Computer Science, asked by abhiakhi, 1 year ago

how to write a menu driven program in java

Answers

Answered by ParveenTanwar
3
Import the Scanner.
Declare the class as public.
Add the void main function.
Add system.out.println() function with the message to enter choice.
Declare input as Scanner.
Take the inputs and save it in variables.
Add a Switch Statement.
Add the formula to calculate area.
Answered by zzliza
4
Program for menu driven system in java.

//import Scanner as we require it. import java.util.Scanner; // the name of our class its public public class MenuDrivenSystem { //void main public static void main (String[] args) { //declare float float a,b,area = 0; char choice; //Declare input as scanner Scanner input = new Scanner(System.in); //Take inputs System.out.println("Enter c for circle."); System.out.println("Enter s for square."); System.out.println("Enter r for rectangle."); System.out.println("Enter t for triangle."); String s = input.next(); choice = s.charAt(0); //add a switch statement switch(choice) { case 'c': System.out.println("Enter radius:"); a = input.nextFloat(); area = 3.14f*a*a; break; case 's': System.out.println("Enter side:"); a = input.nextFloat(); area = a*a; break; case 'r': System.out.println("Enter length and breadth:"); a = input.nextFloat(); b = input.nextFloat(); area = a*b; break; case 't': System.out.println("Enter base and height:"); a =input.nextFloat(); b =input.nextFloat(); area = 0.5f*a*b; break; default: System.out.println("Error"); } System.out.println("Area = "+area); } }

Output

Enter c for circle.
Enter s for square.
Enter r for rectangle.
Enter t for triangle.
t
Enter base and height:
12
54
Area = 324.0

How does it work

You enter the choice.

You enter the necessary inputs

The conditional statement decides the formula and calculates area and prints it.

Extending it

The program can be extended. You can use the program to create your own menu driven system. Go ahead…..

Explanation.

Import the Scanner.

Declare the class as public

Add the void main function

Add system.out.println() function with the message to enter choice.

Declare input as Scanner.

Take the inputs and save it in variables.

Add a Switch Statement.

Add the formula to calculate area.

Add system.out.println() function to print the area.

At the end.

You learnt creating the Java program for Creating Menu driven system. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.


—Hope it helps—
—please mark it as a brainliest answer if you like it—
Similar questions