Solved

Problems with code...

Posted on 2002-05-09
6
181 Views
Last Modified: 2010-03-31
I seem to be having problems getting the following classes to do what they are supposed to do.

********************************************************************************************************************

class QueueNode
{
      public int place, size, totalSize;            // The data for each node
      public String name;                                    //  "   "    "   "    "
      public QueueNode next;            // The node after the current one
      public QueueNode previous;      // The node before the current one
}

********************************************************************************************************************
import uuInOut;

class Queue            // Dynamic implementation of a queue
{
      QueueNode start;
      QueueNode end;
      public int totalSize, choice, yesNo;
      public char delName;
      
      public Queue()
      {
            totalSize = 0;
            start = new QueueNode();
            start = null;
            end = new QueueNode();
            end = null;
      }
      
      public void add ()
      {
            if (start==null)       // Is the queue empty?
            {
                  start = new QueueNode();
                  start.place = 1;                                    // Set's the place of the new node
                  System.out.println ("Please enter the document's name: ");
                  start.name =uuInOut.ReadString();
                  System.out.println ("Please enter the document's size: ");
                  start.size = uuInOut.ReadInt();                  // Updates the size of the queue.
                  totalSize = totalSize + start.size;            // Adds the size of the new document
                                                                              // to the total size of the queue.
                  start.next = null;
                  start.previous = null;
                  end = start;
            }
            
            else
            {
                  QueueNode temp = new QueueNode();
                  temp.place = temp.place + 1;                        // Sets the place of the new Node
                  System.out.println ("Please enter the document's name: ");
                  temp.name= uuInOut.ReadString();
                  System.out.println ("Please enter the size of the document: ");
                  temp.size = uuInOut.ReadInt();
                  totalSize = totalSize + temp.size;      // Updates the size of the queue.
                  temp.next = end;                              // Updates where the next node is.
                  temp.previous = temp;                        // Updates where the previous node is.
                  end= temp;                                          // Temp is now the last node in the Queue
            }
      }
      
      public boolean isEmpty()
      {
            return (start==null);
      }
      
      public void remove()
      {
            QueueNode test = new QueueNode();
            
            if (this.isEmpty())
            {
                  System.out.println("Underflow error, the queue is already empty!");
                  displayMenu();
            }
            else
            {            
                  System.out.println (" Please enter the name of the document to be deleted: ");
                  delName = uuInOut.ReadChar();
                  while (test.name!=delName)
                  {
                        if (start.previous==null)
                        {
                              test = start;                                    // Remember the node to be removed
                              start = null;                                    // Update the start of the queue
                              totalSize = totalSize - test.size;      // Updates the size of the queue
                        }
                        else
                        {            
                              test = start;                                    // Remember the node to be removed
                              start = start.previous;                        // Update the start of the queue
                              totalSize = totalSize - test.size;      // Updates the size of the queue
                        }
                  }
            }
      }
                  
      
      public void displayMenu()
      {
            System.out.println("\n\t1.\tPrint a document.");
            System.out.println("\t2.\tAdd a document to the Queue.");
            System.out.println("\t3.\tRemove a document from the Queue.");
            System.out.println("\t4.\tDisplay all documents within the Queue.");
            System.out.println("\t5.\tDisplay the total size of the Queue.");
            System.out.println("\t6.\tPurge Print Queue (WARNING: This is a permanent");
            System.out.println("\t\t  operation and cannont be undone.");
            System.out.println("\n\t9.\tExit the Program.");
            System.out.println("\n\nWhich option do you require: ");
            System.out.flush();
      }
      
      public void purge()
      {
            QueueNode test = new QueueNode();
            if (this.isEmpty())
            {
                  System.out.println("Underflow error, the queue is already empty!");
                  displayMenu();
            }
            else
            {
                  System.out.println("Are you sure you want to clear the queue?");
                  System.out.println("Enter 1 (Yes) to continue or press enter (No) to return to the menu");
                  yesNo = uuInOut.ReadInt();
                  if (yesNo==1)
                  {
                        totalSize = 0;
                        start = new QueueNode();
                        start = null;
                        end = new QueueNode();
                        end = null;
                  }
                  else
                        displayMenu();      
            }
      }
      
