Lab
05
DUE DATE: This lab must be completed by Sunday, 11:55PM,
October 28st. Ask your TA to verify that your
lab was submitted correctly, and get his/her help if need be before the
deadline. You have two weeks to do this lab.
NOTE – Changes were made to
this document on Thursday October 18. If
you have already completed the lab you may turn in the version you have.
Otherwise please follow the instructions in this document.
OBJECTIVE:
This
lab will give you experience using linked lists. You will learn how to build
and manipulate a linked list. You will
also learn how to implement toString() and
the implementation of the compareTo() method (Comparable Interface). This is a basic singly linked list. It does not have to be circular or doubly
linked.
YOU
MAY NOT USE THE JAVA API LIST OR COLLECTION CLASSES! You must code the list yourself.
GRADING
Total 10 points
- Build list from file ( 3 points)
- Print list ( 2
points)
- Delete member of list ( 2 points)
- Copy list in order ( 3 points)
BACKGROUND:
Review
your class notes and the chapters in your text book on linked lists. Also consult the Java API (http://java.sun.com/javase/6/docs/api/)
on the toString() method
(Object class) and the Comparable Interface.
ASSIGNMENT:
Your
program will read in a file potus.txt containing
information about the 43 Presidents of the
As
you read records in from the file create an instance of your President class
and add the object to the end of a linked list.
Your
program will then perform several manipulations of the list, printing the
contents of the list after each manipulation.
This is what the list will look like after it is
built:
Classes you should implement
Class - President.java
Contains information about each president. There will be an instance variable for each
field in the file record. Be sure you
create getters and setters for the fields.
(Note: NetBeans will do this for you. Right click, refactor,
encapsulate fields). You should declare
all your variables as private and use public methods to get them or set
them.
President must
implement Comparable.
public
Class President implements Comparable {…}
Fields
- Presidential
sequence number (int)
- Name
(String) (Last, first)
- Term
of service (String)
- Party
(String)
Methods
- Constructors
- President(int sequence, String name, String years, String
party)
- President(String
name);
- String
toString();
- int compareTo(Object
o) – This is used by the Comprable interface. It enables the use of obj1.compareTo(obj2)
to compare two objects according to how the programmer wishes them
to be compared. The comparison
method is hidden from the caller.
You implementation should compare the names of any two presidents to
decide which one sorts first alphabetically.
Note - When
generics were introduced in Java 1.5 it became possible to do the following:
public
class President implements Comparable<President>
The
signature for compareTo then becomes
int compareTo(President
p) which simplifies the coding.
- Getters
and Setters – e.g. String
getName();
or void setTerm(String
term)
Class - Node.java
Used
to build the list.
Fields
- Reference
to object (President)
You can
use private President p for this
lab although a more general solution would
be private Object p; or
private Comparable p; or would use
generics.
- Reference
to next Node
private
Node next;
Methods
- Constructors
o
Node(Object
obj) (The reference to the next Node will be null).
o Node(Object obj,
Node next)
- President
getItem()
- void
setItem(President p)
- President
getNext()
- void
setNext(Node n)
Class - Plist.java
A
linked list containing a singly linked series of Nodes.
Methods
- Constructor
void Plist()
Create new empty list
- int getSize()
Get number
of entries in list
- void
insertAtFront(President p)
Insert President object at
the beginning of the list
- void
insertAtEnd(President p)
Insert President object at
the end of the list
- void
insertInSequence(President p)
Insert p in front of the
first node that is greater than p. This
method should use compareTo() to compare objects
(Presidents).
- int locate(President p);
Find a
particular member of the list and return it’s index. This is the current position of an object in
the list and will change if you add and delete Presidents. Use compareTo to
compare p to other Presidents in the list to see if they are equal. Return -1 if not found in list.
Hint – to
find Abraham
int abe_idx =
locate(new President(“Lincoln, Abraham”));
- void
delete(int num)
Delete entry at index num.
- President
getFirst()
Return first entry in list .
- President get(int index)
Return entry at position indicated by index.
Class - TestMain.java
This
class contains main() which performs the following
1)
Open the file potus.txt , read the records and build the list. A link to potus.txt is at the bottom of the
page. Each line in the file contains:
- The
presidential sequence number (e.g. George W. Bush is 43).
- Name
(Last, first)
- Years
in office (9999-9999)
- Party
affiliation
The
fields are separated by tabs (\t).
You
can use a Scanner (or a BufferedReader) and StringTokenizer to read and parse records from the
file. Reading the file is easier if you
a) Set the scanner to use new line
“\n” as a delimiter
Scanner scnr
= new Scanner(input).useDelimiter(“\n”);
b) Trim trailing blanks when you read
each line
String line = scnr.nextLine().trim();
c) Set the delimiter on the StringTokenizer to separate fields on tabs.
StringTokenizer tkn = new StringTokenizer(line, "\t");
d) The presidential sequence number
must be converted from a String to an int.
int sequence = Integer.parseInt(tkn.nextToken());
e) The other fields do not need to be
converted. e.g.
String name
= tkn.nextToken();
Once
you have read them in, the list will be in Presidential sequence number order.
2)
After you have built the list print it.
Loop through the list and use the toString() method of the President class to display the name and party for
each entry:
Lincoln, Abraham Republican
3)
William Henry Harrison had the shortest term of office, thirty-one days. Locate him and delete him from the list. Verify that he has been deleted by printing
the list.
4)
Pick a possible 44th President and insert him/her at the end of the list
with the correct party and term. Print
the list to verify that she/he has been added.
5)
Copy your list into a new list so that the new one has the Presidents in
alphabetic order. Hint: insertInSequence() Print this new list.
What to turn in
You
must turn in a Jar file including
all your source code (.java files and .class files) and compiled Java
program. Including compiled and source code for all classes