hw2
Class Box

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

public class Box
extends Object
implements Iterable<Ball>

This is a container can be used to contain Balls. The key difference between a BallContainer and a Box is that a Box has a finite volume. Once a box is full, a client cannot put in more Balls.


Constructor Summary
Box(double maxVolume)
          Constructor that creates a new box.
 
Method Summary
 boolean add(Ball b)
          This method is used to add Ball objects to this box of finite volume.
 void clear()
          Empties the box, i.e.
 boolean contains(Ball b)
          This method returns true if this box contains the specified Ball.
 Iterator<Ball> getBallsFromSmallest()
          This method returns an iterator that returns all the balls in this box in ascending size, i.e., return the smallest Ball first, followed by Balls of increasing size.
 double getVolume()
          Each Ball has a volume.
 Iterator<Ball> iterator()
          Implements the Iterable interface for this box.
 boolean remove(Ball b)
          Removes a ball from the box.
 int size()
          Returns the number of Balls in this box.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Box

public Box(double maxVolume)
Constructor that creates a new box.

Parameters:
maxVolume - Total volume of balls that this box can contain.
Method Detail

iterator

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

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

add

public boolean add(Ball b)
This method is used to add Ball objects to this box of finite volume. The method returns true if a ball is successfully added to the box, i.e., ball is not already in the box and if the box is not already full; and it returns false, if ball is already in the box or if the box is too full to contain the new ball.

Parameters:
b - Ball to be added.
Returns:
true if ball was successfully added to the box, i.e. ball is not already in the box and if the box is not already full. Returns false, if ball is already in the box or if the box is too full to contain the new ball.

getBallsFromSmallest

public Iterator<Ball> getBallsFromSmallest()
This method returns an iterator that returns all the balls in this box in ascending size, i.e., return the smallest Ball first, followed by Balls of increasing size.

Returns:
an iterator that returns all the balls in this box in ascending size.

remove

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

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

getVolume

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

Returns:
the volume of the contents of the box.

size

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

Returns:
the number of Balls in this box.

clear

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


contains

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

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