I have given you the following java files. (Get them here.)
(The a3files.zip contains all the .java files and this page, for easy downloading.)
I chose to implement SortedList in a file called OrderedList.java. I will then call my applet file OrderedListApplet.java, and there I will do all my applet GUI stuff. OrderedListApplet will have an instance of OrderedList that I will use to store my sorted numbers.
You do not have to use these same names! You can call your two files whatever you want to; I don't care. You can even implement SortedList and your applet in the same file (though I really don't recommend it).
SortedList takes Comparable objects. SortedList does not implement Comparable itself; it just sorts Comparable objects.
When an object is Comparable, it means that it has implemented the Comparable interface. And that means it has the method compareTo. (You can, as always, learn more of the technical details in the Java2 API.) All that compareTo does is compare two objects of the same class and tell you which object should come before the other in a sorted list. If you compare two Strings, you can tell which comes before the other alphabetically. If you compare two Integers, it will tell you which number is less than the other.
More specifically, when you call compareTo, it compares the object it was invoked on to the object given as an argument, and returns an int telling you which should come first in a list. Example:
obj1.compareTo(obj2);
If obj1 comes before obj2 in a list, compareTo will return a number < 0 (probably -1). If obj2 comes first, it turns a number > 0 (probably +1). If the two would share the same position, compareTo returns 0.
obj1 ? obj2 | obj1.compareTo(obj2) returns: |
---|---|
> | > 0 |
< | < 0 |
== | == 0 |
So, if you were comparing ints stored in Nodes, you might have the following code to iterate along the linked list, looking for where to insert a new item:
while (curr != null && curr.getItem() <= item){
curr = curr.getNext();
}
Instead, you could do the same thing with this code:
while (curr != null &&
((Comparable) curr.getItem()).compareTo(item) <= 0){
curr = curr.getNext();
}
This gets the Item out of curr, casts it to a Comparable object (see below for why you need to do this), and then compares it to the given item.
So why bother with this extra complexity? Because now your SortedList will sort and hold any kind of object that can be compared to another. It will still take ints if you wrap them an Integer object. (So to store the int 3 in the list, you can just write list.add(new Integer(3));
) But now, without changing anything in SortedList, it will also sort many other kinds of Comparable objects: Dates, Floats, Strings, Characters, etc. You could make your Employees from assignment 1 implement Comparable by simply writing a compareTo method detailing when one Employee should come before another (perhaps based on their name or ID number). Then you could store and sort Employees without rewriting SortedList.
(Footnote: obj1.compareTo(obj2) == 0 does not necessarily mean obj1.equals(obj2), though it is usually true. As with the Employee example, if they both have the same ID or name, they might be "obj1.compareTo(obj2) == 0". But they're probably only "obj1.equals(obj2)" if every field--ID, name, payPerHour, hoursWorked--are the same. It really depends on how the equals and compareTo methods for those classes were written.)
Whenever you get an item out of a Node (as with curr.getItem()
), you get an Object back. This is because Node's getItem()
method returns an Object.
You will have to cast this back to Comparable before you can call compareTo on it:
Comparable item = (Comparable) curr.getItem();
This is just like Vectors. Vectors and Nodes will both store any kind of object, but you need to cast the Objects back to their specific class when you get them out. (You could change the Node to work only with Comparable objects, but that means you could not use those same Nodes for an unsorted linked list that stores Objects. So it's better to just cast a few times and have reusable code on many levels.)
Is something here unclear? Email me or Blanca.
javac2
and run your program with java2
. Also, most (older) browsers are still using Java 1.1 runtime environments. So you'll need to use appletviewer
or a newer browser or a browser with a recent Java Runtime Environment plug-in for your applet to work.
int num = Integer.parseInt(userEnteredString).
ints can only hold numbers up to 2^31 (about 2.1 billion). You can use longs instead, which are good up to 2^63. Better yet, when you catch the NumberFormatException for when the user enters something that is not a number, also mention that the number may be too long:
Error: Either you did not enter a number or your number is too large.
listDisplay.setEnabled(false);
use:
listDisplay.setEditable(false);
This will allow the area to still scoll, but the user won't be able to change the contents.
OrderedList list = new OrderedList();
...
list.add(new Integer(num));
In OrderedList.java is where you implement the add method. To do so,
you need to check three cases:list.remove(1)
will remove the first/smallest item in the list, and list.remove(list.size())
will return the last/largest item in the list.)
~ztomasze Index :
TA Details: ICS211:
Assignment 3: Help http://www2.hawaii.edu/~ztomasze |
Last Edited: 19 Oct 2003 ©2002 by Z. Tomaszewski. |