My most recent assignment in Java involves me designing and making a program to run a mock Printer queue using dynamic arrays. This is the code I'm using for the main bit of the program. It's not the object itself but it has all the methods that a text based menu or applet (I have to do both) will use. This is pretty much the extent of my ability at the moment. Is there anything here I can use in Games Development? //===================================================================== // MODULE: Queue // PRODUCT: Printer Queue System // VERSION: 2.0 // AUTHOR: Scott Atkins // DESCRIPTION: This program is to allow a user to create an instance of a printer queue and // add file, remove a file, display the entire queue, search for a specific file and // delete a specific file. //===================================================================== public class Queue // Dynamic Version { QueueNode start ; QueueNode end ; //===================================================================== // NAME: Queue // // RETURN TYPE: None // // PARAMETERS: // // // DESCRIPTION: This method creates two instances of QueueNode and sets their values to null. // // start: This is an instance of QueueNode using the dynamic array it // is the starting point for all nodes within the queue. // end: This is an instance of QueueNode and is the ending point for // the chain of odes within the dynamic array. // //===================================================================== public Queue () { start = null ; end = null ; } //===================================================================== // NAME: reset // // RETURN TYPE: None // // PARAMETERS: None // // DESCRIPTION: This method sets both start and end to null, by doing so the nodes within the array are // no longer being used and thus will be deleted. // //===================================================================== public void reset() { start = null ; end = null ; } //===================================================================== // NAME: add // // RETURN TYPE: Nonen // // PARAMETERS: // ownerName: This is the name of the owner of the document to be added to the queue // // docName: This is the name of the document to be added to the queue. // // size: This is the size of the document to be added to the queue. // // // DESCRIPTION: This method will take a given owner name, document name and document size and add it to // the printer queue. The method will check if the queue is empty and if it is, will add the document // as the first in the queue. If it is not the first, it will be added as an additional Node. // //===================================================================== public void add ( String ownerName, String docName, int size ) { if ( start == null ) // If the queue is empty { QueueNode temp = new QueueNode() ; temp.ownerName = ownerName; temp.docName = docName; temp.size = size ; temp.next = null ; temp.previous = null ; start = temp; end = temp ; } else // Add a new node to the // end of the queue { QueueNode temp = new QueueNode() ; temp.ownerName = ownerName; temp.docName = docName; temp.size = size ; temp.next = end ; temp.previous = null; // ??? end.previous = temp ; end = temp ; } } //===================================================================== // NAME: remove // // RETURN TYPE: String // // PARAMETERS: None // // DESCRIPTION: This method will remove the Node that has been n the queue the longest. // It will remove it from the queue and return its contents. The method must // check whether the queue only has one value, if it does, end is set to null. If it has more // than one value it will change starts contents to that of the next node in the queue. // //===================================================================== public String remove () { QueueNode temp = new QueueNode() ; String printed = ""; if ( start == null ) return start; // Queue empty! else if ( start.previous == null) // Only one node in queue { temp = start ; start = null ; end = null; // queue now empty ??? printed = printed + temp.ownerName + "\t\t" + temp.DocName + "\t\t" + temp.size + "\n"; return printed; ; } else // More than one node in queue { temp = start ; start = start.previous ; start.next = null; // ??? printed = printed + temp.ownerName + "\t\t" + temp.DocName + "\t\t" + temp.size + "\n"; return printed; } } //===================================================================== // NAME: displayAll // // RETURN TYPE: String // // PARAMETERS: None // // DESCRIPTION: This method will print out the entire contents of the Printer Queue. If the queue is // empty the method will print that it is empty. If the queue is not empty it will loop // a set of print statements that print the entire contents of the queue in a readable table // formet. // //===================================================================== public String displayAll () { QueueNode temp = start; String entireQueue = "\n"; if (temp == null) { entireQueue = "Queue is empty"; } else { entireQueue = "Owner Name\tDoc Name\tSize\n"; while ( temp != null ) { entireQueue = entireQueue + temp.ownerName + "\t\t" + temp.docName + "\t\t" + temp.size + "\n"; temp = temp.previous ; } } return entireQueue; } //===================================================================== // NAME: findDoc // // RETURN TYPE: Boolean // // PARAMETERS: // docName: This is a String containing the name of a document that the user wishes // to search for. // // DESCRIPTION: This method will search the queue for a specific document name and will retuna booloean value // that tells whether the file was found or not. // //===================================================================== public boolean findDoc(String docName) { QueueNode temp = start; boolean valueFound = false; if (temp == null) { System.out.println("\n\nQueue is empty"); } else { while ((temp != null) && !valueFound) { if (temp.docName == docName) valueFound = true; else temp=temp.previous; } } return valueFound; } //===================================================================== // NAME: removeDoc // // RETURN TYPE: Boolean // // PARAMETERS: // docName: This is a String containing the name of a document that the user wishes // to delete. // // DESCRIPTION: This method will search the queue for a specific document name. If it is found, the document // will be deleted and a true boolean value returned. If it is not, nothing will happen but a // false boolean value being returned. // Due to the program having faults if less than 3 values are in the queue, the method // checks if the queue has one or two values, then searches and deletes accordingly. Otherwise // the method will assume there are more than 3 values and searches for and deletes the file // //===================================================================== public boolean removeDoc(String docName) { QueueNode temp = start; QueueNode before; QueueNode after; boolean valueFound = false; if ( temp == null) { System.out.println("Queue is empty"); } else if (temp.previous == null) { if (temp.docName == docName) { temp = start ; start = null ; end = null; // queue now empty ??? valueFound = true; } } else if (temp.previous.previous == null) { temp = start; if (temp.docName == docName) { start = start.previous ; start.next = null; // ??? valueFound = true; } else if (temp.previous.docName == docName) { end=end.next; end.previous = null; valueFound = true; } } else { while ((temp != null) && !valueFound) { if (temp.docName == docName) { before= temp.previous; after = temp.next; before.next = temp.next; after.previous= temp.previous; valueFound = true; } else temp = temp.previous; } } return valueFound; } //===================================================================== // NAME: howMany // // RETURN TYPE: Integer // // PARAMETERS: // none // // DESCRIPTION: This method will count how many documents are in the queue and return this // value as an integer. // //===================================================================== public int howMany() { int count = 0; QueueNode temp = start; if (temp==null) return 0; else { while (temp != null) { count++; temp=temp.previous; } return count; } } //===================================================================== // NAME: totalSize // // RETURN TYPE: Integer // // PARAMETERS: // none // // DESCRIPTION: This method will count the total size of all documents in the queue and // return this value as an integer. // //===================================================================== public int totalSize() { int count = 0; QueueNode temp = start; if (temp==null) return 0; else { while (temp != null) { count = count + temp.size; temp=temp.previous; } return count; } } } |