473,770 Members | 4,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help

28 New Member
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


Expand|Select|Wrap|Line Numbers
  1. public abstract class DataElement
  2. {
  3.     public abstract boolean equals(DataElement otherElement);
  4.       //Method to determine whether two objects contain the
  5.       //same data.
  6.       //Postcondition: Returns true if this object contains the
  7.       //               same data as the object otherElement;
  8.       //               otherwise, it returns false.
  9.  
  10.     public abstract int compareTo(DataElement otherElement);
  11.       //Method to compare two objects.
  12.       //Postcondition: Returns a value < 0 if this object is
  13.       //                    less than the object otherElement;
  14.       //               Returns 0 if this object is the same as
  15.       //                    the object otherElement.
  16.       //               Returns a value > 0 if this object is
  17.       //                  greater than the object otherElement.
  18.  
  19.     public abstract void makeCopy(DataElement otherElement);
  20.       //Method to copy otherElement into this object.
  21.       //Postcondition: The data of otherElement is copied into
  22.       //               this object.
  23.  
  24.     public abstract DataElement getCopy();
  25.       //Method to return a copy of this object.
  26.       //Postcondition: A copy of this object is created and
  27.       //               a reference of the copy is returned.
  28. }
  29.  
  30.  
  31.  
  32. public class IntElement extends DataElement
  33. {
  34.     protected int num;
  35.  
  36.       //default constructor
  37.     public IntElement()
  38.     {
  39.         num = 0;
  40.     }
  41.  
  42.       //constructor with a parameter
  43.     public IntElement(int x)
  44.     {
  45.         num = x;
  46.     }
  47.       //copy constructor
  48.     public IntElement(IntElement otherElement)
  49.     {
  50.         num = otherElement.num;
  51.     }
  52.  
  53.       //Method to set the value of the instance variable num.
  54.       //Postcondition: num = x;
  55.     public void setNum(int x)
  56.     {
  57.         num = x;
  58.     }
  59.  
  60.       //Method to return the value of the instance variable num.
  61.       //Postcondition: The value of num is returned.
  62.     public int getNum()
  63.     {
  64.         return num;
  65.     }
  66.  
  67.     public boolean equals(DataElement otherElement)
  68.     {
  69.         IntElement temp = (IntElement) otherElement;
  70.         return (num == temp.num);
  71.     }
  72.  
  73.     public int compareTo(DataElement otherElement)
  74.     {
  75.         IntElement temp = (IntElement) otherElement;
  76.         return (num - temp.num);
  77.     }
  78.  
  79.     public void makeCopy(DataElement otherElement)
  80.     {
  81.         IntElement temp = (IntElement) otherElement;
  82.         num = temp.num;
  83.     }
  84.  
  85.     public DataElement getCopy()
  86.     {
  87.         IntElement temp = new IntElement(num);
  88.         return temp;
  89.     }
  90.  
  91.     public String toString()
  92.     {
  93.         return String.valueOf(num);
  94.     }
  95. }
  96.  
  97.  
  98.  
  99. public class QueueException extends RuntimeException
  100. {
  101.     public QueueException()
  102.     {
  103.     }
  104.  
  105.     public QueueException(String msg)
  106.     {
  107.         super(msg);
  108.     }
  109. }
  110.  
  111.  
  112.  
  113. public class QueueOverflowException extends QueueException
  114. {
  115.     public QueueOverflowException()
  116.     {
  117.          super("Queue Overflow");
  118.     }
  119.  
  120.     public QueueOverflowException(String msg)
  121.     {
  122.         super(msg);
  123.     }
  124. }
  125.  
  126.  
  127.  
  128. public class QueueUnderflowException extends QueueException
  129. {
  130.     public QueueUnderflowException()
  131.     {
  132.          super("Queue Underflow");
  133.     }
  134.  
  135.     public QueueUnderflowException(String msg)
  136.     {
  137.         super(msg);
  138.     }
  139. }
  140.  
  141.  
  142.  
  143. public class LinkedQueueClass
  144. {
  145.           //Definition of the node
  146.     protected class QueueNode
  147.     {
  148.         DataElement info;
  149.         QueueNode link;
  150.     }
  151.  
  152.     private QueueNode queueFront; //reference variable to the
  153.                                   //first element of the queue
  154.     private QueueNode queueRear;  //reference variable to the
  155.                                   //last element of the queue
  156.         //default constructor
  157.     public LinkedQueueClass()
  158.     {
  159.         queueFront = null;
  160.         queueRear  = null;
  161.     }
  162.  
  163.  
  164.         //copy constructor
  165.     public LinkedQueueClass(LinkedQueueClass otherQueue)
  166.     {
  167.         queueFront = otherQueue.queueFront;
  168.         queueRear  = otherQueue.queueRear;
  169.     }//end copy constructor
  170.  
  171.         //Method to initialize the queue to an empty state.
  172.         //Postcondition: queueFront = null; queueRear = null
  173.      public void initializeQueue()
  174.      {
  175.           queueFront = null;
  176.           queueRear = null;
  177.      }
  178.  
  179.         //Method to determine whether the queue is empty.
  180.         //Postcondition: Returns true if the queue is empty;
  181.         //               otherwise, returns false.
  182.      public boolean isEmptyQueue()
  183.      {
  184.           return (queueFront == null);
  185.      }
  186.  
  187.  
  188.         //Method to determine whether the queue is full.
  189.         //Postcondition: Returns true if the queue is full;
  190.         //               otherwise, returns false.
  191.      public boolean isFullQueue()
  192.      {
  193.           return false;
  194.      }
  195.  
  196.         //Method to return the first element of the queue.
  197.         //Precondition: The queue exists and is not empty.
  198.         //Postcondition: If the queue is empty, the method throws
  199.         //               QueueUnderflowException; otherwise, a
  200.         //               reference to a copy of the first element
  201.         //               of the queue is returned.
  202.      public DataElement front() throws QueueUnderflowException
  203.      {
  204.           if(isEmptyQueue())
  205.              throw new QueueUnderflowException();
  206.  
  207.           DataElement temp = queueFront.info.getCopy();
  208.           return temp;
  209.      }
  210.  
  211.         //Method to return the last element of the queue.
  212.         //Precondition: The queue exists and is not empty.
  213.         //Postcondition: If the queue is empty, the method throws
  214.         //               QueueUnderflowException; otherwise, a
  215.         //               reference to a copy of the last element
  216.         //               of the queue is returned.
  217.      public DataElement back() throws QueueUnderflowException
  218.      {
  219.           if(isEmptyQueue())
  220.              throw new QueueUnderflowException();
  221.  
  222.           DataElement temp = queueRear.info.getCopy();
  223.           return temp;
  224.      }
  225.  
  226.  
  227.         //Method to add queueElement to the queue.
  228.         //Precondition: The queue exists.
  229.         //Postcondition: The queue is changed and queueElement
  230.         //               is added to the queue.
  231.      public void addQueue(DataElement newElement)
  232.      {
  233.           QueueNode newNode;
  234.  
  235.           newNode = new QueueNode();  //create the node
  236.  
  237.           newNode.info = newElement.getCopy();  //store the info
  238.           newNode.link = null;   //initialize the link field to null
  239.  
  240.           if(queueFront == null) //if initially the queue is empty
  241.           {
  242.              queueFront = newNode;
  243.              queueRear = newNode;
  244.           }
  245.           else   //add newNode at the end
  246.           {
  247.              queueRear.link = newNode;
  248.              queueRear = queueRear.link;
  249.           }
  250.      }//end addQueue
  251.  
  252.  
  253.         //Method to remove the first element of the queue.
  254.         //Precondition: The queue exists and is not empty.
  255.         //Postcondition: The queue is changed and the first
  256.         //               element is removed from the queue.
  257.      public void deleteQueue() throws QueueUnderflowException
  258.      {
  259.           if(isEmptyQueue())
  260.              throw new QueueUnderflowException();
  261.  
  262.           queueFront = queueFront.link; //advance queueFront
  263.  
  264.           if(queueFront == null)  //if after deletion the queue is
  265.              queueRear = null;  //empty, set queueRear to null
  266.      } //end deleteQueue
  267.  
  268.  
  269.         //Method to make a copy of otherQueue.
  270.         //Postcondition: A copy of otherQueue is created and
  271.         //               assigned to this queue.
  272.     public void copyQueue(LinkedQueueClass otherQueue)
  273.     {
  274.       if (this != otherQueue)  //avoid self-copy
  275.           copyQueue(otherQueue);
  276.     }
  277. }
  278.  
  279.  
  280.  
  281. public class QueueProgram
  282. {
  283.     public static void main(String[] args)
  284.     {
  285.         LinkedQueueClass intQueue1 = new LinkedQueueClass();
  286.         LinkedQueueClass intQueue2 = new LinkedQueueClass();
  287.         LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
  288.         LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
  289.  
  290.  
  291.         intQueue1.addQueue(new IntElement(23));
  292.         intQueue1.addQueue(new IntElement(45));
  293.         intQueue1.addQueue(new IntElement(38));
  294.  
  295.         intQueue2.addQueue(new IntElement(32));
  296.         intQueue2.addQueue(new IntElement(54));
  297.         intQueue2.addQueue(new IntElement(83));
  298.  
  299.  
  300.         System.out.print("intQueue1 elements: ");
  301.  
  302.         while(!intQueue1.isEmptyQueue())
  303.         {
  304.             System.out.print(intQueue1.front() + " ");
  305.             intQueue1.deleteQueue();
  306.         }
  307.  
  308.         System.out.println();
  309.  
  310.         System.out.print("intQueue2 elements: ");
  311.  
  312.         while(!intQueue2.isEmptyQueue())
  313.         {
  314.             System.out.print(intQueue2.front() + " ");
  315.             intQueue2.deleteQueue();
  316.         }
  317.  
  318.         System.out.println();
  319.  
  320.         intQueue1.copyQueue(intQueue2);
  321.         System.out.print("After copying intQueue1 elements into intQueue2, intQueue2 elements are: ");
  322.  
  323.  
  324.  
  325.     }
  326. }
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.
Nov 14 '06 #1
10 2084
r035198x
13,262 MVP
Expand|Select|Wrap|Line Numbers
  1.  
  2. abstract class DataElement
  3. {
  4.     public abstract boolean equals(DataElement otherElement);
  5.       //Method to determine whether two objects contain the
  6.       //same data.
  7.       //Postcondition: Returns true if this object contains the
  8.       //               same data as the object otherElement;
  9.       //               otherwise, it returns false.
  10.     public abstract int compareTo(DataElement otherElement);
  11.       //Method to compare two objects.
  12.       //Postcondition: Returns a value < 0 if this object is
  13.       //                    less than the object otherElement;
  14.       //               Returns 0 if this object is the same as
  15.       //                    the object otherElement.
  16.       //               Returns a value > 0 if this object is
  17.       //                  greater than the object otherElement.
  18.     public abstract void makeCopy(DataElement otherElement);
  19.       //Method to copy otherElement into this object.
  20.       //Postcondition: The data of otherElement is copied into
  21.       //               this object.
  22.     public abstract DataElement getCopy();
  23.       //Method to return a copy of this object.
  24.       //Postcondition: A copy of this object is created and
  25.       //               a reference of the copy is returned.
  26. }
  27.  
  28. class IntElement extends DataElement
  29. {
  30.     protected int num;
  31.       //default constructor
  32.     public IntElement()
  33.     {
  34.         num = 0;
  35.     }
  36.       //constructor with a parameter
  37.     public IntElement(int x)
  38.     {
  39.         num = x;
  40.     }
  41.       //copy constructor
  42.     public IntElement(IntElement otherElement)
  43.     {
  44.         num = otherElement.num;
  45.     }
  46.       //Method to set the value of the instance variable num.
  47.       //Postcondition: num = x;
  48.     public void setNum(int x)
  49.     {
  50.         num = x;
  51.     }
  52.       //Method to return the value of the instance variable num.
  53.       //Postcondition: The value of num is returned.
  54.     public int getNum()
  55.     {
  56.         return num;
  57.     }
  58.     public boolean equals(DataElement otherElement)
  59.     {
  60.         IntElement temp = (IntElement) otherElement;
  61.         return (num == temp.num);
  62.     }
  63.     public int compareTo(DataElement otherElement)
  64.     {
  65.         IntElement temp = (IntElement) otherElement;
  66.         return (num - temp.num);
  67.     }
  68.     public void makeCopy(DataElement otherElement)
  69.     {
  70.         IntElement temp = (IntElement) otherElement;
  71.         num = temp.num;
  72.     }
  73.     public DataElement getCopy()
  74.     {
  75.         IntElement temp = new IntElement(num);
  76.         return temp;
  77.     }
  78.     public String toString()
  79.     {
  80.         return String.valueOf(num);
  81.     }
  82. }
  83.  
  84. class QueueException extends RuntimeException
  85. {
  86.     public QueueException()
  87.     {
  88.     }
  89.     public QueueException(String msg)
  90.     {
  91.         super(msg);
  92.     }
  93. }
  94.  
  95. class QueueOverflowException extends QueueException
  96. {
  97.     public QueueOverflowException()
  98.     {
  99.          super("Queue Overflow");
  100.     }
  101.     public QueueOverflowException(String msg)
  102.     {
  103.         super(msg);
  104.     }
  105. }
  106.  
  107. class QueueUnderflowException extends QueueException
  108. {
  109.     public QueueUnderflowException()
  110.     {
  111.          super("Queue Underflow");
  112.     }
  113.     public QueueUnderflowException(String msg)
  114.     {
  115.         super(msg);
  116.     }
  117. }
  118.  
  119. class LinkedQueueClass
  120. {
  121.           //Definition of the node
  122.     protected class QueueNode
  123.     {
  124.         DataElement info;
  125.         QueueNode link;
  126.     }
  127.     private QueueNode queueFront; //reference variable to the
  128.                                   //first element of the queue
  129.     private QueueNode queueRear;  //reference variable to the
  130.                                   //last element of the queue
  131.         //default constructor
  132.     public LinkedQueueClass()
  133.     {
  134.         queueFront = null;
  135.         queueRear  = null;
  136.     }
  137.  
  138.         //copy constructor
  139.     public LinkedQueueClass(LinkedQueueClass otherQueue)
  140.     {
  141.         queueFront = otherQueue.queueFront;
  142.         queueRear  = otherQueue.queueRear;
  143.     }//end copy constructor
  144.         //Method to initialize the queue to an empty state.
  145.         //Postcondition: queueFront = null; queueRear = null
  146.      public void initializeQueue()
  147.      {
  148.           queueFront = null;
  149.           queueRear = null;
  150.      }
  151.         //Method to determine whether the queue is empty.
  152.         //Postcondition: Returns true if the queue is empty;
  153.         //               otherwise, returns false.
  154.      public boolean isEmptyQueue()
  155.      {
  156.           return (queueFront == null);
  157.      }
  158.  
  159.         //Method to determine whether the queue is full.
  160.         //Postcondition: Returns true if the queue is full;
  161.         //               otherwise, returns false.
  162.      public boolean isFullQueue()
  163.      {
  164.           return false;
  165.      }
  166.         //Method to return the first element of the queue.
  167.         //Precondition: The queue exists and is not empty.
  168.         //Postcondition: If the queue is empty, the method throws
  169.         //               QueueUnderflowException; otherwise, a
  170.         //               reference to a copy of the first element
  171.         //               of the queue is returned.
  172.      public DataElement front() throws QueueUnderflowException
  173.      {
  174.           if(isEmptyQueue())
  175.              throw new QueueUnderflowException();
  176.           DataElement temp = queueFront.info.getCopy();
  177.           return temp;
  178.      }
  179.         //Method to return the last element of the queue.
  180.         //Precondition: The queue exists and is not empty.
  181.         //Postcondition: If the queue is empty, the method throws
  182.         //               QueueUnderflowException; otherwise, a
  183.         //               reference to a copy of the last element
  184.         //               of the queue is returned.
  185.      public DataElement back() throws QueueUnderflowException
  186.      {
  187.           if(isEmptyQueue())
  188.              throw new QueueUnderflowException();
  189.           DataElement temp = queueRear.info.getCopy();
  190.           return temp;
  191.      }
  192.  
  193.         //Method to add queueElement to the queue.
  194.         //Precondition: The queue exists.
  195.         //Postcondition: The queue is changed and queueElement
  196.         //               is added to the queue.
  197.      public void addQueue(DataElement newElement)
  198.      {
  199.           QueueNode newNode;
  200.           newNode = new QueueNode();  //create the node
  201.           newNode.info = newElement.getCopy();  //store the info
  202.           newNode.link = null;   //initialize the link field to null
  203.           if(queueFront == null) //if initially the queue is empty
  204.           {
  205.              queueFront = newNode;
  206.              queueRear = newNode;
  207.           }
  208.           else   //add newNode at the end
  209.           {
  210.              queueRear.link = newNode;
  211.              queueRear = queueRear.link;
  212.           }
  213.      }//end addQueue
  214.  
  215.         //Method to remove the first element of the queue.
  216.         //Precondition: The queue exists and is not empty.
  217.         //Postcondition: The queue is changed and the first
  218.         //               element is removed from the queue.
  219.      public void deleteQueue() throws QueueUnderflowException
  220.      {
  221.           if(isEmptyQueue())
  222.              throw new QueueUnderflowException();
  223.           queueFront = queueFront.link; //advance queueFront
  224.           if(queueFront == null)  //if after deletion the queue is
  225.              queueRear = null;  //empty, set queueRear to null
  226.      } //end deleteQueue
  227.  
  228.         //Method to make a copy of otherQueue.
  229.         //Postcondition: A copy of otherQueue is created and
  230.         //               assigned to this queue.
  231.  
  232.     public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  233.     {
  234.         LinkedQueueClass intQueue1 = new LinkedQueueClass();
  235.        while(!otherQueue.isEmptyQueue())
  236.   {
  237.       intQueue1.addQueue(otherQueue.front());
  238.       otherQueue.deleteQueue();
  239.         }
  240.     return intQueue1;
  241.  
  242.     }
  243. }
  244.  
  245. public class QueueProgram
  246. {
  247.     public static void main(String[] args)
  248.     {
  249.         LinkedQueueClass intQueue1 = new LinkedQueueClass();
  250.         LinkedQueueClass intQueue2 = new LinkedQueueClass();
  251.         LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
  252.         LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
  253.  
  254.         intQueue1.addQueue(new IntElement(23));
  255.         intQueue1.addQueue(new IntElement(45));
  256.         intQueue1.addQueue(new IntElement(38));
  257.         intQueue2.addQueue(new IntElement(32));
  258.         intQueue2.addQueue(new IntElement(54));
  259.         intQueue2.addQueue(new IntElement(83));
  260.  
  261.         System.out.print("intQueue1 elements: ");
  262.         while(!intQueue1.isEmptyQueue())
  263.         {
  264.             System.out.print(intQueue1.front() + " ");
  265.             intQueue1.deleteQueue();
  266.         }
  267.         System.out.println("Here");
  268.         System.out.print("intQueue2 elements: ");
  269.         while(!intQueue2.isEmptyQueue())
  270.         {
  271.             System.out.print(intQueue2.front() + " ");
  272.             intQueue2.deleteQueue();
  273.         }
  274.  
  275.   //intQueue2 is now empty
  276.   intQueue2.addQueue(new IntElement(32));
  277.   intQueue2.addQueue(new IntElement(54));
  278.         intQueue2.addQueue(new IntElement(83));
  279.  
  280.         intQueue1 = intQueue1.copyQueue(intQueue2);
  281.         System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
  282.  
  283.   while(!intQueue1.isEmptyQueue())
  284.   {
  285.        System.out.print(intQueue1.front() + " ");
  286.        intQueue1.deleteQueue();
  287.         }
  288.  
  289.     }
  290. }
  291.  
  292.  
  293.  
