Colorado Technical University
CS230–1403A-01 Intermediate Java Programming I
Phase 4 — IP
Zoo Contributors
Jack Simmonds
Professor: Edward Kaplan
8/2014
version 0.0.4
1
Contents
Colorado Technical University ........................................................................................... 1
Zoo Contributors ................................................................................................................. 1
Phase 1 — Linked List ........................................................................................................ 4
Zoo Contributors—Project Outline................................................................................. 4
The Contributor Class and Constructor ...................................................................... 7
Stack push,print, pop and reprint: with error handling ............................................... 9
Class StackInheritance: push, pop and list name ...................................................... 10
Class ListNode creates the node objects use by class List (and ultimately Donor) .. 11
Class List: the backbone of the program ................................................................... 12
Class List continued: ................................................................................................. 13
Class List continued… .............................................................................................. 14
Sample output image1: ............................................................................................. 15
Sample output image2: ............................................................................................. 16
Phase 2 — Hash Table with Collision Handling .......................................................... 17
Discussion ..................................................................................................................... 17
Input using delimiter and buffered reader ................................................................. 18
Each iteration, a line is read from the file, the ID is obtained from the contributor
object created, and the ID value is added to an array to be passed to the hash function. The
size of this array is used to set the size of the hash table. As each line is read in, a
contributor object is instantiated (using the Donor constructor). Only the IDs are needed for
this week’s exercise. ............................................................................................................. 18
Hash Function 1 from Class Hash ............................................................................ 19
Hash function 2 and search method “findKey” ........................................................ 20
Printing the hash table and collisions stack .............................................................. 21
Linked List print method .......................................................................................... 22
2
Push & Pop methods ................................................................................................. 23
Screenshot of output from hash table and collisions stack ....................................... 24
Phase 3 — Sorting Algorithms ......................................................................................... 25
Discussion ..................................................................................................................... 25
Contributor object: .................................................................................................... 26
Print method for individual contributor objects ........................................................ 26
PrintAll method to print linked lists of contributors ................................................. 26
Input method readCSV() which calls the add() method ........................................... 27
Input method continued ............................................................................................ 28
The add method......................................................................................................... 29
Output screenshot...................................................................................................... 30
Phase 4 — Searching the sorted linked list ....................................................................... 31
Output continued ....................................................................................................... 32
Discussion ..................................................................................................................... 33
Phase 5 — Recursion ........................................................................................................ 34
TBD............................................................................................................................... 34
References ......................................................................................................................... 35
3
Phase 1 — Linked List
Zoo Contributors—Project Outline
Your first task in developing the application for tracking contributors is to load a list of
the people who are helping the cause. Design and develop a linked list, implemented as a stack,
to track all of the contributors. You will read the contributor information from a file provided; it
is a comma delimited (CSV) file. Your design should include the following:
Each contributor will have the following Information:

Name: String; //the name of the contributor

City: String; //the city in which the contributor lives

Country: String; //the country in which the contributor lives (I used state instead
of country)

Phone: String; //the phone number for the contributor

Contribution: Double; //the amount of the contribution given by the contributor to
the zoo

ID: Integer; //identifier key for future needs
The above values are passed through a constructor in a file called Donor and are
accessible via its getters and setters (called from class DonorTest). The setters are used to assign
csv file values to the Donor constructor fields. Donor's constructor is then called and the getters
are used to populate the fields in the contributor object it creates. Once constructed, the
contributor object is pushed onto a stack list. File contributors.csv was edited to add the city
information. Going by the area code provided it was determined that the city is Asheville, NC.
Another modification made was to use the state, rather than the country, for the contributors. All
contributors are from the Asheville area, going by the phone numbers provided.
4
Contributor Functions/Methods:
Input constructor: //to accept a string for the name and additional information for each
contributor (this should call the Push constructor to implement the stack): readCSV method in
class DonorTest, uses a FileReader constructor wrapped in a BufferedReader (using its
constructor).
DonorsTest contains a while loop that is used to read the csv file line by line. For each
line the split method is used to populate an array of String tokens. The string tokens are then
assigned to fields in a contributor class (Donor) which contains a constructor to create
contributor objects. Since two of the fields are numeric (one with decimal places and one with
integers) they are parsed to double and integer types before being loaded into the contributor
object. As they are created, the contributor objects are pushed onto a stack list. The list is then
printed out using a print method in class List. The toString method is overridden in class Donor
and used to format the ouput of each object as its printed. Method toString is called from the
print method in List. Currently it is called explicitly, but will also work as an implicit call.
Once the list is populated and printed, a pop method is called to remove the object at the
top of the stack (removeFromFront() in class List called from pop() in class StackInheritance).
The removed object is printed out for display purposed and the list is re-printed to show the
remaining objects it contains.
Required methods:

