HW 1 - A Trip Planner
SETUP
Note that in this assignment, you will have to use the eclipse IDE. To start:
- Download cse214_hw1_fall2009.zip and save it to a directory of your choice. Don't unzip the file! You'll need to remember where you put it.
- Open eclipse now, and choose whatever workspace directory you like.
- Click on:
- File
- Import
- General
- Existing Projects into Workspace
- Next
- Select Archive File
- Browse
- Now find and select the cse214_hw1_fall2009.zip file you just downloaded and click OK
- Finish
- You should now have a project with all the files necessary to do this homework, including image and data files.
- Verify that the project was properly imported into eclipse. Are you able to open and edit the source code in eclipse?
- NOTE: There should be no compiler errors after you import the zip file
Making and Using A Trip List
A linked list is a useful data structure for managing sequential data where you may have to periodically add data at the start or at the end or remove data from the start or end, and where random access of individual pieces of data is not common. In fact, linked lists are typically used in such a way that all the data gets processed at once, in order, from start to finish, which we'll do using an iterator. In this assignment, you are going to define methods for manipulating a singly linked list that can store data of any Object type. You will then see one potential use of a linked list, for managing stops on a trip.
Examine the following 4 files:
HW1TestProgram.javaSinglyLinkedList.javaTripCity.javaTripPlanner.jav
Go through the code of these classes and read the documentation. TripPlanner has a main method. Try running it. It will display a GUI (Graphical User Interface) and will provide controls to test your linked list methods. In addition, I have provided a test class called HW1TestProgram. This class tests some (not all) of your methods in a few simple ways, and you can and should add further tests to this class to test your methods.
A singly linked list - The SinglyLinkedList class manages a linked list of Objects, meaning it can store any type of data. The TripPlanner class uses it to manage TripCity objects, while the HW1TestProgram program demonstates using it to manage Strings. The linked list you'll be manipulating has both a head node and a tail node, and you must make sure to properly maintain these instance variables at all times. In addition, the class has a size instance variable which you must make sure to maintain. In this assignment, you are going to write methods in this class for manipulating nodes in various ways. Remember to carefully handle all special cases.
Your 10 methods inside SinglyLinkedList
public void addFirst(Object dataToAdd)- Inserts the specified element at the beginning of this list.public void addLast(Object dataToAdd)- Appends the specified element to the end of this list.public void addAll(Collection c)- Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.public void addAll(int index, Collection c)- Inserts all of the elements in the specified collection into this list, starting at the specified position.public boolean contains(Object o)- Returns true if this list contains the an object equivalent to the specified element, false otherwise.public Object get(int index)- Returns the element at the specified position in this list.public void removeFirstOccurrence(Object o)- Removes the first occurrence of the specified element in this list (when traversing the list from head to tail).public void removeLastOccurrence(Object o)- Removes the last occurrence of the specified element in this list (when traversing the list from head to tail).public void set(int index, Object o)- Replaces the element at the specified position in this list with the specified element.public Object[] toArray()- Returns an array containing all of the elements in this list in proper sequence (from first to last element).
What's an Iterator? Iterators are a common tool for traversing through all the data in order in a linked list in both the Java and C++ programming languages. In addition to defining methods for linked list manipulation, in this assignment you will define a custom iterator, ListIterator which is an inner class of SinglyLinkedList, and which implements Java's Iterator interface. This interface has 3 methods, hasNext, next, and remove, though we really only care about hasNext and next. Anytime one wishes to go through all the elements in a linked list, one can get a custom iterator object (i.e. call the listIterator method) and as long as there are more items to fetch (hasNext is true), we can fetch the current item and move on to the next one.
Note that an iterator is just a disposable object used to go through a linked list once. Each time one wishes to go through all the elements in the list, one will have to construct a brand new iterator object. So how should these methods work? Well, my TripPlanner class, for example, uses your iterator to go through all the list data in order to draw the trip onto the map. In the HW1TestProgram we use an iterator to print out a textual representation of each piece of data in the list. We can do so by writing:
Iterator it = linkedList.listIterator();
while(it.hasNext())
{
Object obj = it.next();
System.out.println(obj);
}
Your 2 methods inside ListIterator, which is an inner class defined inside SinglyLinkedList
public boolean hasNext()- This method lets us know if there are more elements for this iterator instance to produce. If there are more elements that haven't been produced,true, elsefalseis returned.public Object next()- This method should return a single piece of data from the linked list. It should also and advance the iterator so the next time it's called it produces the next piece of data. The first time next is called, it should return the data in the head. The second time it's called it should produce the data in the second node, and so on. So how will you know which data to return? Well, you can add instance variables to the ListIterator so that each constructed iterator always knows where it is, meaning what data is to be produced next.
SAMPLE OUTPUT
As I mentioned, you can and should add further tests to the HW1TestProgram program. This does some basic tests, but does not do so thoroughly. As it exists, running the program should produce the following output:
Current List:
A->C->D->C->null
Current List:
C->D->C->null
Current List:
A->B->A->C->A->B->null
Current List:
A->B->B->A->null
As for the TripPlanner program, if your linked list methods are defined properly, it should allow one to create a trip, adding, inserting, and removing nodes. This trip would then be drawn over the map. One such trip might involve the folloing stops:
0: Austin
1: Boston
2: Dallas
3: Denver
4: El Paso
5: Houston
6: Indianapolis
7: Jacksonville
8: Los Angeles
9: New Orleans
10: New York City
11: Phoenix
12: San Francisco
13: Seattle
14: Washington D. C.
For such a trip, the program would draw the following:
Handin Instructions
- Go to the hand-in instructions page for the details on the required handin procedure. You must hand in by the designated due date and time in the course schedule for credit. Late assignments will not be graded.
Grading
All assignment will be graded purely based on program performance. Submissions that do not compile will receive 0 points. Submissions that never produce correct results will receive 0 points. The TAs will test your submissions using various input, and grades will be awarded depending on how often your code produces the correct results.
Web page created and maintained
by Richard McKenna