Java Programming | Creating a Menu driven program

Introduction

This is a tutorial for creating a menu driven program in java. The program is given below which creates a menu for area of different shapes and asks the user to calculate the area for which shape. The program is extendable. Go enjoy the program. Lets begin……..

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

  1. You enter the choice.
  2. You enter the necessary inputs
  3. 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.

  1. Import the Scanner.
  2. Declare the class as public
  3. Add the void main function
  4. Add system.out.println() function with the message to enter choice.
  5. Declare input as Scanner.
  6. Take the inputs and save it in variables.
  7. Add a Switch Statement.
  8. Add the formula to calculate area.
  9. 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s