Print constructor: //to print out the contributor data: located in class List

Pop constructor: located in StackInheritance

Push constructor located: in StackInheritance
The entire application uses 6 classes to create list objects, push them onto a stack list, and
pop one of the objects back off the list. Four of the classes is designed to allow for list creation
involving objects of any type, whether primitive or object. Methods in class list make it possible
to design lists that are either stack or queue type, or a hybrid of the two. ListNode is used to
create node objects consisting of references and data. List is used to instantiate a list and
manipulate nodes within it. StackInheritance extends List and is used to specifically implement a
5
stack type list. EmptyListException is used to handle empty list exceptions, should they arise. Its
methods are called from class list. Class Donor was written to create contributor objects for this
assignment. It consists of the requested contributor information ( as private class fields ) and an
associated constructor to create list objects. Donor also contains public setters and getters.
Class DonorTest is the application file. It contains the main method for the package. It
also contains a method named readCSV. ReadCSV is used to; read in the csv file, parse its lines,
assign parsed token values to the contributor objects (via the Donor constructor), push the
constructed objects onto the stack list, print out the list, pop an object off the list and re-print it.
The screenshots show two ways of using the list's print function. First, the list is printed as each
object is pushed. This confirms that the list is being properly populated as each line is read from
the input file. Next the print method is moved outside the while loop and a screenshot shows the
results of printing the list after its fully populated. Both screenshots show the one object
(contributor) being popped off the list, and the list showing the remaining contributors.
Deliverables:

A fully documented program to load the data implemented as a stack

A test plan to show how the program runs and can be executed

