HW: Generic Container due Mon 02 Apr 10:00

This assignment provides practice in creating a generic container class.


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 these steps:
  1. In hw10 make a copy of StringContainer called MyContainer.
  2. Modify the existing code of MyContainer to be generic.
  3. Modify the driver so that it uses the new generic MyContainer class. Do your initial testing by storing strings into your generic container. Commit your work.
  4. 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>.
  5. 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.
  6. Once you have the code compiling and working for both strings and RPSPlayers take a moment to document your code as needed and commit it.
  7. 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 methods:

  1. MyContainer(int size): the constructor will reserve size slots in the array
  2. MyContainer(): the constructor will set data array to null
  3. 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 inserting
  4. int size()): return the number of elements in the array (not the capacity)
  5. 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
  6. T get(int index)): will return a reference to the object at the provided index of the array; return null if element not found
  7. void sort(): uses a selection sort to sort entries using the compareTo() method to decide ordering; put smallest values first
  8. 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:
Correctness 16 pts
Documentation 2 pts
Conventions 2 pts
Version Control 2 pts
Total 22 pts

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.

Quick Links