Introduction
Java is a widely used programming language that is popular for its platform independence and object-oriented programming features. In this article, we will discuss the development of a menu-driven program in Java used to execute queue operations. We will provide a detailed explanation of the program architecture, functionality, and code implementation.
Concepts and Architecture
Queue Data Structure
A queue is a linear data structure that follows the FIFO (First In First Out) principle. It is an ordered collection in which elements are added from the back and removed from the front. Queue operations include:
Enqueue (add element to the back of queue)
Dequeue (remove element from the front of queue)
Peek (return the front element without removing it)
Menu-Driven Program
A menu-driven program is an interactive program that allows users to select options from a menu of choices. The program displays a list of options, and the user selects an option by typing a number or letter corresponding to the desired option. This type of program is commonly used in applications where different functionalities need to be performed based on user input.
Program Architecture
The program consists of a main menu that provides the user with options to add elements to the queue, remove elements from the queue, display the front element of the queue, display the size of the queue, and exit the program. The program uses the Queue interface in Java to implement the queue data structure. The user’s input is taken as a command line argument, and the program uses switch statements to perform the appropriate operation based on the input.
Code Implementation
Let's take a look at the implementation details of the menu-driven program.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class QueueMenuDrivenProgram {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Queue Menu:");
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Peek");
System.out.println("4. Size");
System.out.println("5. Exit");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Enter element to add to queue:");
int element = scanner.nextInt();
queue.add(element);
System.out.println(element + " added to queue");
break;
case 2:
if (queue.isEmpty()) {
System.out.println("Queue is empty");
} else {
System.out.println(queue.remove() + " removed from queue");
}
break;
case 3:
if (queue.isEmpty()) {
System.out.println("Queue is empty");
} else {
System.out.println("Front element of queue: " + queue.peek());
}
break;
case 4:
System.out.println("Queue size: " + queue.size());
break;
case 5:
System.out.println("Exiting program");
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}
Explanation of Code
The program starts by creating an instance of the Queue interface, which is implemented using the LinkedList class. A scanner is initialized to read the user's input from the command line.
The program then enters an infinite loop that displays the main menu and waits for the user's input. The user's input is read using the scanner, and the program uses switch statements to perform the appropriate operation based on the input.
For example, if the user selects option 1 (Enqueue), the program prompts the user to enter the value to be added to the queue. The value is read using the scanner and added to the queue using the Queue.add() method. The program then displays a message indicating that the element has been added to the queue.
If the user selects option 2 (Dequeue), the program checks if the queue is empty using the Queue.isEmpty() method. If the queue is empty, the program displays a message indicating that the queue is empty. Otherwise, the program removes the front element of the queue using the Queue.remove() method and displays a message indicating that the element has been removed from the queue.
If the user selects option 3 (Peek), the program performs the same check for an empty queue but instead of removing the element, it returns the front element of the queue using the Queue.peek() method. If the queue is empty, the program displays a message indicating that the queue is empty. Otherwise, the program displays the front element of the queue.
If option 4 (Size) is selected, the program displays the size of the queue using the Queue.size() method.
If the user selects option 5 (Exit), the program exits using the System.exit() method.
Conclusion
In this article, we have discussed the development of a menu-driven program in Java used to execute queue operations. The program uses the Queue interface in Java and implements the queue data structure using the LinkedList class. We have provided a detailed explanation of the program architecture, functionality, and code implementation.
The program can be customized to include additional functionalities or use a different data structure. The concepts and techniques used in this program can be applied to the development of menu-driven programs for other types of data structures or applications.