need help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satan
    New Member
    • Oct 2006
    • 28

    #1

    need help

    I need the definitions of the method copyQueue, the default constructor, and the copy constructor folr the class LinkedQueueClas s.
    This is what i get so far


    Code:
    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: ");
     
          
     
        }
    }
    And i'm getting this error in my output:
    ----jGRASP exec: java QueueProgram

    intQueue1 elements: 23 45 38
    intQueue2 elements: 32 54 83
    java.lang.Stack OverflowError
    Exception in thread "main"
    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Code:
     
    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.
    }
      
    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);
    	}
    }
     
    class QueueException extends RuntimeException
    {
    	public QueueException()
    	{
    	}
    	public QueueException(String msg)
    	{
    		super(msg);
    	}
    }
     
    class QueueOverflowException extends QueueException
    {
    	public QueueOverflowException()
    	{
    		 super("Queue Overflow");
    	}
    	public QueueOverflowException(String msg)
    	{
    		super(msg);
    	}
    }
     
    class QueueUnderflowException extends QueueException
    {
    	public QueueUnderflowException()
    	{
    		 super("Queue Underflow");
    	}
    	public QueueUnderflowException(String msg)
    	{
    		super(msg);
    	}
    }
     
    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 LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
    	{
    		LinkedQueueClass intQueue1 = new LinkedQueueClass();
    	   while(!otherQueue.isEmptyQueue())
      {
    	  intQueue1.addQueue(otherQueue.front());
    	  otherQueue.deleteQueue();
    		}
    	return intQueue1;
    
    	}
    }
     
    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("Here");
    		System.out.print("intQueue2 elements: ");
    		while(!intQueue2.isEmptyQueue())
    		{
    			System.out.print(intQueue2.front() + " ");
    			intQueue2.deleteQueue();
    		}
    
      //intQueue2 is now empty
      intQueue2.addQueue(new IntElement(32));
      intQueue2.addQueue(new IntElement(54));
    		intQueue2.addQueue(new IntElement(83));
     
    		intQueue1 = intQueue1.copyQueue(intQueue2);
    		System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
    
      while(!intQueue1.isEmptyQueue())
      {
    	   System.out.print(intQueue1.front() + " ");
    	   intQueue1.deleteQueue();
    		}
    
    	}
    }
    Do the same for the copy constructor

    Comment

    • satan
      New Member
      • Oct 2006
      • 28

      #3
      Originally posted by r035198x
      Code:
       
      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.
      }
        
      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);
      	}
      }
       
      class QueueException extends RuntimeException
      {
      	public QueueException()
      	{
      	}
      	public QueueException(String msg)
      	{
      		super(msg);
      	}
      }
       
      class QueueOverflowException extends QueueException
      {
      	public QueueOverflowException()
      	{
      		 super("Queue Overflow");
      	}
      	public QueueOverflowException(String msg)
      	{
      		super(msg);
      	}
      }
       
      class QueueUnderflowException extends QueueException
      {
      	public QueueUnderflowException()
      	{
      		 super("Queue Underflow");
      	}
      	public QueueUnderflowException(String msg)
      	{
      		super(msg);
      	}
      }
       
      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 LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
      	{
      		LinkedQueueClass intQueue1 = new LinkedQueueClass();
      	   while(!otherQueue.isEmptyQueue())
        {
      	  intQueue1.addQueue(otherQueue.front());
      	  otherQueue.deleteQueue();
      		}
      	return intQueue1;
      
      	}
      }
       
      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("Here");
      		System.out.print("intQueue2 elements: ");
      		while(!intQueue2.isEmptyQueue())
      		{
      			System.out.print(intQueue2.front() + " ");
      			intQueue2.deleteQueue();
      		}
      
        //intQueue2 is now empty
        intQueue2.addQueue(new IntElement(32));
        intQueue2.addQueue(new IntElement(54));
      		intQueue2.addQueue(new IntElement(83));
       
      		intQueue1 = intQueue1.copyQueue(intQueue2);
      		System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
      
        while(!intQueue1.isEmptyQueue())
        {
      	   System.out.print(intQueue1.front() + " ");
      	   intQueue1.deleteQueue();
      		}
      
      	}
      }
      Do the same for the copy constructor

      Please, the same what?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by satan
        Please, the same what?
        Sorry, I meant to adapt the copyQueue method in writing the copy constructor.

        Comment

        • satan
          New Member
          • Oct 2006
          • 28

          #5
          Originally posted by r035198x
          Sorry, I meant to adapt the copyQueue method in writing the copy constructor.
          U meant like this! if so i've done like that but it's not working.


          Code:
          //copy constructor
          	public LinkedQueueClass(LinkedQueueClass otherQueue)
          	{
          		queueFront = otherQueue.queueFront;
          		queueRear  = otherQueue.queueRear;
          	}//end copy constructor


          Code:
          Method to make a copy of otherQueue.
                  //Postcondition: A copy of otherQueue is created and
                  //               assigned to this queue.
              public void copyQueue(LinkedQueueClass otherQueue)
              {
                     queueFront = otherQueue.queueFront;
                      queueRear = otherQueue.queueRear;
                    }
          }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by satan
            U meant like this! if so i've done like that but it's not working.


            Code:
            //copy constructor
            	public LinkedQueueClass(LinkedQueueClass otherQueue)
            	{
            		queueFront = otherQueue.queueFront;
            		queueRear = otherQueue.queueRear;
            	}//end copy constructor


            Code:
            Method to make a copy of otherQueue.
            //Postcondition: A copy of otherQueue is created and
            // assigned to this queue.
            public void copyQueue(LinkedQueueClass otherQueue)
            {
            queueFront = otherQueue.queueFront;
            queueRear = otherQueue.queueRear;
            }
            }
            No. To adapt this

            Code:
            public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
            {
              LinkedQueueClass intQueue1 = new LinkedQueueClass();
               while(!otherQueue.isEmptyQueue())
              {
            	  intQueue1.addQueue(otherQueue.front());
            	  otherQueue.deleteQueue();
              }
              return intQueue1;
             }
            Is this not working as well?

            Comment

            • satan
              New Member
              • Oct 2006
              • 28

              #7
              Originally posted by r035198x
              No. To adapt this

              Code:
              public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
              {
                LinkedQueueClass intQueue1 = new LinkedQueueClass();
                 while(!otherQueue.isEmptyQueue())
                {
              	  intQueue1.addQueue(otherQueue.front());
              	  otherQueue.deleteQueue();
                }
                return intQueue1;
               }
              Is this not working as well?
              It's not working either

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by satan
                It's not working either
                What are you saying is not working?

                Code:
                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.
                }
                  
                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);
                }
                }
                
                class QueueException extends RuntimeException
                {
                public QueueException()
                {
                }
                public QueueException(String msg)
                {
                super(msg);
                }
                }
                
                class QueueOverflowException extends QueueException
                {
                public QueueOverflowException()
                {
                super("Queue Overflow");
                }
                public QueueOverflowException(String msg)
                {
                super(msg);
                }
                }
                
                class QueueUnderflowException extends QueueException
                {
                public QueueUnderflowException()
                {
                super("Queue Underflow");
                }
                public QueueUnderflowException(String msg)
                {
                super(msg);
                }
                }
                
                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 LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
                {
                LinkedQueueClass intQueue1 = new LinkedQueueClass();
                   while(!otherQueue.isEmptyQueue())
                  {
                  intQueue1.addQueue(otherQueue.front());
                  otherQueue.deleteQueue();
                }
                return intQueue1;
                
                }
                }
                
                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("Here");
                System.out.print("intQueue2 elements: ");
                while(!intQueue2.isEmptyQueue())
                {
                System.out.print(intQueue2.front() + " ");
                intQueue2.deleteQueue();
                }
                
                  //intQueue2 is now empty
                  intQueue2.addQueue(new IntElement(32));
                  intQueue2.addQueue(new IntElement(54));
                intQueue2.addQueue(new IntElement(83));
                
                intQueue1 = intQueue1.copyQueue(intQueue2);
                System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
                
                  while(!intQueue1.isEmptyQueue())
                  {
                   System.out.print(intQueue1.front() + " ");
                   intQueue1.deleteQueue();
                }
                
                }
                }
                when I run this it works properly.

                Comment

                • satan
                  New Member
                  • Oct 2006
                  • 28

                  #9
                  Originally posted by r035198x
                  What are you saying is not working?

                  Code:
                  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.
                  }
                    
                  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);
                  }
                  }
                  
                  class QueueException extends RuntimeException
                  {
                  public QueueException()
                  {
                  }
                  public QueueException(String msg)
                  {
                  super(msg);
                  }
                  }
                  
                  class QueueOverflowException extends QueueException
                  {
                  public QueueOverflowException()
                  {
                  super("Queue Overflow");
                  }
                  public QueueOverflowException(String msg)
                  {
                  super(msg);
                  }
                  }
                  
                  class QueueUnderflowException extends QueueException
                  {
                  public QueueUnderflowException()
                  {
                  super("Queue Underflow");
                  }
                  public QueueUnderflowException(String msg)
                  {
                  super(msg);
                  }
                  }
                  
                  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 LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
                  {
                  LinkedQueueClass intQueue1 = new LinkedQueueClass();
                     while(!otherQueue.isEmptyQueue())
                    {
                    intQueue1.addQueue(otherQueue.front());
                    otherQueue.deleteQueue();
                  }
                  return intQueue1;
                  
                  }
                  }
                  
                  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("Here");
                  System.out.print("intQueue2 elements: ");
                  while(!intQueue2.isEmptyQueue())
                  {
                  System.out.print(intQueue2.front() + " ");
                  intQueue2.deleteQueue();
                  }
                  
                    //intQueue2 is now empty
                    intQueue2.addQueue(new IntElement(32));
                    intQueue2.addQueue(new IntElement(54));
                  intQueue2.addQueue(new IntElement(83));
                  
                  intQueue1 = intQueue1.copyQueue(intQueue2);
                  System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
                  
                    while(!intQueue1.isEmptyQueue())
                    {
                     System.out.print(intQueue1.front() + " ");
                     intQueue1.deleteQueue();
                  }
                  
                  }
                  }
                  when I run this it works properly.
                  Yeah! it's running properly, thanks for your help. But the way you wrote the method copyQueue is not the same thing the problem asks me to do

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by satan
                    Yeah! it's running properly, thanks for your help. But the way you wrote the method copyQueue is not the same thing the problem asks me to do
                    How does the problem want you to write the copyQueue method then?There are several ways of doing it.

                    Comment

                    • satan
                      New Member
                      • Oct 2006
                      • 28

                      #11
                      Originally posted by r035198x
                      How does the problem want you to write the copyQueue method then?There are several ways of doing it.
                      This is the heading for the method copyQueue

                      Code:
                      /Method to make a copy of otherQueue.
                              //Postcondition: A copy of otherQueue is created and
                              //               assigned to this queue.
                          public void copyQueue(LinkedQueueClass otherQueue)

                      Comment

                      Working...