Solved

Problems with code...

Posted on 2002-05-09
6
176 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 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

21 Experts available now in Live!

Get 1:1 Help Now