Do the same for the copy constructor
Nov 14 '06 #2
satan
28 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2. abstract class DataElement
  3. {
  4.     public abstract boolean equals(DataElement otherElement);
  5.       //Method to determine whether two objects contain the
  6.       //same data.
  7.       //Postcondition: Returns true if this object contains the
  8.       //               same data as the object otherElement;
  9.       //               otherwise, it returns false.
  10.     public abstract int compareTo(DataElement otherElement);
  11.       //Method to compare two objects.
  12.       //Postcondition: Returns a value < 0 if this object is
  13.       //                    less than the object otherElement;
  14.       //               Returns 0 if this object is the same as
  15.       //                    the object otherElement.
  16.       //               Returns a value > 0 if this object is
  17.       //                  greater than the object otherElement.
  18.     public abstract void makeCopy(DataElement otherElement);
  19.       //Method to copy otherElement into this object.
  20.       //Postcondition: The data of otherElement is copied into
  21.       //               this object.
  22.     public abstract DataElement getCopy();
  23.       //Method to return a copy of this object.
  24.       //Postcondition: A copy of this object is created and
  25.       //               a reference of the copy is returned.
  26. }
  27.  
  28. class IntElement extends DataElement
  29. {
  30.     protected int num;
  31.       //default constructor
  32.     public IntElement()
  33.     {
  34.         num = 0;
  35.     }
  36.       //constructor with a parameter
  37.     public IntElement(int x)
  38.     {
  39.         num = x;
  40.     }
  41.       //copy constructor
  42.     public IntElement(IntElement otherElement)
  43.     {
  44.         num = otherElement.num;
  45.     }
  46.       //Method to set the value of the instance variable num.
  47.       //Postcondition: num = x;
  48.     public void setNum(int x)
  49.     {
  50.         num = x;
  51.     }
  52.       //Method to return the value of the instance variable num.
  53.       //Postcondition: The value of num is returned.
  54.     public int getNum()
  55.     {
  56.         return num;
  57.     }
  58.     public boolean equals(DataElement otherElement)
  59.     {
  60.         IntElement temp = (IntElement) otherElement;
  61.         return (num == temp.num);
  62.     }
  63.     public int compareTo(DataElement otherElement)
  64.     {
  65.         IntElement temp = (IntElement) otherElement;
  66.         return (num - temp.num);
  67.     }
  68.     public void makeCopy(DataElement otherElement)
  69.     {
  70.         IntElement temp = (IntElement) otherElement;
  71.         num = temp.num;
  72.     }
  73.     public DataElement getCopy()
  74.     {
  75.         IntElement temp = new IntElement(num);
  76.         return temp;
  77.     }
  78.     public String toString()
  79.     {
  80.         return String.valueOf(num);
  81.     }
  82. }
  83.  
  84. class QueueException extends RuntimeException
  85. {
  86.     public QueueException()
  87.     {
  88.     }
  89.     public QueueException(String msg)
  90.     {
  91.         super(msg);
  92.     }
  93. }
  94.  
  95. class QueueOverflowException extends QueueException
  96. {
  97.     public QueueOverflowException()
  98.     {
  99.          super("Queue Overflow");
  100.     }
  101.     public QueueOverflowException(String msg)
  102.     {
  103.         super(msg);
  104.     }
  105. }
  106.  
  107. class QueueUnderflowException extends QueueException
  108. {
  109.     public QueueUnderflowException()
  110.     {
  111.          super("Queue Underflow");
  112.     }
  113.     public QueueUnderflowException(String msg)
  114.     {
  115.         super(msg);
  116.     }
  117. }
  118.  
  119. class LinkedQueueClass
  120. {
  121.           //Definition of the node
  122.     protected class QueueNode
  123.     {
  124.         DataElement info;
  125.         QueueNode link;
  126.     }
  127.     private QueueNode queueFront; //reference variable to the
  128.                                   //first element of the queue
  129.     private QueueNode queueRear;  //reference variable to the
  130.                                   //last element of the queue
  131.         //default constructor
  132.     public LinkedQueueClass()
  133.     {
  134.         queueFront = null;
  135.         queueRear  = null;
  136.     }
  137.  
  138.         //copy constructor
  139.     public LinkedQueueClass(LinkedQueueClass otherQueue)
  140.     {
  141.         queueFront = otherQueue.queueFront;
  142.         queueRear  = otherQueue.queueRear;
  143.     }//end copy constructor
  144.         //Method to initialize the queue to an empty state.
  145.         //Postcondition: queueFront = null; queueRear = null
  146.      public void initializeQueue()
  147.      {
  148.           queueFront = null;
  149.           queueRear = null;
  150.      }
  151.         //Method to determine whether the queue is empty.
  152.         //Postcondition: Returns true if the queue is empty;
  153.         //               otherwise, returns false.
  154.      public boolean isEmptyQueue()
  155.      {
  156.           return (queueFront == null);
  157.      }
  158.  
  159.         //Method to determine whether the queue is full.
  160.         //Postcondition: Returns true if the queue is full;
  161.         //               otherwise, returns false.
  162.      public boolean isFullQueue()
  163.      {
  164.           return false;
  165.      }
  166.         //Method to return the first element of the queue.
  167.         //Precondition: The queue exists and is not empty.
  168.         //Postcondition: If the queue is empty, the method throws
  169.         //               QueueUnderflowException; otherwise, a
  170.         //               reference to a copy of the first element
  171.         //               of the queue is returned.
  172.      public DataElement front() throws QueueUnderflowException
  173.      {
  174.           if(isEmptyQueue())
  175.              throw new QueueUnderflowException();
  176.           DataElement temp = queueFront.info.getCopy();
  177.           return temp;
  178.      }
  179.         //Method to return the last element of the queue.
  180.         //Precondition: The queue exists and is not empty.
  181.         //Postcondition: If the queue is empty, the method throws
  182.         //               QueueUnderflowException; otherwise, a
  183.         //               reference to a copy of the last element
  184.         //               of the queue is returned.
  185.      public DataElement back() throws QueueUnderflowException
  186.      {
  187.           if(isEmptyQueue())
  188.              throw new QueueUnderflowException();
  189.           DataElement temp = queueRear.info.getCopy();
  190.           return temp;
  191.      }
  192.  
  193.         //Method to add queueElement to the queue.
  194.         //Precondition: The queue exists.
  195.         //Postcondition: The queue is changed and queueElement
  196.         //               is added to the queue.
  197.      public void addQueue(DataElement newElement)
  198.      {
  199.           QueueNode newNode;
  200.           newNode = new QueueNode();  //create the node
  201.           newNode.info = newElement.getCopy();  //store the info
  202.           newNode.link = null;   //initialize the link field to null
  203.           if(queueFront == null) //if initially the queue is empty
  204.           {
  205.              queueFront = newNode;
  206.              queueRear = newNode;
  207.           }
  208.           else   //add newNode at the end
  209.           {
  210.              queueRear.link = newNode;
  211.              queueRear = queueRear.link;
  212.           }
  213.      }//end addQueue
  214.  
  215.         //Method to remove the first element of the queue.
  216.         //Precondition: The queue exists and is not empty.
  217.         //Postcondition: The queue is changed and the first
  218.         //               element is removed from the queue.
  219.      public void deleteQueue() throws QueueUnderflowException
  220.      {
  221.           if(isEmptyQueue())
  222.              throw new QueueUnderflowException();
  223.           queueFront = queueFront.link; //advance queueFront
  224.           if(queueFront == null)  //if after deletion the queue is
  225.              queueRear = null;  //empty, set queueRear to null
  226.      } //end deleteQueue
  227.  
  228.         //Method to make a copy of otherQueue.
  229.         //Postcondition: A copy of otherQueue is created and
  230.         //               assigned to this queue.
  231.  
  232.     public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  233.     {
  234.         LinkedQueueClass intQueue1 = new LinkedQueueClass();
  235.        while(!otherQueue.isEmptyQueue())
  236.   {
  237.       intQueue1.addQueue(otherQueue.front());
  238.       otherQueue.deleteQueue();
  239.         }
  240.     return intQueue1;
  241.  
  242.     }
  243. }
  244.  
  245. public class QueueProgram
  246. {
  247.     public static void main(String[] args)
  248.     {
  249.         LinkedQueueClass intQueue1 = new LinkedQueueClass();
  250.         LinkedQueueClass intQueue2 = new LinkedQueueClass();
  251.         LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
  252.         LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
  253.  
  254.         intQueue1.addQueue(new IntElement(23));
  255.         intQueue1.addQueue(new IntElement(45));
  256.         intQueue1.addQueue(new IntElement(38));
  257.         intQueue2.addQueue(new IntElement(32));
  258.         intQueue2.addQueue(new IntElement(54));
  259.         intQueue2.addQueue(new IntElement(83));
  260.  
  261.         System.out.print("intQueue1 elements: ");
  262.         while(!intQueue1.isEmptyQueue())
  263.         {
  264.             System.out.print(intQueue1.front() + " ");
  265.             intQueue1.deleteQueue();
  266.         }
  267.         System.out.println("Here");
  268.         System.out.print("intQueue2 elements: ");
  269.         while(!intQueue2.isEmptyQueue())
  270.         {
  271.             System.out.print(intQueue2.front() + " ");
  272.             intQueue2.deleteQueue();
  273.         }
  274.  
  275.   //intQueue2 is now empty
  276.   intQueue2.addQueue(new IntElement(32));
  277.   intQueue2.addQueue(new IntElement(54));
  278.         intQueue2.addQueue(new IntElement(83));
  279.  
  280.         intQueue1 = intQueue1.copyQueue(intQueue2);
  281.         System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
  282.  
  283.   while(!intQueue1.isEmptyQueue())
  284.   {
  285.        System.out.print(intQueue1.front() + " ");
  286.        intQueue1.deleteQueue();
  287.         }
  288.  
  289.     }
  290. }
  291.  
  292.  
  293.  
