I am having problems with an assignment I have, I have tried doing it but my program won't compile and won't work at all. My assignment is as follows. Any help would be greatly appreciated.
The Bag class which is compiled contains the following.
public Bag()
/* The constructor for the Bag class creates an
empty Bag
*/
public void add( Object o )
/* The Object o is added to the Bag.
*/
public Object getOne()
/* Removes one object, chosen at random, from the
Bag, and returns it. Precondition: In order for
this method to work correctly, the Bag must contain
one or more objects when the method is called.
*/
public boolean empty()
/* Returns true if there are no objects in the Bag,
and returns false otherwise. The contents of the
Bag remain unchanged.
*/
And then the extended class I am required to make is called BetterBag.java and I am required to implement the following.
public BetterBag()
/* The constructor creates a BetterBag that is empty.
*/
public int size()
/* Returns the actual number of objects stored in the Bag. Postcondition: Upon return, the BetterBag must contain the same objects as when the method was called.
*/
public
String toString()
/* Returns a string that contains a textual description of the contents of the BetterBag. In this string, the contents of each object should be surrounded by square brackets '[' and ']'.
*/
public int compareTo(Object o)
/* Compare two BetterBags to determine which one contains more objects. If the BetterBag for which the method is called contains more objects than the parameter o, the return value is greater than 0; if o contains more objects than the BetterBag for which the method is called, the return value is less than 0; if the two contain the same number of objects, the return value is 0. Precondition: The parameter object o must actually be a BetterBag in order for this method to work properly.
*/
I had problems with the public int size() method because I didn't know how to get it working. Any help would be appreciated, I just can't
get anything I code to work or compile, and I am lost.