Assignment 11

CSIS 10B
Assignment 11 Ordered Structures
Due: next Wed
In class Demos (0 points)
We develop a simple (non-generic) OrderedList of String class:
1) using linear search to add items
2) using binary search to add items
Assignment (10 points) Do either Basic or Advanced
Basic -- do all these exercises in lab11basic folder inside mod11 download
You are given a Student class and some example code (OrderedVectorDemo) that reads a file of
Students into a Vector.
1) Modify the Student class to add a compareTo method that compares students by last name.
2) Make sure you add the phrase implements Comparable<Student> to the Student class header
to satisfy the generic requirement in the OrderedVector class.
3) Compile Student—if errors, debug
4) Now in OrderedVectorDemo change Vector to OrderedVector. When you rerun the program
you should notice that the order has changed so the students are now shown alphabetically.
5) There is no get method for an OrderedVector, and the toString method puts all the values on
one line, making them hard to read. However, there is an iterator method. Use an iterator of
your OrderedVector to display the Students in the OrderedVector on one line per student.
6) Write a merge method for the OrderedVector class. This method will take another
OrderedVector and merge it with the host data, maintaining the ordered quality in the resulting
data.
A simple way to do this is to just iterate through the other OrderedList and insert (add) each
value into the host list. This will give poor performance, however, since many items will need to
be shuffled out of the way for inserting into the middle of a vector.
A better way to implement a merge operation it to follow the ideas set forth here
http://www.algolist.net/Algorithms/Merge/Sorted_arrays , although be aware that your
method will be very different since you are writing a member of the OrderedVector class and
don't have direct access to the arrays.
You WILL have access to the vectors (this.data and that.data, assuming your parameter is
named that, and you can use the get method to access elements. The overall plan for the merge
method is to:
a) create a new vector inside the merge method, call it newData
b) go through the merge algorithm described by the link above and insert the items from both
this.data and that.data vectors into newData
c) after merging, make sure to check and remove duplicate data fields in newData (they should
be in adjacent locations)
d) assign data=newData, thereby replacing the old data vector with the new, merged data
vector.
e) Test your merge algorithm by merging the two OrderedVectors of students as shown in the
demo code
7) Write a subset method for the OrderedVector class. This method will take another
OrderedVector and return Boolean true or false depending on whether the second
OrderedVector is a subset of the first. For example, if ov1 contains the data “a c f g e” and ov2
contains the data “c f g h” and ov3 contains “a f e”, then:
ov1.subset(ov2); will return false, and ov1.subset(ov3) will return true
Since the data in both sets are sorted already, you can set up a loop that steps through all
the items in that.data (where that is the parameter to the method), with an inner nested loop
that advances thru this.data until a match is found (keep going) or the end of this.data is
reached (return false).
Use two counter variables, for example countA (for this) and countB (for that) to keep track of
where in the data vectors you are currently looking. These counters will only grow, they never
need to be reset.
Algorithm development is a part of this class: to assist your thought process sketch these ideas
out on paper, showing your logic in moving the counter variables through the two data sets.
Only when you can clearly explain the process to yourself should you begin coding up your
solution.
Advanced:
Create the BestOf class described in Lab11 on page 275, and use it in an application to find the largest 5
integer values in the sequence generated by Ulam(10001); (the Ulam sequence was described under
Advanced in Assignment 7&8). Your BestOf class does not need to be generic, and the Ulam generator
class has been provided for you in lab11advanced (with a small main test app already in the class).