      public void displayAll()
      {
            QueueNode test = new QueueNode();
            while (test !=null)
            {
                  System.out.println("Document Name: "+test.name);
                  System.out.println("Document Size: "+test.size);
                  test = test.previous;
            }
      }
}
********************************************************************************************************************

class TestQueue extends Queue
{
      public static void main (String[] args)
      {
            int choice;
            String yesNo;
            Queue test =new Queue();
            
            test.displayMenu();
            choice = uuInOut.ReadInt();
            while (choice != 9)
            {
                  switch(choice)
                  {
                        case 2: test.add();
                                    break;
                        case 3:      test.remove();
                                    break;
                        case 4: test.displayAll();
                                    break;
                        case 5: System.out.println("The total size of the queue is:"+test.totalSize);
                                    break;
                        case 6: test.purge();      
                                    break;
                         default: System.out.println ("Error invalid selection please try again.");
                  }
            System.out.println("\nWhich option do you require: ");
            choice = uuInOut.ReadInt();
            }
      }
}
********************************************************************************************************************

The menu in the class TestQueue contains what functions the menu should do.  

Any and all help fixing this would be extremely appreciated.
0
Comment
Question by:marksmall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 6998592
 Firsly this is a dublicate question. First award the points (or close the other question) and then open a new one.

  Second, what exactly is this supposed to do? Error messages you are getting?
0
 
LVL 35

Expert Comment

by:girionis
ID: 6998598
 Sorry, I confused your question with a similar one...
0
 
LVL 35

Expert Comment

by:girionis
ID: 6998690
 Sorry, I confused your question with a similar one...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:marksmall
ID: 6998780
This is different to the previous question in that this is me using a friends acccount.

This is supposed to be a variable size printer queue with the options of adding a document, removing one from any point in the queue, purging the queue, printing a document (same as deleting), display all the documents in the queue and display the total size of the queue.

There are no problems in adding documents to the queue or displaying the total size of the queue, but when i ask it to display all then there is a problem.  

In the remove class it removes the first document in the queue and then when you try to remove more (if there are more inserted into the queue) it comes up with the underflow message despite the totalSize integer remaing as it should.  I'm tearing my hair out trying to figure out how to get it to work.

Please save me and my hair.
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
ID: 6998903
in your add routine:

         else
          {
               QueueNode temp = new QueueNode();
               temp.place = temp.place + 1;                    // Sets the place of the new Node
               System.out.println ("Please enter the document's name: ");
               temp.name= uuInOut.ReadString();
               System.out.println ("Please enter the size of the document: ");
               temp.size = uuInOut.ReadInt();
               totalSize = totalSize + temp.size;     // Updates the size of the queue.
               temp.next = end;                         // Updates where the next node is.
               temp.previous = temp;                    // Updates where the previous node is.
               end= temp;                                   // Temp is now the last node in the Queue
          }

temp.previous = temp should be temp.previous = null (you are adding it to end of the queue, there should be nothing after it right?

in remove()
              while (test.name!=delName)
               {
                    if (start.previous==null)
                    {
                         test = start;                              // Remember the node to be removed
                         start = null;                              // Update the start of the queue
                         totalSize = totalSize - test.size;     // Updates the size of the queue
                    }
                    else
                    {          
                         test = start;                              // Remember the node to be removed
                         start = start.previous;                    // Update the start of the queue
                         totalSize = totalSize - test.size;     // Updates the size of the queue
                    }


also, in your remove, you have while (test.name!=delName)
what if test.name never equals delName (infinite loop)... you'll probably want to check and 'break' out of the loop if there aren't any more nodes to check.

also, just a sidenote:
in your Constructor you have,
 {
          totalSize = 0;
          start = new QueueNode(); // WHY BOTHER DOING THIS?
          start = null; // WHEN YOU CHANGE IT HERE?
          end = new QueueNode(); // OR THIS
          end = null; // FOR THE SAME REASON
     }


you're remove doesn't look quite right either... i have to do something else right now (my own job), but i'll take a look at it in a little bit.

good luck!
0
 

Author Comment

by:marksmall
ID: 7000675
Thanks a load.  Got it all sorted.
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question