Netbeans/ JAVA write this code: document every line. Four Classes! Your assignme
ID: 3670903 • Letter: N
Question
Netbeans/ JAVA write this code: document every line. Four Classes!
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 head queue, and the size of the queue as queue properties. 4 tail 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.Explanation / Answer
//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;
}
}
/*==========================================================*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.