hw2
Class BallContainer

java.lang.Object
  extended by hw2.BallContainer
All Implemented Interfaces:
Iterable<Ball>

public class BallContainer
extends Object
implements Iterable<Ball>

This is a container can be used to contain Balls. A given Ball may only appear in a BallContainer once.


Constructor Summary
BallContainer()
          Constructor that creates a new ballcontainer.
 
Method Summary
 boolean add(Ball b)
          Adds a ball to the container.
 void clear()
          Empties the container, i.e.
 boolean contains(Ball b)
          This method returns true if this container contains the specified Ball.
 double getVolume()
          Each Ball has a volume.
 Iterator<Ball> iterator()
          Implements the Iterable interface for this container.
 boolean remove(Ball b)
          Removes a ball from the container.
 int size()
          Returns the number of Balls in this container.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BallContainer

public BallContainer()
Constructor that creates a new ballcontainer.

Method Detail

iterator

public Iterator<Ball> iterator()
Implements the Iterable interface for this container.

Specified by:
iterator in interface Iterable<Ball>
Returns:
an Iterator over the Ball objects contained in this container.

add

public boolean add(Ball b)
Adds a ball to the container. This method returns true if ball was successfully added to the container, i.e. ball is not already in the container. Of course, you are allowed to put a Ball into a container only once. Hence, this method returns false, if ball is already in the container.

Parameters:
b - Ball to be added.
Returns:
true if ball was successfully added to the container, i.e. ball is not already in the container. Returns false, if ball is already in the container.

remove

public boolean remove(Ball b)
Removes a ball from the container. This method returns true if ball was successfully removed from the container, i.e. ball is actually in the container. You cannot remove a Ball if it is not already in the container and so ths method will return false, otherwise.

Parameters:
b - Ball to be removed.
Returns:
true if ball was successfully removed from the container, i.e. ball is actually in the container. Returns false, if ball is not in the container.

getVolume

public double getVolume()
Each Ball has a volume. This method returns the total volume of all the Balls in the container.

Returns:
the volume of the contents of the container.

size

public int size()
Returns the number of Balls in this container.

Returns:
the number of Balls in this container.

clear

public void clear()
Empties the container, i.e. removes all its contents.


contains

public boolean contains(Ball b)
This method returns true if this container contains the specified Ball. It will return false otherwise.

Parameters:
b - Ball to be checked if its in container
Returns:
true if this container contains the specified Ball. Returns false, otherwise.