Menu Driven Program in Java Example

Hey everyone, welcome to my new Menu Driven Program In Java Tutorial. Here you will learn to create a menu driven program using Java programming language.

But before implementing menu driven code, we need to understand about what is menu driven program, why we use it and what are the various ways to create a menu driven program in Java. So without wasting time let's gets started.

Also Read – Object Oriented Programming in Java Questions and Answers

Contents

  • 1 An Introduction To Menu Driven Program In Java
    • 1.1 What is a menu driven program in Java?
    • 1.2 Which statement is specially used for menu driven program in Java?
    • 1.3 How do you write an algorithm for a menu-driven program?
    • 1.4 How do I create a menu driven program?
  • 2 How To Write Menu Driven Program In Java
    • 2.1 Menu Driven Program in Java for Calculator
    • 2.2 Menu Driven Program in Java for String Operations
    • 2.3 Menu Driven Program in Java using While Loop
    • 2.4 What We Did?
    • 2.5 Menu Driven Program In Java Using do-while Loop
    • 2.6 Menu driven program in Java using Methods
    • 2.7 How do I exit from a Menu Driven Program?

In many applications you must have seen menus. Menus are generally a collection of choice in which user can select any choice according to their interest and can perform that particular task.

So let's know a brief idea about menu driven in Java.

A menu driven program displays a list of options to take input from user. User can select and enter options displayed on screen as per his/her choice and can do whatever they want to do.

Menu driven program are very user friendly that's why most applications use this instead of command line input. The two advantage of using menu driven program is –

  • The system is less prone to user error because input is via single key strokes
  • Because only a limited range of characters are allowed, the way in which the input is to be entered is unambiguous.

Switch statement is used to create menu driven program because switch statement contains cases. Each case contains different functionality.

Switch statement takes user choice as input and calls the correct case related to choice so that correct task can be performed.

I hope now you have understand all about menu driven program. But still you will be thinking how to implement  it. So first let's understand algorithm for a menu driven program.

We can write menu driven program in two ways –

  • Write the whole program in switch-case branches.
  • Create separate methods and call those methods from the switch-case branch.

So guys until now you have learned about menu driven program now its time to writing java program for it.

Menu driven program using java language is generally written with the help of switch statement. So let's implement it practically.

In this example, we will create a simple menu-driven program for calculation purposes of 4 essential math operations Addition, Subtraction, Multiplication, and division.

Code is following.

What We Did?

  • First of all, we have imported the Scanner class.
  • Then created a class and given a name CalculatorMenuDrivenExample.
  • Then started the main method.
  • Inside main method, declared all the variable names which we used later.
  • After that, I have created an infinite while loop by using true as its condition, and it will run for infinite times, and later on, we will also have the condition to exit from this infinite loop.
  • Then displayed a menu.
  • After that asked the user to enter their choice.
  • Then created switch case branch. The first case is for finding the addition of two numbers, the second case is for finding the difference of two numbers, the third case is for finding the product of two numbers, the fourth case is for finding the quotient and the fifth case is for quitting the program.
  • If the user enters the wrong choice which is not available in the menu, then a message of invalid choice will display.

Output

Now check it's output which is following.

Menu Driven Program In Java
Calculator Menu Driven Program

We are taking an example where we have to write a simple menu driven program using Java for string operations. If you want to learn string operations in java then check here.

We can perform various operations on string in Java but here I am taking just 4 string operations which are following to illustrate this example.

  • Length of string
  • Concatenate two strings
  • Comparing two strings
  • String trimming

So now let's write menu driven program for string operations in Java. Code is following.

What We Did?

  • First of all we have imported Scanner class.
  • Then created a class and given a name StringMenuDrivenExample.
  • Then started the main method.
  • Inside main method, declared all the variable names which we used later.
  • Then displayed a menu.
  • After that asked the user to enter their choice.
  • Then created switch case branch. First case is for finding length of string, second case is for concatenation of string, third case is for comparison of strings, forth case is for trimming of string and 5 case is for exit the program.
  • If the user enters the wrong choice which is not available in the menu, then a message of invalid choice will display.

Output

Now check it's output which is following.

Menu Driven Program in Java
String Operation using Menus in Java

In the output, you can see user has entered choice 2 which is string concatenation. That's why second case is executed.

Now we will see how to make menu driven program using while loop in Java language.

What We Did?

  • We have used while loop whose condition statement always evaluates to true.
  • Inside the while loop, we have displayed the menu and reading user's choice.
  • When user will enter his/her choice then string operations will be performed according to their entered choice.

Output

Now it's time to check the output. So its outputs are as follows.

Menu driven program in Java
Choice 1
Menu driven program in Java
Choice 2
Choice 3
Choice 4

In a menu driven program, generally we have to execute body of menu loop at least once. In this case do-while loop is very helpful to create menu driven program.

So the code of menu driven system using do-while loop in Java is following. Let's write and execute this code to check the output.

Output

So the output of the above code is following.

Menu driven program in Java
Choice 1
Choice 3
Choice 5

In this case, what we will do that when the user makes any choices, then we will call a specific method inside the switch case for that particular choice.

For the example purpose, I am going to find the area of rectangle, square and circle. If the user makes a choice first, then the area of the rectangle will be evaluated. If the user makes choice second, then the area of the square will be evaluated. If the user makes a choice third, then the area of the circle will be evaluated.

So the code of menu driven system using method in Java is following. Let's write and execute this code to check the output.

What We Did?

  • First of all we have imported Scanner class.
  • Then created a class and given a name MenuDrivenUsingFuction.
  • Inside the class, we declared all the variables and created a Scanner class which we used later.
  • Then started the main method.
  • Inside the main method, displayed a menu and asked the user to make his choice.
  • Then created switch case branch. If The user makes choice 1, then we will call the method to evaluate the area of the Rectangle, if the user makes choice 2, then we will call the method to evaluate the area of the Square, if the user makes choice 3, then we will call the method to evaluate the area of the Circle, if the user makes choice 4 then the program will quit, and if the user makes any invalid choice, then the default case will be executed.
  • Then created separate methods to evaluate the area of the Rectangle, Square, and Circle.

Output

Now check it's output which is following.

Menu driven program in Java
Menu driven program using functions

To exit the menu driven program, you can put a case in the switch case so that the program will quit and for that purpose, there is a predefined method System.exit(int) that you can use in order to achieve program termination.

So guys here I am wrapping up Menu Driven Program In Java. I hope you have a good understanding of how to write a menu driven code in Java. But still if you have any query then don't forget to drop your queries in comment section.

People Are Also Reading…

  • How to Play Mp3 File in Java Tutorial | Simple Steps
  • Tic Tac Toe Java Code Against Computer With Source Code
  • Calculator Program in Java Swing/JFrame with Source Code
  • Registration Form in Java With Database Connectivity
  • How to Create Login Form in Java Swing
  • Text to Speech in Java
  • How to Create Splash Screen in Java
  • Java Button Click Event
  • Object-Oriented Programming in Java Questions and Answers
  • Java Swing Password Field

gundersoncyricionsien.blogspot.com

Source: https://www.tutorialsfield.com/menu-driven-program-in-java/

0 Response to "Menu Driven Program in Java Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel