Your assignment is to create and test a class for a queue of objects. You may us
ID: 3601322 • 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.
when I test it, it has problems. I also need to create the object class. here is the code:
//QueueTesting.java
package queuetesting;
import java.util.Scanner;
public class QueueTesting {
public static void main(String[] args) {
Scanner StdIn=new Scanner(System.in);
Queue q = new Queue();
int item=0;
boolean status=q.isEmpty();
if(status==true)
System.out.print("Queue is empty");
else
System.out.print("Queue is not empty");
while (item!=-999) {
System.out.println("Enter element <-999 to stop>");
item = StdIn.nextInt();
if (item!=0) q.enqueue(item);
else if (!q.isEmpty())
System.out.print(q.dequeue() + " ");
}
System.out.println("Number of elements in queue: " + q.size());
System.out.println("Queue: "+ q.toString());
}
}
/*==========================================================*/
//QueueElement .java
package queuetesting;
public class QueueElement {
int item;
QueueElement next;
public QueueElement()
{}
public QueueElement(int d)
{
item=d;
}
public void setData(int d)
{
item=d;
}
public void setPtrToNextElement(QueueElement q)
{
next=q;
}
public QueueElement returnPtrToNextElement()
{
return next;
}
}
/*==========================================================*/
//Queue.java
package queuetesting;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Queue {
private int N; // number of elements on queue
private QueueElement head ; // beginning of queue
private QueueElement tail; // end of queue
public Queue()
{
N=0;
head=null;
tail=null;
}
/**
* Is this queue empty?
* @return true if this queue is empty; false otherwise
*/
public boolean isEmpty() {
return head == null;
}
/**
* Returns the number of items in this queue.
* @return the number of items in this queue
*/
public int size() {
return N;
}
/**
* Adds the item to this queue.
* @param element
*/
public void enqueue(Object element) {
QueueElement oldlast = tail;
tail = new QueueElement();
tail.item=(int)element;
// tail.data = (int)element;
tail.next = null;
if (isEmpty()) head = tail;
else
oldlast.next = tail;
N++;
assert check();
}
/**
* Removes and returns the item on this queue that was least recently added.
* @return the item on this queue that was least recently added
* @throws java.util.NoSuchElementException if this queue is empty
*/
public Object dequeue() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
int item = head.item;
head = head.next;
N--;
if (isEmpty()) tail = null; // to avoid loitering
assert check();
return item;
}
// an iterator, doesn't implement remove() since it's optional
private class ListIterator implements Iterator {
private QueueElement current = head;
public boolean hasNext() { return current != null; }
public void remove() { throw new UnsupportedOperationException(); }
public Object next() {
if (!hasNext()) throw new NoSuchElementException();
int item = current.item;
current = current.next;
return item;
}
}
public Iterator iterator() {
return new ListIterator();
}
public String toString()
{
StringBuilder s = new StringBuilder();
for (Object listIter = iterator().next(); iterator().hasNext();){
s.append(iterator().next()).append(" ");
}
return s.toString();
}
// check internal invariants
private boolean check() {
if (N < 0) {
return false;
}
else if (N == 0) {
if (head != null) return false;
if (tail != null) return false;
}
else if (N == 1) {
if (head == null || tail == null) return false;
if (head != tail) return false;
if (head.next != null) return false;
}
else {
if (head == null || tail == null) return false;
if (head == tail) return false;
if (head.next == null) return false;
if (tail.next != null) return false;
// check internal consistency of instance variable N
int numberOfNodes = 0;
for (QueueElement x = head; x != null && numberOfNodes <= N; x = x.next) {
numberOfNodes++;
}
if (numberOfNodes != N) return false;
// check internal consistency of instance variable tail
QueueElement tailNode = head;
while (tailNode.next != null) {
tailNode = tailNode.next;
}
if (tail != tailNode) return false;
}
return true;
}
}
Explanation / Answer
Queue implementation we can achieve in multiple ways. As I observed from your code, how you're implemented was quite good. Small issue with your code was
1) while printing the queue elements , its iterating into infinite loop , this is due to first element only available in iterater loop always - I refactored the code on the same
2) Naming convension are as per the standard
Please find the code below
QueueDriver.java - test class
import java.util.Scanner;
public class QueueDriver {
public static void main(String[] args) {
Scanner StdIn = new Scanner(System.in);
Queue q = new Queue();
int item = 0;
boolean status = q.isEmpty();
if (status == true)
System.out.print("Queue is empty");
else
System.out.print("Queue is not empty");
while (item != -999) {
System.out.println("Enter element <-999 to stop>");
item = StdIn.nextInt();
if (item != 0)
q.enqueue(item);
else if (!q.isEmpty())
System.out.print(q.dequeue() + " ");
}
System.out.println("Number of elements in queue: " + q.size());
System.out.println(q);
String queueValues = q.queueElements(q);
System.out.println("Queue: " + queueValues);
}
}
Queue.java:
Main business logic avaiable here , my code changes you can see here
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Queue {
private int N; // number of elements on queue
private QueueElement head; // beginning of queue
private QueueElement tail; // end of queue
public Queue() {
N = 0;
head = null;
tail = null;
}
/**
* Is this queue empty?
*
* @return true if this queue is empty; false otherwise
*/
public boolean isEmpty() {
return head == null;
}
/**
* Returns the number of items in this queue.
*
* @return the number of items in this queue
*/
public int size() {
return N;
}
/**
* Adds the item to this queue.
*
* @param element
*/
public void enqueue(Object element) {
QueueElement oldlast = tail;
tail = new QueueElement();
tail.item = (int) element;
// tail.data = (int)element;
tail.next = null;
if (isEmpty())
head = tail;
else
oldlast.next = tail;
N++;
assert check();
}
/**
* Removes and returns the item on this queue that was least recently added.
*
* @return the item on this queue that was least recently added
* @throws java.util.NoSuchElementException
* if this queue is empty
*/
public Object dequeue() {
if (isEmpty())
throw new NoSuchElementException("Queue underflow");
int item = head.item;
head = head.next;
N--;
if (isEmpty())
tail = null; // to avoid loitering
assert check();
return item;
}
// an iterator, doesn't implement remove() since it's optional
private class ListIterator implements Iterator {
private QueueElement current = head;
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Object next() {
if (!hasNext())
throw new NoSuchElementException();
int item = current.item;
current = current.next;
return item;
}
}
public Iterator iterator() {
return new ListIterator();
}
/*public String toString() {
StringBuilder s = new StringBuilder();
while (iterator().hasNext()) { //In your code always returning first element what it is stored , iterating the loop for infinite so we're getting java heap space error
s.append(iterator().next());
s.append(' ');
}
return s.toString();
}*/
public String queueElements(Queue q) {
QueueElement head = q.head;
StringBuilder builder = new StringBuilder();
while(head != null) {
builder.append(head.item);
builder.append(' ');
head = head.next;
}
return builder.toString();
}
// check internal invariants
private boolean check() {
if (N < 0) {
return false;
} else if (N == 0) {
if (head != null)
return false;
if (tail != null)
return false;
} else if (N == 1) {
if (head == null || tail == null)
return false;
if (head != tail)
return false;
if (head.next != null)
return false;
} else {
if (head == null || tail == null)
return false;
if (head == tail)
return false;
if (head.next == null)
return false;
if (tail.next != null)
return false;
// check internal consistency of instance variable N
int numberOfNodes = 0;
for (QueueElement x = head; x != null && numberOfNodes <= N; x = x.next) {
numberOfNodes++;
}
if (numberOfNodes != N)
return false;
// check internal consistency of instance variable tail
QueueElement tailNode = head;
while (tailNode.next != null) {
tailNode = tailNode.next;
}
if (tail != tailNode)
return false;
}
return true;
}
}
QueueElemets.java - supporter class/helper class to define Queue and maintain
public class QueueElement {
int item;
QueueElement next;
public QueueElement() {
}
public QueueElement(int d) {
item = d;
}
public void setData(int d) {
item = d;
}
public void setPtrToNextElement(QueueElement q) {
next = q;
}
public QueueElement returnPtrToNextElement() {
return next;
}
}
Please feel free to back , if you have any concern
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.