Menu Driven Program in Java [ Program With Explanation ]
What is Menu Driven Program in Java?
As the name suggests the menu driven program is a type of computer program which accepts the input from the user by showing a list of options. And users have to select any of the options to perform any operation. An example of a menu driven program is a washing machine which consists of a menu driven programmed microprocessor.
The second example of menu driven program in java is a Simple calculator which we used to perform some calculations. If we press ‘+’ for addition then the addition of two numbers case is executed. We can also do the calculations related to mensuration i.e calculating the area of the circle, the circumference of the cylinder, etc.
Menu driven programs are beneficial in a situation where the multitasking machines can perform a single task at a time. So without wasting any time we’ll see how to write menu driven program in java.
In this tutorial, we’ll code menu driven programs in java using different ways. Let’s see them one by one:
Menu Driven Program in Java Using While Loop
In this program, we’ll write a java program to perform string operations using a while loop.
Output:

Explanation:
In the above program, we’ve used a while loop to perform string operations. After executing the program the compiler displays the list of menu options. And then the user has to choose the option by pressing the numbers which are assigned to the menu. If the user enters 1 and selects the Length of the string then the compiler asks the user to enter the string and then displays the output as the length of the entered string.
Similarly, after choosing the menu option 2nd the compiler asks the user to enter the first and the second string for concatenation. And after the concatenated string will be displayed on the screen.
If the user enters the wrong choice then the message “Invalid choice!!! Please make a valid choice.” will be displayed.
By choosing menu option 5 the program will be exited.
Menu Driven Program in Java Using Switch Case
In this program, we’ll build one simple calculator which accepts some input from the user and then perform calculations.
Output:

Explanation:
If we write a simple calculator program using c then we use a switch case to perform different operations. Similarly in Java, we can use a switch case to build a calculator using functions or while loops. In this program, we’re writing a program using a switch case. We’ll do the basic mathematical operations such as Addition, Subtraction, Multiplication, and Division. Users have to input 1 to perform Addition Similarly 2 for Subtraction, 3 for Multiplication, and 4 for Division.
In the program, the input entered by the user is stored in the choice variable. Then the compiler compares the choice variable with the numbers and jumps to the matched case. The cases used in this program accept input numbers to perform operations. If we want to perform the addition then the first and second number is taken by the user.
Then the compiler calculates the addition and prints the result on the screen. If the user enters the wrong choice then the message “Invalid choice!!! Please make a valid choice.” is printed. If the user wants to stop the execution of the program then he or she should choose menu option 5.
Menu Driven Program in Java Using Functions
In this program, we’ll write menu driven program in java using functions.
Output:

Explanation:
In the above program, we’ve used functions to write menu driven programs in java. After executing the above program the compiler will display the list of menu options. The user has to choose the option and enter it. The compiler will compare the input entered by the user and then call the function.
Suppose if the user enters input 1 then the compiler will store that input into choice and check for the condition choice==1 then the function rectangleArea() will be executed. The logic of calculating the area of a rectangle is written in the rectangleArea() function which accepts the length and the breadth and displays the area as the output.
After calculating the area the compiler displays the desired area of the circle on the screen. If you enter the wrong choice then the compiler will print the “You have entered the wrong choice” message.
After choosing menu option 4 the program will be stopped.
Conclusion
In this way, we’ve learned how to write menu driven programs in java using switch case, while loop, functions. We’ve written three different programs to clearly understand the menu concept in java. If you have any doubts then please feel free to contact us. We’ll contact you as soon as possible.
Thanks and Happy Coding 🙂
Post a Comment