• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bag Class

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Curt
What have you got so far? Post the code that you've already written and tell us where you are stuck at so we can give you a hand. If you haven't written any then take a shot at it and build it in pieces.
Create a Bag class and a constructor. Then add you member variables.
Then add some of the functionality a piece at a time.
This is probably not the answer you're looking for, but most people will not give you an answer to a school assignment - but we're more than happy to help out if your stuck.
For your size method all it needs to do is return the number of Objects contained in the bag. You can create a member variable called size that is changed everytime an Object is added or deleted from the bag.
On other point to keep in mind, if you want to follow the more popular conventions then you might want to consider changing your size method to getSize( ) and the empty method to isEmpty( ).
[ January 29, 2002: Message edited by: Dave Vick ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic