Your assignment is to create and test a class for a queue of objects. You may us
ID: 3680399 • Letter: Y
Question
Your assignment is to create and test a class for a queue of objects. You may use any object class of your choice as the data for the queue. The instances of the class should have at least one field that distinguishes each instance from other instances of the class (key property, also called a key field).
You should complete the software to implement and test your queue and submit the software along with a project report.
Your queue class should maintain a head pointer to the first element in the queue, a tail pointer to the last element in the queue, and the size of the queue as queue properties.
We will add elements to the tail of the queue and remove elements from the head of the queue. The head pointer points to the spot from which elements are deleted from the queue, and the tail pointer points to the end of the queue where elements are added to the queue.
Note that elements move though the queue in the opposite direction of the pointers from one element to the next. Head points to the first element in the queue. Head.next points to the element that will become the head when the head is removed (de-queue method) – the pointers point back in the queue.
You should have methods to:
instantiate a null queue
enqueue – add an element to the queue
dequeue – remove an element from the queue
size – return the size of the queue
a boolean isEmpty method
You will need to create a class of nodes for the data in the queue, which we call a queueElement.
Each queueElement should have data and a pointer to the next queueElement.
We need:
A null constructor (default)
An initializing constructor (instantiates a queueElement from a data element)
and methods to :
set the data in a queue element
return the data in a queue element
set the pointer to the next element
return the pointer to the next element
This assignment requires four classes – Queue, QueueElement, the data object class, and a class for your test project. Each class should be a separate file in your NetBeans project, all in the same source directory.
Explanation / Answer
Working java code for the queue class.
import java.util.*;
class Queue
{
protected int Queue[] ;
protected int front, rear, size, len;
public Queue(int n)
{
size = n;
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
public boolean isEmpty()
{
return front == -1;
}
public boolean isFull()
{
return front==0 && rear == size -1 ;
}
public int size() //size method for the queue class
{
return len ;
}
public int peek()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
return Queue[front];
}
public void enqueue(int i) //enqueue method for the queue class
{
if (rear == -1)
{
front = 0;
rear = 0;
Queue[rear] = i;
}
else if (rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow happened in queue. Throwing exception");
else if ( rear + 1 < size)
Queue[++rear] = i;
len++ ;
}
public int dequeue() //dequeue method
{
if (isEmpty())
throw new NoSuchElementException("Underflow happened in queue. Throwing exception");
else
{
len-- ;
int ele = Queue[front];
if ( front == rear)
{
front = -1;
rear = -1;
}
else
front++;
return ele;
}
}
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(Queue[i]+" ");
System.out.println();
}
}
public class QueueImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Array Queue Test ");
System.out.println("Enter Size of Integer Queue ");
int n = scan.nextInt();
Queue q = new Queue(n);
char ch;
do{
System.out.println(" Queue Operations");
System.out.println("1. enqueue");
System.out.println("2. dequeue");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. check full");
System.out.println("6. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to enqueue");
try
{
q.enqueue( scan.nextInt() );
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;
case 2 :
try
{
System.out.println("dequeued Element = "+q.dequeue());
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Peek Element = "+q.peek());
}
catch(Exception e)
{
System.out.println("Error : "+e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+q.isEmpty());
break;
case 5 :
System.out.println("Full status = "+q.isFull());
break;
case 6 :
System.out.println("Size = "+ q.size());
break;
default : System.out.println("Wrong Entry ");
break;
}
q.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.