Do the same for the copy constructor

Please, the same what?
Nov 15 '06 #3
r035198x
13,262 MVP
Please, the same what?
Sorry, I meant to adapt the copyQueue method in writing the copy constructor.
Nov 15 '06 #4
satan
28 New Member
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.


Expand|Select|Wrap|Line Numbers
  1. //copy constructor
  2.     public LinkedQueueClass(LinkedQueueClass otherQueue)
  3.     {
  4.         queueFront = otherQueue.queueFront;
  5.         queueRear  = otherQueue.queueRear;
  6.     }//end copy constructor


Expand|Select|Wrap|Line Numbers
  1. Method to make a copy of otherQueue.
  2.         //Postcondition: A copy of otherQueue is created and
  3.         //               assigned to this queue.
  4.     public void copyQueue(LinkedQueueClass otherQueue)
  5.     {
  6.            queueFront = otherQueue.queueFront;
  7.             queueRear = otherQueue.queueRear;
  8.           }
  9. }
Nov 15 '06 #5
r035198x
13,262 MVP
U meant like this! if so i've done like that but it's not working.


Expand|Select|Wrap|Line Numbers
  1. //copy constructor
  2.     public LinkedQueueClass(LinkedQueueClass otherQueue)
  3.     {
  4.         queueFront = otherQueue.queueFront;
  5.         queueRear = otherQueue.queueRear;
  6.     }//end copy constructor


