?
Solved

Problems with code...

Posted on 2002-05-09
6
Medium Priority
?
182 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 800 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

765 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