Design and implement a generic container that will store values of any type of
object as long as that object implements a definition of compareTo(). Take
- In hw10 make a copy of StringContainer called MyContainer.
- Modify the existing code of MyContainer to be generic.
- Modify the driver so that it uses the new generic MyContainer class.
Do your initial testing by storing strings into your generic container. Commit
- Copy the driver code that creates and tests MyContainer when using
strings. Modify the copied code to demonstrate and test MyContainer
using RPSPlayers. You can copy the most recent definition of
RPSPlayer from a previous assignment. Change the .compareTo()
method in the local copy to be based on name. Also, be sure to announce
in RPSPlayer that it implements Comparable<RPSPlayer>.
- You may notice that indexOf() does not work properly when dealing
with RPSPlayer objects. This is because we have not defined what it
means for two RPSPlayers to be equal. This problem can be solved by
a couple of ways. For now, solve the problem by using .compareTo()
rather than .equals() in your implementation of .indexOf().
Doing so will generate an error because .compareTo() is not guaranteed
to exist for any object. You will have to put a stipulation on the
types that can be used with MyContainer. In particular, we will only
allow types that implement the Comparable interface to be stored
in our container.
- Once you have the code compiling and working for both strings and
RPSPlayers take a moment to document your code as needed and
- Go through the checklist of methods outlined below to make sure
you have implemented each of them. Especially pay attention to the
display() and sort() methods that may not have been in
the original StringContainer.
To summarize, your MyContainer class should be able to hold any type of
object that implements the Comparable interface. Specifically, it should
throw a compiler error if a programmer tries to store non-Comparable
objects in it. Your MyContainer class should provide the following
- MyContainer(int size): the constructor will reserve size slots in
- MyContainer(): the constructor will set data array to null
- void add(T elem): insert the parameter at the end of the array;
if you did not do so in the previous assignment you should, when necessary,
automatically increase the size of the array and copy elements over before
- int size()): return the number of elements in the array (not the
- int indexOf(T elem)): return the position of elem in the
array; return -1 if not found; use the object's compareTo() method
to determine equality
- T get(int index)): will return a reference to the object at the
provided index of the array; return null if element not found
- void sort(): uses a selection sort to sort entries using the
compareTo() method to decide ordering; put smallest values first
- void display(): list all the objects in the container
The Driver will demonstrate that each of these methods works for three
different data types: Integer, String, and RPSPlayer.
The assignment will be graded according to the following criteria:
Your program should be completely working and should follow all of the
programming conventions named in class and explained in
http://josephus.hsutx.edu/classes/all/javaconventions/ (including the use
of comments that conform to the JavaDoc guidelines).
The instructor will grade your work based on what you have pushed to the hosted
homework repository you have shared. For all assignments the program is due
before the beginning of class on the due date. Repositories that show a commit
time after that time will be graded as late.