Expand|Select|Wrap|Line Numbers
  1. Method to make a copy of otherQueue.
  2. //Postcondition: A copy of otherQueue is created and
  3. // assigned to this queue.
  4. public void copyQueue(LinkedQueueClass otherQueue)
  5. {
  6. queueFront = otherQueue.queueFront;
  7. queueRear = otherQueue.queueRear;
  8. }
  9. }
No. To adapt this

Expand|Select|Wrap|Line Numbers
  1. public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  2. {
  3.   LinkedQueueClass intQueue1 = new LinkedQueueClass();
  4.    while(!otherQueue.isEmptyQueue())
  5.   {
  6.       intQueue1.addQueue(otherQueue.front());
  7.       otherQueue.deleteQueue();
  8.   }
  9.   return intQueue1;
  10.  }
  11.  
Is this not working as well?
Nov 15 '06 #6
satan
28 New Member
No. To adapt this

Expand|Select|Wrap|Line Numbers
  1. public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  2. {
  3.   LinkedQueueClass intQueue1 = new LinkedQueueClass();
  4.    while(!otherQueue.isEmptyQueue())
  5.   {
  6.       intQueue1.addQueue(otherQueue.front());
  7.       otherQueue.deleteQueue();
  8.   }
  9.   return intQueue1;
  10.  }
  11.  
