This is what i get so far
Expand|Select|Wrap|Line Numbers
- public abstract class DataElement
- {
- public abstract boolean equals(DataElement otherElement);
- //Method to determine whether two objects contain the
- //same data.
- //Postcondition: Returns true if this object contains the
- // same data as the object otherElement;
- // otherwise, it returns false.
- public abstract int compareTo(DataElement otherElement);
- //Method to compare two objects.
- //Postcondition: Returns a value < 0 if this object is
- // less than the object otherElement;
- // Returns 0 if this object is the same as
- // the object otherElement.
- // Returns a value > 0 if this object is
- // greater than the object otherElement.
- public abstract void makeCopy(DataElement otherElement);
- //Method to copy otherElement into this object.
- //Postcondition: The data of otherElement is copied into
- // this object.
- public abstract DataElement getCopy();
- //Method to return a copy of this object.
- //Postcondition: A copy of this object is created and
- // a reference of the copy is returned.
- }
- public class IntElement extends DataElement
- {
- protected int num;
- //default constructor
- public IntElement()
- {
- num = 0;
- }
- //constructor with a parameter
- public IntElement(int x)
- {
- num = x;
- }
- //copy constructor
- public IntElement(IntElement otherElement)
- {
- num = otherElement.num;
- }
- //Method to set the value of the instance variable num.
- //Postcondition: num = x;
- public void setNum(int x)
- {
- num = x;
- }
- //Method to return the value of the instance variable num.
- //Postcondition: The value of num is returned.
- public int getNum()
- {
- return num;
- }
- public boolean equals(DataElement otherElement)
- {
- IntElement temp = (IntElement) otherElement;
- return (num == temp.num);
- }
- public int compareTo(DataElement otherElement)
- {
- IntElement temp = (IntElement) otherElement;
- return (num - temp.num);
- }
- public void makeCopy(DataElement otherElement)
- {
- IntElement temp = (IntElement) otherElement;
- num = temp.num;
- }
- public DataElement getCopy()
- {
- IntElement temp = new IntElement(num);
- return temp;
- }
- public String toString()
- {
- return String.valueOf(num);
- }
- }
- public class QueueException extends RuntimeException
- {
- public QueueException()
- {
- }
- public QueueException(String msg)
- {
- super(msg);
- }
- }
- public class QueueOverflowException extends QueueException
- {
- public QueueOverflowException()
- {
- super("Queue Overflow");
- }
- public QueueOverflowException(String msg)
- {
- super(msg);
- }
- }
- public class QueueUnderflowException extends QueueException
- {
- public QueueUnderflowException()
- {
- super("Queue Underflow");
- }
- public QueueUnderflowException(String msg)
- {
- super(msg);
- }
- }
- public class LinkedQueueClass
- {
- //Definition of the node
- protected class QueueNode
- {
- DataElement info;
- QueueNode link;
- }
- private QueueNode queueFront; //reference variable to the
- //first element of the queue
- private QueueNode queueRear; //reference variable to the
- //last element of the queue
- //default constructor
- public LinkedQueueClass()
- {
- queueFront = null;
- queueRear = null;
- }
- //copy constructor
- public LinkedQueueClass(LinkedQueueClass otherQueue)
- {
- queueFront = otherQueue.queueFront;
- queueRear = otherQueue.queueRear;
- }//end copy constructor
- //Method to initialize the queue to an empty state.
- //Postcondition: queueFront = null; queueRear = null
- public void initializeQueue()
- {
- queueFront = null;
- queueRear = null;
- }
- //Method to determine whether the queue is empty.
- //Postcondition: Returns true if the queue is empty;
- // otherwise, returns false.
- public boolean isEmptyQueue()
- {
- return (queueFront == null);
- }
- //Method to determine whether the queue is full.
- //Postcondition: Returns true if the queue is full;
- // otherwise, returns false.
- public boolean isFullQueue()
- {
- return false;
- }
- //Method to return the first element of the queue.
- //Precondition: The queue exists and is not empty.
- //Postcondition: If the queue is empty, the method throws
- // QueueUnderflowException; otherwise, a
- // reference to a copy of the first element
- // of the queue is returned.
- public DataElement front() throws QueueUnderflowException
- {
- if(isEmptyQueue())
- throw new QueueUnderflowException();
- DataElement temp = queueFront.info.getCopy();
- return temp;
- }
- //Method to return the last element of the queue.
- //Precondition: The queue exists and is not empty.
- //Postcondition: If the queue is empty, the method throws
- // QueueUnderflowException; otherwise, a
- // reference to a copy of the last element
- // of the queue is returned.
- public DataElement back() throws QueueUnderflowException
- {
- if(isEmptyQueue())
- throw new QueueUnderflowException();
- DataElement temp = queueRear.info.getCopy();
- return temp;
- }
- //Method to add queueElement to the queue.
- //Precondition: The queue exists.
- //Postcondition: The queue is changed and queueElement
- // is added to the queue.
- public void addQueue(DataElement newElement)
- {
- QueueNode newNode;
- newNode = new QueueNode(); //create the node
- newNode.info = newElement.getCopy(); //store the info
- newNode.link = null; //initialize the link field to null
- if(queueFront == null) //if initially the queue is empty
- {
- queueFront = newNode;
- queueRear = newNode;
- }
- else //add newNode at the end
- {
- queueRear.link = newNode;
- queueRear = queueRear.link;
- }
- }//end addQueue
- //Method to remove the first element of the queue.
- //Precondition: The queue exists and is not empty.
- //Postcondition: The queue is changed and the first
- // element is removed from the queue.
- public void deleteQueue() throws QueueUnderflowException
- {
- if(isEmptyQueue())
- throw new QueueUnderflowException();
- queueFront = queueFront.link; //advance queueFront
- if(queueFront == null) //if after deletion the queue is
- queueRear = null; //empty, set queueRear to null
- } //end deleteQueue
- //Method to make a copy of otherQueue.
- //Postcondition: A copy of otherQueue is created and
- // assigned to this queue.
- public void copyQueue(LinkedQueueClass otherQueue)
- {
- if (this != otherQueue) //avoid self-copy
- copyQueue(otherQueue);
- }
- }
- public class QueueProgram
- {
- public static void main(String[] args)
- {
- LinkedQueueClass intQueue1 = new LinkedQueueClass();
- LinkedQueueClass intQueue2 = new LinkedQueueClass();
- LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
- LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
- intQueue1.addQueue(new IntElement(23));
- intQueue1.addQueue(new IntElement(45));
- intQueue1.addQueue(new IntElement(38));
- intQueue2.addQueue(new IntElement(32));
- intQueue2.addQueue(new IntElement(54));
- intQueue2.addQueue(new IntElement(83));
- System.out.print("intQueue1 elements: ");
- while(!intQueue1.isEmptyQueue())
- {
- System.out.print(intQueue1.front() + " ");
- intQueue1.deleteQueue();
- }
- System.out.println();
- System.out.print("intQueue2 elements: ");
- while(!intQueue2.isEmptyQueue())
- {
- System.out.print(intQueue2.front() + " ");
- intQueue2.deleteQueue();
- }
- System.out.println();
- intQueue1.copyQueue(intQueue2);
- System.out.print("After copying intQueue1 elements into intQueue2, intQueue2 elements are: ");
- }
- }
----jGRASP exec: java QueueProgram
intQueue1 elements: 23 45 38
intQueue2 elements: 32 54 83
java.lang.StackOverflowError
Exception in thread "main"
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.