Solved

Problems with code...

Posted on 2002-05-09
6
177 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Impossible to extract MSI from new JAVA releases 2 47
get weblogic logged in user in java 2 57
ArrayIndexOutOfBoundException 9 67
map related example 6 32
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now