Is this not working as well?
It's not working either
Nov 15 '06 #7
r035198x
13,262 MVP
It's not working either
What are you saying is not working?

Expand|Select|Wrap|Line Numbers
  1. abstract class DataElement
  2. {
  3. public abstract boolean equals(DataElement otherElement);
  4.   //Method to determine whether two objects contain the
  5.   //same data.
  6.   //Postcondition: Returns true if this object contains the
  7.   //    same data as the object otherElement;
  8.   //    otherwise, it returns false.
  9. public abstract int compareTo(DataElement otherElement);
  10.   //Method to compare two objects.
  11.   //Postcondition: Returns a value < 0 if this object is
  12.   // less than the object otherElement;
  13.   //    Returns 0 if this object is the same as
  14.   // the object otherElement.
  15.   //    Returns a value > 0 if this object is
  16.   //   greater than the object otherElement.
  17. public abstract void makeCopy(DataElement otherElement);
  18.   //Method to copy otherElement into this object.
  19.   //Postcondition: The data of otherElement is copied into
  20.   //    this object.
  21. public abstract DataElement getCopy();
  22.   //Method to return a copy of this object.
  23.   //Postcondition: A copy of this object is created and
  24.   //    a reference of the copy is returned.
  25. }
  26.  
  27. class IntElement extends DataElement
  28. {
  29. protected int num;
  30.   //default constructor
  31. public IntElement()
  32. {
  33. num = 0;
  34. }
  35.   //constructor with a parameter
  36. public IntElement(int x)
  37. {
  38. num = x;
  39. }
  40.   //copy constructor
  41. public IntElement(IntElement otherElement)
  42. {
  43. num = otherElement.num;
  44. }
  45.   //Method to set the value of the instance variable num.
  46.   //Postcondition: num = x;
  47. public void setNum(int x)
  48. {
  49. num = x;
  50. }
  51.   //Method to return the value of the instance variable num.
  52.   //Postcondition: The value of num is returned.
  53. public int getNum()
  54. {
  55. return num;
  56. }
  57. public boolean equals(DataElement otherElement)
  58. {
  59. IntElement temp = (IntElement) otherElement;
  60. return (num == temp.num);
  61. }
  62. public int compareTo(DataElement otherElement)
  63. {
  64. IntElement temp = (IntElement) otherElement;
  65. return (num - temp.num);
  66. }
  67. public void makeCopy(DataElement otherElement)
  68. {
  69. IntElement temp = (IntElement) otherElement;
  70. num = temp.num;
  71. }
  72. public DataElement getCopy()
  73. {
  74. IntElement temp = new IntElement(num);
  75. return temp;
  76. }
  77. public String toString()
  78. {
  79. return String.valueOf(num);
  80. }
  81. }
  82.  
  83. class QueueException extends RuntimeException
  84. {
  85. public QueueException()
  86. {
  87. }
  88. public QueueException(String msg)
  89. {
  90. super(msg);
  91. }
  92. }
  93.  
  94. class QueueOverflowException extends QueueException
  95. {
  96. public QueueOverflowException()
  97. {
  98. super("Queue Overflow");
  99. }
  100. public QueueOverflowException(String msg)
  101. {
  102. super(msg);
  103. }
  104. }
  105.  
  106. class QueueUnderflowException extends QueueException
  107. {
  108. public QueueUnderflowException()
  109. {
  110. super("Queue Underflow");
  111. }
  112. public QueueUnderflowException(String msg)
  113. {
  114. super(msg);
  115. }
  116. }
  117.  
  118. class LinkedQueueClass
  119. {
  120.   //Definition of the node
  121. protected class QueueNode
  122. {
  123. DataElement info;
  124. QueueNode link;
  125. }
  126. private QueueNode queueFront; //reference variable to the
  127.   //first element of the queue
  128. private QueueNode queueRear;  //reference variable to the
  129.   //last element of the queue
  130. //default constructor
  131. public LinkedQueueClass()
  132. {
  133. queueFront = null;
  134. queueRear  = null;
  135. }
  136.  
  137. //copy constructor
  138. public LinkedQueueClass(LinkedQueueClass otherQueue)
  139. {
  140. queueFront = otherQueue.queueFront;
  141. queueRear  = otherQueue.queueRear;
  142. }//end copy constructor
  143. //Method to initialize the queue to an empty state.
  144. //Postcondition: queueFront = null; queueRear = null
  145. public void initializeQueue()
  146. {
  147.   queueFront = null;
  148.   queueRear = null;
  149. }
  150. //Method to determine whether the queue is empty.
  151. //Postcondition: Returns true if the queue is empty;
  152. //    otherwise, returns false.
  153. public boolean isEmptyQueue()
  154. {
  155.   return (queueFront == null);
  156. }
  157.  
  158. //Method to determine whether the queue is full.
  159. //Postcondition: Returns true if the queue is full;
  160. //    otherwise, returns false.
  161. public boolean isFullQueue()
  162. {
  163.   return false;
  164. }
  165. //Method to return the first element of the queue.
  166. //Precondition: The queue exists and is not empty.
  167. //Postcondition: If the queue is empty, the method throws
  168. //    QueueUnderflowException; otherwise, a
  169. //    reference to a copy of the first element
  170. //    of the queue is returned.
  171. public DataElement front() throws QueueUnderflowException
  172. {
  173.   if(isEmptyQueue())
  174. throw new QueueUnderflowException();
  175.   DataElement temp = queueFront.info.getCopy();
  176.   return temp;
  177. }
  178. //Method to return the last element of the queue.
  179. //Precondition: The queue exists and is not empty.
  180. //Postcondition: If the queue is empty, the method throws
  181. //    QueueUnderflowException; otherwise, a
  182. //    reference to a copy of the last element
  183. //    of the queue is returned.
  184. public DataElement back() throws QueueUnderflowException
  185. {
  186.   if(isEmptyQueue())
  187. throw new QueueUnderflowException();
  188.   DataElement temp = queueRear.info.getCopy();
  189.   return temp;
  190. }
  191.  
  192. //Method to add queueElement to the queue.
  193. //Precondition: The queue exists.
  194. //Postcondition: The queue is changed and queueElement
  195. //    is added to the queue.
  196. public void addQueue(DataElement newElement)
  197. {
  198.   QueueNode newNode;
  199.   newNode = new QueueNode();  //create the node
  200.   newNode.info = newElement.getCopy();  //store the info
  201.   newNode.link = null;   //initialize the link field to null
  202.   if(queueFront == null) //if initially the queue is empty
  203.   {
  204. queueFront = newNode;
  205. queueRear = newNode;
  206.   }
  207.   else   //add newNode at the end
  208.   {
  209. queueRear.link = newNode;
  210. queueRear = queueRear.link;
  211.   }
  212. }//end addQueue
  213.  
  214. //Method to remove the first element of the queue.
  215. //Precondition: The queue exists and is not empty.
  216. //Postcondition: The queue is changed and the first
  217. //    element is removed from the queue.
  218. public void deleteQueue() throws QueueUnderflowException
  219. {
  220.   if(isEmptyQueue())
  221. throw new QueueUnderflowException();
  222.   queueFront = queueFront.link; //advance queueFront
  223.   if(queueFront == null)  //if after deletion the queue is
  224. queueRear = null;  //empty, set queueRear to null
  225. } //end deleteQueue
  226.  
  227. //Method to make a copy of otherQueue.
  228. //Postcondition: A copy of otherQueue is created and
  229. //    assigned to this queue.
  230.  
  231. public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  232. {
  233. LinkedQueueClass intQueue1 = new LinkedQueueClass();
  234.    while(!otherQueue.isEmptyQueue())
  235.   {
  236.   intQueue1.addQueue(otherQueue.front());
  237.   otherQueue.deleteQueue();
  238. }
  239. return intQueue1;
  240.  
  241. }
  242. }
  243.  
  244. public class QueueProgram
  245. {
  246. public static void main(String[] args)
  247. {
  248. LinkedQueueClass intQueue1 = new LinkedQueueClass();
  249. LinkedQueueClass intQueue2 = new LinkedQueueClass();
  250. LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
  251. LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
  252.  
  253. intQueue1.addQueue(new IntElement(23));
  254. intQueue1.addQueue(new IntElement(45));
  255. intQueue1.addQueue(new IntElement(38));
  256. intQueue2.addQueue(new IntElement(32));
  257. intQueue2.addQueue(new IntElement(54));
  258. intQueue2.addQueue(new IntElement(83));
  259.  
  260. System.out.print("intQueue1 elements: ");
  261. while(!intQueue1.isEmptyQueue())
  262. {
  263. System.out.print(intQueue1.front() + " ");
  264. intQueue1.deleteQueue();
  265. }
  266. System.out.println("Here");
  267. System.out.print("intQueue2 elements: ");
  268. while(!intQueue2.isEmptyQueue())
  269. {
  270. System.out.print(intQueue2.front() + " ");
  271. intQueue2.deleteQueue();
  272. }
  273.  
  274.   //intQueue2 is now empty
  275.   intQueue2.addQueue(new IntElement(32));
  276.   intQueue2.addQueue(new IntElement(54));
  277. intQueue2.addQueue(new IntElement(83));
  278.  
  279. intQueue1 = intQueue1.copyQueue(intQueue2);
  280. System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
  281.  
  282.   while(!intQueue1.isEmptyQueue())
  283.   {
  284.    System.out.print(intQueue1.front() + " ");
  285.    intQueue1.deleteQueue();
  286. }
  287.  
  288. }
  289. }
  290.  