A screenshot showing that the program loaded the data, and after all data is
loaded, perform a single pop off the stack
6
The Contributor Class and Constructor
The Donor class defines and constructs contributor objects. It also contains an overriding
toString method used by the List class when called from the application class (DonorsTest) to
print out a list of donors. This class also contains get and set methods for each of the class fields.
The methods are public so they may be called by other members of the package.
7
Import the CSV file, set the contributor values and instantiate a contributor object
A method in the application Class “DonorsTest. This method reads the comma separated
value file, line by line. As each line is read it is parsed to tokens. The tokens are used to populate
the fields of the Donor class. Lastly, a Donor object is instantiated to create a contributor object
for the list.
8
Stack push,print, pop and reprint: with error handling
This is the remainder of the previous method. The file reading loop ends by pushing the
contributor object onto a stack. The stack is printed out, the top object popped, and the stack is
re–printed. The rest is error handling and the top of a very short main method. The main method
is used to name the list (using a method in StackInheritance which extends Class List). The only
other function of main is to call the method shown, which reads in the CSV file and executes the
application.
9
Class StackInheritance: push, pop and list name
This class extends List and is what determines that List, and the application in DonorsTest, are
implemented as a stack type linked list.
10
Class ListNode creates the node objects use by class List (and ultimately Donor)
Class ListNode defines the nodes used in the linked list. It contains fields for a reference value, a
data value, overloaded constructors for root and subsequent nodes. It also contains getters for
both the data and reference values. This class is used by class List and its linked list methods.
11
Class List: the backbone of the program
This class contains the methods needed to manipulate list nodes. Shown here are the class fields,
a default constructor, a one argument constructor (for input of a list name via parameter), and a
method for tracking the number of objects in a list. The class continues below.
12
Class List continued:
Class List continued. Shown are the methods needed for a stack list; insertAtFront, and
removeFromFront. Notice that when an item is removed, its data is stored in a variable in case it
is needed elsewhere (like to print out for confirmation). The class continues below.
13
Class List continued…
Class List continued. Not shown is the method removeFromBack. It is not used in a stack list, so
is not used by the application at this time. It is however; available for creating queue type lists.
Shown are the isEmpty method and the print method. The print method is called from the
application class, DonorsTest, and uses the overriding toString method of Class Donor.
14
Sample output image1:
This screenshot shows the list being printed as each contributor object is pushed onto the stack.
Once the list is populated with all the contributors in the csv file provided, one contributor is
popped off the top of the stack. The removed object is identified, by printing it out, then the list
is re-printed showing the remaining contributors.
15
Sample output image2:
In this screenshot, the list is printed after being populated. Next an object is popped off
the top of the list (stack type list). Finally, the list is re-printed showing the remaining
contributors it contains.
16
Phase 2 — Hash Table with Collision Handling
Discussion
This week a hash table was added that takes the contributor’s ID and, using a hash
function, places the value in an array. Where collisions occur due to attempts at overwriting an
already populated hash bucket, they are handled. Handling occurs in two ways. First, the hash
table index is incremented and a new insertion point is attempted. The loop repeats until the ID
value finds a hash bucket that is available. Second, the colliding ID is pushed onto a stack type
linked list.
Output shows the hash table being built, collisions being handled. Colliding IDs being
pushed onto a stack, and subsequently displayed. It also displays the contents of the hash table by
ID value and hash index. Find works by rehashing the ID value to create a hash index. If the hash
bucket selected by the index has an ID value that matches the input value the search result is
printed. If the ID value was a collision when entered initially to the table, then its current hash
index is found, and then the ID and hash bucket are printed out.
Below are a series of images showing the code for the various methods requested for the
assignment. The input from file stage is much like last week’s, except that here, only the ID is
needed from the contributor object. The objects are being generated however; and could be
accessed via the hash table at a later date. The IDs are stored temporarily in an an array
(elementsToAdd) which is passed to the from which they are iteratively moved, as
newElementVal(s) into the hash table. As the IDs are populating the hash table, the hash index
values are calculated using a hash function.
Normally, it may be best to allow more room for the hash table. Reading suggests two
times the size for the table as there are elements to go into it. Banas (Banas, 2013) suggests the
closest prime number larger than two times the elements to be hashed. This prevents a lot of
collisions, and leaves room for expanding a data set. However, part of the week’s exercise was to
handle collisions and place colliding IDs into a linked list stack. This is done as the hash table is
poplulated.
17
Input using delimiter and buffered reader
Each iteration, a line is read from the file, the ID is obtained from the contributor object
created, and the ID value is added to an array to be passed to the hash function. The size of this
array is used to set the size of the hash table. As each line is read in, a contributor object is
instantiated (using the Donor constructor). Only the IDs are needed for this week’s exercise.
18
Hash Function 1 from Class Hash
This shows the hash function method as far down as where the newly hashed ID value is
added to the hash table according to its hash function index. Also shown in lines 58 through 75
are collision handling for the hash table, and IDs that were involved in collisions are pushed onto
a linked list stack.
19
Hash function 2 and search method “findKey”
End of the hashFunction shows two methods called on the collisions stack. The print
function calls a print method in List which is overridden by a toString method in Donor. Donor is
the class used to create contributor objects.
20
Printing the hash table and collisions stack
The hash table constructor uses Java API class Arrays. The fill method is used to
populate the hash buckets with -1. This serves as a flag later when checking for occupied buckets
during execution of the hash function. Method displayTheStack prints out the hash table,
showing both the ID values and the hashed indices associated with each.
This is the overriding toString method used to format output of linked list objects. Here it
is used for IDs in the collisions stack.
21
Linked List print method
This method calls the toString method in class Donor to format object output. In this case
its only printing out the IDs, but could just as well print a list of contributor objects with all their
information.
22
Push & Pop methods
Method push, in StackInheritance class, calls method insertAtFront in List class. This
pushes an object onto the linked list stack.
Method pop, in StackInheritance, calls method removeFromFront in List
These methods are used with the collisions linked list.
23
Screenshot of output from hash table and collisions stack
Output screenshot showing the hash table being built, using a hash function and with
collision handling. The collisions stack, after collisions are pushed during hash table population.
And, output displaying the contents of the hash table with the associated hash for each ID value.
24
Phase 3 — Sorting Algorithms
Discussion
The phase 1 application was modified to include an insertion sort of the contributors. As
each contributor is read in from the CSV file they are pushed to an unsorted stack and the add
method is called. Method add populates an object array with contributors, as they’re read in, and
sorts the array. The sorted array is then used to populate a sorted linked list of contributors.
Output shows the initial unsorted linked list and the final sorted linked list. The former can be
used to confirm that the sorted list contains all the contributor information that was read from
file.
To perform the sort by name, it was assumed that sorting by last name would be
preferred. Accordingly, the contributor constructor was modified to include first name and last
name fields. CompareTo() was then used in the insertion sort, to compare last names of the
contributor objects. Contributors are typically referred to as donors in the code only because
donor is quicker to type.
Images below show the code for the requested contributor object constructor, the input
constructor (readCSV) a print constructor for individual contributor objects, a printAll method
for printing out a linked list of objects, the add method (which performs the insertion sort) and a
screenshot showing the output with unsorted and sorted lists.
25
Contributor object:
Print method for individual contributor objects
PrintAll method to print linked lists of contributors
26
Input method readCSV() which calls the add() method
This method takes two screenshots to cover. Here it is shown as far along as creating
“donor” object after reading in a line from the CSV file.
27
Input method continued
Towards the top of this image a contributor object is pushed to an unsorted stack, then the
add method is called to sort the list. Once the CSV file is read in (after the while loop) the
unsorted stack is printed. The sorted array is used to populate a sorted linked list and the linked
list is printed. The bottom of the method is just exception handling for the I/O functions being
performed with the CSV file.
28
The add method
Method add() takes a contributor object as a parameter which it then assigns to an object
array. The array is insertion sorted each time a new contributor object is read in from the CSV
file. Once the array is sorted, its contents are transferred to a linked list (in readCSV() ) and the
sorted, linked list is printed.
29
Output screenshot
The printAll method is used to print out both before and after linked lists. PrintAll calls the
toString method of the object constructor class to print out the individual contributor
information. The same toString method is used by the print method to print out individual
contributors. Method printAll is in the List class, while method print is in the Donor class. Notice
that, once the application is compiled, the list is read from file, sorted and output in less than a
second. Initially, allowing for compile time, NetBeans will show total time of 1 second.
30
Phase 4 — Searching the sorted linked list
Output shows the linked list before and after sorting. The two searches, one by name and the
other by ID number (by sequentially searching the sorted linked list). And the linked list used to
create the hash table.
31
Output continued
The above shows the hash table being built. The problem with getting the collision objects to
store properly in the linked list of contributors involved in collisions has been resolved. The hash
table itself is being built properly, and the collisions are being handled correctly. The hash
function being used will result in 2 collisions with the CSV file provided. Both are now being
stored, as contributor objects, in a “collisions” linked list. As with other linked lists shown, it is
printed out using the printAll method from the List class.
32
Discussion
Phase 4 application was coded by combining code from phases 2 and 3. The initial error
with the hash table’s linked list of collision objects has been resolved. The hash table is building
correctly as collisions are handled. Collision handling is working, but the linked list of collision
related objects (contributors) is not building correctly. However; the two search items, one by
last name and the other by ID number are working fine.
Two search methods were created. Both take a search key and traverse the sorted linked
list until the key matches a data field in the current node. If the current node does not contain
matching data, then current node is set to next node, and the traversal continues until the list has
been searched. If the current node contains the key being searched for, then the contributor
object is printed out. The print method (from class Donor) is used for printing individual objects.
As the CSV file is read in: each line is used to populate the arguments of a contributor
object constructor. A linked list is built according to the order of contributors in the CSV file. As
each object is created, the add method is called. The add method performs an insertion sort and
builds a sorted linked list. Once the CSV file is all read in, the sorted list is printed out, using the
printAll method from class List. PrintAll in turn, calls the toString method from Donor when
printing the contents of node data. PrintAll will continue printing objects until the entire linked
list is printed.
33
Phase 5 — Recursion
TBD
34
References
Deitel, P., Deitel, H.( 2012 ).Java: How to Program, 9th Edition ( Early Objects Version). Pearson
(Prentice Hall): ebook, web
Drozdek, A.(2010), Data Structures and Algorithms in Java, 2nd Ed.,
Course Technology, Inc: Boston, MA
Gupta, L.(2013).Parse CSV files in Java.How To Do In Java.accessed online:
http://howtodoinjava.com/2013/05/27/parse-csv-files-in-java/
Kaplan, E. (2014). Live chat presentation 1.2, Data Structures, CTU: Colorado Springs, CO.
web: Virtual Campus, CS230-1403A-01
https://campus.ctuonline.edu/portal/6/pages/mainframe.aspx?contentframe=/Default.aspx
M.U.S.E.(2010).Data Structures.CS230, CTU online, 2014: Colorado Springs, CO
web: https://campus.ctuonline.edu/courses/CS230/p1/hub1/hub.html
Banas, D.(2013).Tutorial: Java Hash Table.New Think Tank.web:
https://www.youtube.com/watch?v=B4vqVDeERhI
Seifert, D.(2013).Java Linked List Search Method.StackOverflow, accessed online:
http://stackoverflow.com/questions/20298496/java-linked-list-search-method
35