(Java) Design a class named Queue for storing integers. Like a stack, a queue ho
ID: 3679682 • Letter: #
Question
(Java) Design a class named Queue for storing integers.
Like a stack, a queue holds elements. In a stack, the elements are retrieved in a last-in first-out fashion.
In a queue, the elements are retrieved in a first-in first-out fashion. The class contains:
- An int[] data field named elements that stores the int values in the queue.
- A data field named size that strores the number of elements in the queue.
- A constructor that creates a Queue object with default capacity 8.
- The method enqueue(int v) that adds v into the queue.
- The method dequeue() that removes and returns the element from the queue.
- The method empty() that returns true if the queue is empty.
- The method getSize() that retruns the size of the queue.
Implement the class with the initial array size to 8. The array size will be doubled once the number of the elements exceeds the size.
After an element is removed from the beginning of the array, you need to shift all the elements in the array one position the left.
Write a test program that adds 20 numbers from 1 to 20 into the queue and removes these numbers and displays them.
Explanation / Answer
Queue class:
//Program to implement linear queue using array
//This queue stores integers
public class Queue
{
//Instance variables
int MAX=8; //defining maximum size of queue
int arr[]; //array to behave as a queue
int front,rear; //pointers to point to the front and rear of queue
//constructor to create array and initialize members
public void QueueNoInterface()
{
arr=new int[MAX];
front=-1;
rear=-1; //initialized to -1 because queue is empty initially
}
//to check the queue is full or not
public boolean isFull()
{
if(rear==(MAX-1))//to check for max. capacity of queue
{
MAX = 2 * MAX;
return true;
}
else
return false;
}
//to check the queue is empty or not
public boolean isEmpty()
{
if(front==-1)//to check for empty queue
return true;
else
return false;
}
//to add an element to a queue
public void enqueue(int n)
{
if(isFull()) //call to isFull() method to check space in the queue
System.out.println("****Queue overflow******");
else
{
rear++; //Increment rear because element is added to the queue from rear end
arr[rear]=n;
if(front==-1)//if first element is being added
front=0;
System.out.println("******Element added to the queue*****");
}
}
//to remove an element from queue
public int dequeue()
{
int x;
if(isEmpty()) // check whether queue contains any element to be deleted or not
x=-999; //sentinel value
else
{
x=arr[front];
arr[front]=0;
if(front==rear) //if last element is removed queue again becomes empty
front=rear=-1;
else
front++; //because element is removed from queue from front end
}
return x;
}
//to show elements of queue
public void show()
{
if(isEmpty()) //if empty there is nothing to display in queue
System.out.print("****Empty*****");
else
{
for (int i=front;i<=rear;i++)
System.out.print(arr[i]+"");
}
System.out.println();
}//method ends
}//class ends
Queue_implement class:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Queue_implement {
public static void main(String[] args) throws NumberFormatException, IOException {
Queue obj=new Queue();
// displaying 20 numbers first in first out basis
/*for (int i = 0; i < 20; i++)
{
System.out.printf("%2d ", obj.dequeue());
if ((i + 1) % 5 == 0)
System.out.printf(" ");
}*/
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n=0, ch=0;
do
{
//displaying menu
System.out.println(" Enter 1 for adding an element to a queue");
System.out.println("Enter 2 for deleting an element from a queue");
System.out.println("Enter 3 for showing elements of a queue");
System.out.println("Enter 4 for exit");
System.out.println("Enter your choice");
ch=Integer.parseInt(br.readLine());
//selecting the right method according to choice
switch (ch)
{
//adding an element to the queue
case 1:
System.out.println(" Enter an integer to be added to the queue");
/*for (int i = 0; i < 20; i++)
{
obj.enqueue(i);
}*/
n=Integer.parseInt(br.readLine());
obj.enqueue(n);
break;
//deleting an element from queue
case 2:
int x=obj.dequeue();
if(x==-999) //sentinel value
System.out.println("******Queue underflow******");
else
System.out.println("****Element deleted is: “+x+” ******");
break;
//displaying queue status
case 3:
System.out.println("******Queue status*******");
obj.show();
break;
//Ending Program
case 4:
System.out.println("*****Program terminates******");
System.exit(0);
//If wrong code entered
default:
System.out.println("****Wrong code. please enter code 1 to 4 only****");
} //switch ends
}while(ch!=4);
}
}
Class Name: Queue
Instance variables:
arr[]- an array to behave as a queue
front, rear- pointers to point to the beginning and end of the queue respectively
MAX- constant to define the size of queue
Methods:
1. isFull()- returns true if queue has reached MAX, indicating no space in queue, otherwise returns false
2. isEmpty()- returns true if rear=-1 , indicating empty queue, otherwise returns false
3. enqueue()- adds an integer to the queue
4. dequeue()- deletes and returns an integer from queue
5. show()- shows all the integers in the queue
6. main()- calls all other methods of the class according to the user’s choice
PROGRAM CODE:
import java.io.*;
//Program to implement linear queue using array
//This queue stores integers
public class Queue
{
//Instance variables
final int MAX=10; //defining maximum size of queue
int arr[]; //array to behave as a queue
int front,rear; //pointers to point to the front and rear of queue
//constructor to create array and initialize members
public QueueNoInterface()
{
arr=new int[MAX];
front=rear=-1; //initialized to -1 because queue is empty initially
}
//to check the queue is full or not
public boolean isFull()
{
if(rear==(MAX-1))//to check for max. capacity of queue
return true;
else
return false;
}
//to check the queue is empty or not
public boolean isEmpty()
{
if(front==-1)//to check for empty queue
return true;
else
return false;
}
//to add an element to a queue
public void enqueue(int n)
{if(isFull()) //call to isFull() method to check space in the queue
System.out.println(“****Queue overflow******”);
else
{
rear++; //Increment rear because element is added to the queue from rear end
arr[rear]=n;
if(front==-1)//if first element is being added
front=0;
System.out.println(“******Element added to the queue*****”);
}
}
//to remove an element from queue
public int dequeue()
{
int x;
if(isEmpty()) // check whether queue contains any element to be deleted or not
x=-999; //sentinel value
else
{
x=arr[front];
arr[front]=0;
if(front==rear) //if last element is removed queue again becomes empty
front=rear=-1;
else
front++; //because element is removed from queue from front end
}
return x;
}
//to show elements of queue
public void show()
{
if(isEmpty()) //if empty there is nothing to display in queue
System.out.print(“****Empty*****”);
else
{
for (int i=front;i<=rear;i++)
System.out.print(arr[i]+” “);
}
System.out.println();
}//method ends
//main() method to call other methods
public static void main(String args[]) throws IOException
{
Queue queue = new Queue(); // inserting 20 numbers into queue
for (int i = 0; i < 20; i++)
{
queue.enqueue(i);
}
// displaying 20 numbers first in first out basis
for (int i = 0; i < 20; i++)
{
System.out.printf("%2d ", queue.dequeue());
if ((i + 1) % 5 == 0)
System.out.printf(" ");
}
QueueNoInterface obj=new QueueNoInterface();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n=0, ch=0;
do
{
//displaying menu
System.out.println(“ Enter 1 for adding an element to a queue”);
System.out.println(“Enter 2 for deleting an element from a queue”);
System.out.println(“Enter 3 for showing elements of a queue”);
System.out.println(“Enter 4 for exit”);
System.out.println(“Enter your choice”);
ch=Integer.parseInt(br.readLine());
//selecting the right method according to choice
switch (ch)
{
//adding an element to the queue
case 1:
System.out.println(“ Enter an integer to be added to the queue”);
n=Integer.parseInt(br.readLine());
obj.enqueue(n);
break;
//deleting an element from queue
case 2:
n=obj.dequeue();
if(n==-999) //sentinel value
System.out.println(“******Queue underflow******”);
else
System.out.println(“****Element deleted is: “+n+” ******”);
break;
//displaying queue status
case 3:
System.out.println(“******Queue status*******”);
obj.show();
break;
//Ending Program
case 4:
System.out.println(“*****Program terminates******”);
System.exit(0);
//If wrong code entered
default:
System.out.println(“****Wrong code. please enter code 1 to 4 only****”);
} //switch ends
}while(ch!=4);
}//method ends
}//class ends
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.