when I run this it works properly.
Nov 15 '06 #8
satan
28 New Member
What are you saying is not working?

Expand|Select|Wrap|Line Numbers
  1. abstract class DataElement
  2. {
  3. public abstract boolean equals(DataElement otherElement);
  4.   //Method to determine whether two objects contain the
  5.   //same data.
  6.   //Postcondition: Returns true if this object contains the
  7.   //    same data as the object otherElement;
  8.   //    otherwise, it returns false.
  9. public abstract int compareTo(DataElement otherElement);
  10.   //Method to compare two objects.
  11.   //Postcondition: Returns a value < 0 if this object is
  12.   // less than the object otherElement;
  13.   //    Returns 0 if this object is the same as
  14.   // the object otherElement.
  15.   //    Returns a value > 0 if this object is
  16.   //   greater than the object otherElement.
  17. public abstract void makeCopy(DataElement otherElement);
  18.   //Method to copy otherElement into this object.
  19.   //Postcondition: The data of otherElement is copied into
  20.   //    this object.
  21. public abstract DataElement getCopy();
  22.   //Method to return a copy of this object.
  23.   //Postcondition: A copy of this object is created and
  24.   //    a reference of the copy is returned.
  25. }
  26.  
  27. class IntElement extends DataElement
  28. {
  29. protected int num;
  30.   //default constructor
  31. public IntElement()
  32. {
  33. num = 0;
  34. }
  35.   //constructor with a parameter
  36. public IntElement(int x)
  37. {
  38. num = x;
  39. }
  40.   //copy constructor
  41. public IntElement(IntElement otherElement)
  42. {
  43. num = otherElement.num;
  44. }
  45.   //Method to set the value of the instance variable num.
  46.   //Postcondition: num = x;
  47. public void setNum(int x)
  48. {
  49. num = x;
  50. }
  51.   //Method to return the value of the instance variable num.
  52.   //Postcondition: The value of num is returned.
  53. public int getNum()
  54. {
  55. return num;
  56. }
  57. public boolean equals(DataElement otherElement)
  58. {
  59. IntElement temp = (IntElement) otherElement;
  60. return (num == temp.num);
  61. }
  62. public int compareTo(DataElement otherElement)
  63. {
  64. IntElement temp = (IntElement) otherElement;
  65. return (num - temp.num);
  66. }
  67. public void makeCopy(DataElement otherElement)
  68. {
  69. IntElement temp = (IntElement) otherElement;
  70. num = temp.num;
  71. }
  72. public DataElement getCopy()
  73. {
  74. IntElement temp = new IntElement(num);
  75. return temp;
  76. }
  77. public String toString()
  78. {
  79. return String.valueOf(num);
  80. }
  81. }
  82.  
  83. class QueueException extends RuntimeException
  84. {
  85. public QueueException()
  86. {
  87. }
  88. public QueueException(String msg)
  89. {
  90. super(msg);
  91. }
  92. }
  93.  
  94. class QueueOverflowException extends QueueException
  95. {
  96. public QueueOverflowException()
  97. {
  98. super("Queue Overflow");
  99. }
  100. public QueueOverflowException(String msg)
  101. {
  102. super(msg);
  103. }
  104. }
  105.  
  106. class QueueUnderflowException extends QueueException
  107. {
  108. public QueueUnderflowException()
  109. {
  110. super("Queue Underflow");
  111. }
  112. public QueueUnderflowException(String msg)
  113. {
  114. super(msg);
  115. }
  116. }
  117.  
  118. class LinkedQueueClass
  119. {
  120.   //Definition of the node
  121. protected class QueueNode
  122. {
  123. DataElement info;
  124. QueueNode link;
  125. }
  126. private QueueNode queueFront; //reference variable to the
  127.   //first element of the queue
  128. private QueueNode queueRear;  //reference variable to the
  129.   //last element of the queue
  130. //default constructor
  131. public LinkedQueueClass()
  132. {
  133. queueFront = null;
  134. queueRear  = null;
  135. }
  136.  
  137. //copy constructor
  138. public LinkedQueueClass(LinkedQueueClass otherQueue)
  139. {
  140. queueFront = otherQueue.queueFront;
  141. queueRear  = otherQueue.queueRear;
  142. }//end copy constructor
  143. //Method to initialize the queue to an empty state.
  144. //Postcondition: queueFront = null; queueRear = null
  145. public void initializeQueue()
  146. {
  147.   queueFront = null;
  148.   queueRear = null;
  149. }
  150. //Method to determine whether the queue is empty.
  151. //Postcondition: Returns true if the queue is empty;
  152. //    otherwise, returns false.
  153. public boolean isEmptyQueue()
  154. {
  155.   return (queueFront == null);
  156. }
  157.  
  158. //Method to determine whether the queue is full.
  159. //Postcondition: Returns true if the queue is full;
  160. //    otherwise, returns false.
  161. public boolean isFullQueue()
  162. {
  163.   return false;
  164. }
  165. //Method to return the first element of the queue.
  166. //Precondition: The queue exists and is not empty.
  167. //Postcondition: If the queue is empty, the method throws
  168. //    QueueUnderflowException; otherwise, a
  169. //    reference to a copy of the first element
  170. //    of the queue is returned.
  171. public DataElement front() throws QueueUnderflowException
  172. {
  173.   if(isEmptyQueue())
  174. throw new QueueUnderflowException();
  175.   DataElement temp = queueFront.info.getCopy();
  176.   return temp;
  177. }
  178. //Method to return the last element of the queue.
  179. //Precondition: The queue exists and is not empty.
  180. //Postcondition: If the queue is empty, the method throws
  181. //    QueueUnderflowException; otherwise, a
  182. //    reference to a copy of the last element
  183. //    of the queue is returned.
  184. public DataElement back() throws QueueUnderflowException
  185. {
  186.   if(isEmptyQueue())
  187. throw new QueueUnderflowException();
  188.   DataElement temp = queueRear.info.getCopy();
  189.   return temp;
  190. }
  191.  
  192. //Method to add queueElement to the queue.
  193. //Precondition: The queue exists.
  194. //Postcondition: The queue is changed and queueElement
  195. //    is added to the queue.
  196. public void addQueue(DataElement newElement)
  197. {
  198.   QueueNode newNode;
  199.   newNode = new QueueNode();  //create the node
  200.   newNode.info = newElement.getCopy();  //store the info
  201.   newNode.link = null;   //initialize the link field to null
  202.   if(queueFront == null) //if initially the queue is empty
  203.   {
  204. queueFront = newNode;
  205. queueRear = newNode;
  206.   }
  207.   else   //add newNode at the end
  208.   {
  209. queueRear.link = newNode;
  210. queueRear = queueRear.link;
  211.   }
  212. }//end addQueue
  213.  
  214. //Method to remove the first element of the queue.
  215. //Precondition: The queue exists and is not empty.
  216. //Postcondition: The queue is changed and the first
  217. //    element is removed from the queue.
  218. public void deleteQueue() throws QueueUnderflowException
  219. {
  220.   if(isEmptyQueue())
  221. throw new QueueUnderflowException();
  222.   queueFront = queueFront.link; //advance queueFront
  223.   if(queueFront == null)  //if after deletion the queue is
  224. queueRear = null;  //empty, set queueRear to null
  225. } //end deleteQueue
  226.  
  227. //Method to make a copy of otherQueue.
  228. //Postcondition: A copy of otherQueue is created and
  229. //    assigned to this queue.
  230.  
  231. public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  232. {
  233. LinkedQueueClass intQueue1 = new LinkedQueueClass();
  234.    while(!otherQueue.isEmptyQueue())
  235.   {
  236.   intQueue1.addQueue(otherQueue.front());
  237.   otherQueue.deleteQueue();
  238. }
  239. return intQueue1;
  240.  
  241. }
  242. }
  243.  
  244. public class QueueProgram
  245. {
  246. public static void main(String[] args)
  247. {
  248. LinkedQueueClass intQueue1 = new LinkedQueueClass();
  249. LinkedQueueClass intQueue2 = new LinkedQueueClass();
  250. LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
  251. LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
  252.  
  253. intQueue1.addQueue(new IntElement(23));
  254. intQueue1.addQueue(new IntElement(45));
  255. intQueue1.addQueue(new IntElement(38));
  256. intQueue2.addQueue(new IntElement(32));
  257. intQueue2.addQueue(new IntElement(54));
  258. intQueue2.addQueue(new IntElement(83));
  259.  
  260. System.out.print("intQueue1 elements: ");
  261. while(!intQueue1.isEmptyQueue())
  262. {
  263. System.out.print(intQueue1.front() + " ");
  264. intQueue1.deleteQueue();
  265. }
  266. System.out.println("Here");
  267. System.out.print("intQueue2 elements: ");
  268. while(!intQueue2.isEmptyQueue())
  269. {
  270. System.out.print(intQueue2.front() + " ");
  271. intQueue2.deleteQueue();
  272. }
  273.  
  274.   //intQueue2 is now empty
  275.   intQueue2.addQueue(new IntElement(32));
  276.   intQueue2.addQueue(new IntElement(54));
  277. intQueue2.addQueue(new IntElement(83));
  278.  
  279. intQueue1 = intQueue1.copyQueue(intQueue2);
  280. System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
  281.  
  282.   while(!intQueue1.isEmptyQueue())
  283.   {
  284.    System.out.print(intQueue1.front() + " ");
  285.    intQueue1.deleteQueue();
  286. }
  287.  
  288. }
  289. }
  290.  
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
Nov 15 '06 #9
r035198x
13,262 MVP
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.
Nov 16 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
6328
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334, column 13: there is no attribute "SRC" <bgsound src="C:\My Documents\zingwent.mids"> You have used the attribute named above in your document, but the document type you are using does not support that attribute for this element. This error is...
5
2196
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows: Objectives The purpose of this assignment is to have you practice the design of object-oriented classes, including one or more of the following concepts
0
1840
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities, all dont work "Re:" need help "Re:need help"
9
2937
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the device
7
3306
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
15
4642
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
16
2539
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and push a submit button.
8
2750
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only the sequence nos and it should be such that i can access it through the structure .plz help me .
0
3961
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
20
4285
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.