Solved

Problems with code...

Posted on 2002-05-09
6
180 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
null output 3 55
jsp login check 12 62
Facing this issue for maven proxy setting 2 43
Need Help! Getting a syntax error and don't understand why 3 54
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

737 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