Help Wanted

What I can do. – skynes

Skynes
Member

Posts: 202
From: Belfast, N Ireland
Registered: 01-18-2004
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;
}
}
}

MadProf
Member

Posts: 181
From: Larnaka, Cyprus
Registered: 01-24-2001
yep! you can _always_ use "//" in games development. its kinda scary how many people forget though. comments rule.

seriously though, pretty much anything can be used in games development. I don't quite grok your question though, are you asking if any of that specific code could be used? or the skills and concepts you are using? if code, well, yes, if you needed to have a list/queue, you could use this. you can use the concepts you are using (classes, methods, etc) in programming of any type or language (with the possible exception of intercal). Its all useful stuff to learn, and helps!

Mad "Just ignore me" Prof

------------------
7 days without prayer makes one weak.

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
quote:

My most recent assignment in Java involves me designing and making a program to run a mock Printer queue using dynamic arrays.

Umm, after looking at the code - where's the array?

Your code still needs quite a bit of work - but yes, the ideas can be used in game programming.

------------------
There are only 10 types of people - those who understand binary, and those who don't.
Switch Mayhem now available! Get it here
Codename: Roler - Writing object code and GUI.

Skynes
Member

Posts: 202
From: Belfast, N Ireland
Registered: 01-18-2004
its an object called QueueNode

QueueNode itself has

class QueueNode
{
public String ownerName;
public String docName;
public int size;
public QueueNode next;
public QueueNode previous;
}

I meant was both this specific code and these general ideas.

MadProf
Member

Posts: 181
From: Larnaka, Cyprus
Registered: 01-24-2001
thats a linked list, not an array. (i think).

an array looks something like

int array[200];

linked lists are pretty cool though. Some people hate them, I believe.
Same as most programming issues. Wherever there is more than 0 ways to do something, some people will love it, and others will hate it.

Dan

------------------
7 days without prayer makes one weak.

Skynes
Member

Posts: 202
From: Belfast, N Ireland
Registered: 01-18-2004
Its called a Dynamic Array. Its one that changes size depending on how much information is in it. A standard array is stuck at one size and takes up the same space ALL the time. A dynamic array is only as big as the data in it.
CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
It's still a linked list, not an array, IMHO. An array should be accessible in O(1) time.

My idea of a dynamic array is a class that wraps itself around an array, does bounds checking, and automatically resizes the array if it overflows.

Resizing can be accomplished by creating a new, larger array and copying the old one to the new one. To prevent the resizing from happening too many times, usually the size is doubled each time it needs to resize.

------------------
There are only 10 types of people - those who understand binary, and those who don't.
Switch Mayhem now available! Get it here
Codename: Roler - Writing object code and GUI.

Skynes
Member

Posts: 202
From: Belfast, N Ireland
Registered: 01-18-2004
My class was told it's called a Dynamic Array so thats just what I call it :P
CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
Well, if that's what they call it, OK. In any case, you don't need random access if all you're doing is emulating a printer queue anyway.

------------------
There are only 10 types of people - those who understand binary, and those who don't.
Switch Mayhem now available! Get it here
Codename: Roler - Writing object code and GUI.