JAVA PROGRAMMING In previous assignment you implemented MyQu

JAVA PROGRAMMING:

In previous assignment, you implemented MyQueue class by using a linked list with two instance variables: front and rear. For this lab assignment, you are required to implement MyQueue.java (ths class name MUST be MyQueue.java) with only one instance variable: rear, the reference to the rear of the queue.

1. Create a class Named MyQueue.java which extends MyQueueAbstract. Do not add any other data members or methods.
2. Implement enqueue and dequeue methods.
3. Write a client program named MyQueueApp.java to test your class: enqueue 3 Strings and dequeue to empty the queue

NB: previous assignment (it\'s done)- CSC202 fast food restaurant may not have great food but it prides itself being faster than its major competitors: CSC201, Nandoos, and Taco Tiger. CSC202 requires between 3 and 9 minutes to fill a customer order. On average, a customer arrives at CSC202 every 6 minutes. The manager would like for you to perform a simulation in order to gather statistics about the amount of time customers have to wait between the time they arrive and the time they get their order. For this assignment, You are required to to use a linked list to implement the Queue ADT. In addition, because we just started our business, we assume there is only one person taking orders.

Solution

Abstract Class

abstract class MyQueueAbstract
   {
       public int arr[] ;
       public int front, rear, size, len;
       public abstract void enqueue(int x);
       public abstract void dequeue();
   }

Queue Class

import java.util.*;
public class MyQueue extends MyQueueAbstract
   {
       public void arrayInit(int n)
       {
       size = n;
       len = 0;
       arr = new int[size];
       front = -1;
       rear = -1;
       }
/* Function to check if queue is empty */
       public boolean isEmpty()
       {
               return front == -1;
       }
/* Function to check if queue is full */
       public boolean isFull()
       {
       return front==0 && rear == size -1 ;
       }
/* Function to get the size of the queue */
       public int getSize()
       {
       return len ;
       }
/* Function to insert an element to the queue */
public void enqueue(int i)
{
if (rear == -1)
{
front = 0;
rear = 0;
arr[rear] = i;
}
else if (rear + 1 >= size)
System.out.println(\"Overflow \");
else if ( rear + 1 < size)
arr[++rear] = i;
len++ ;
}
/* Function to remove front element from the queue */
public void dequeue()
{
if (isEmpty())
System.out.println(\"Underflow\");
else
{
len-- ;
int ele = arr[front];
if ( front == rear)
{
front = -1;
rear = -1;
}
else
front++;
System.out.println(\" \" + ele);
}
}
/* Function to display the status of the queue */
public void display()
{
System.out.print(\"\ Queue = \");
if (len == 0)
{
System.out.print(\"Empty\ \");
return ;
}
for (int i = front; i <= rear; i++)
System.out.print(arr[i]+\" \");
System.out.println();
}              
  
   public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

System.out.println(\"Enter Size of Queue \");
int n = scan.nextInt();
/* creating object of class arrayQueue */
MyQueue q = new MyQueue();
       q.arrayInit(n);
/* Perform Queue Operations */
char ch;
do{
System.out.println(\"\ Queue Operations\");
System.out.println(\"1. insert\");
System.out.println(\"2. remove\");
System.out.println(\"3. check empty\");
System.out.println(\"4. check full\");
System.out.println(\"5. size\");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println(\"Enter integer element to insert\");
try
{
q.enqueue( scan.nextInt() );
}
catch(Exception e)
{
System.out.println(\"Error : \" +e.getMessage());
}   
break;   
case 2 :
try
{
q.dequeue();
}
catch(Exception e)
{
System.out.println(\"Error : \" +e.getMessage());
}
break;   
case 3 :
System.out.println(\"Empty status = \"+q.isEmpty());
break;
case 4 :
System.out.println(\"Full status = \"+q.isFull());
break;
case 5 :
System.out.println(\"Size = \"+ q.getSize());
break;   
default : System.out.println(\"Wrong Entry \ \");
break;
}
/* display Queue */
q.display();
System.out.println(\"\ Do you want to continue (Type y or n) \ \");
ch = scan.next().charAt(0);

} while (ch == \'Y\'|| ch == \'y\');
}
  
   }

JAVA PROGRAMMING: In previous assignment, you implemented MyQueue class by using a linked list with two instance variables: front and rear. For this lab assignm
JAVA PROGRAMMING: In previous assignment, you implemented MyQueue class by using a linked list with two instance variables: front and rear. For this lab assignm
JAVA PROGRAMMING: In previous assignment, you implemented MyQueue class by using a linked list with two instance variables: front and rear. For this lab assignm

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site