Box
public class Box : Sequence
This is a container can be used to contain Balls. A given Ball may only appear in a BallContainer once.
-
Declaration
Swift
public typealias Element = Ball
-
Declaration
Swift
public typealias Iterator = BallContainer.Iterator
-
Constructor that creates a new box.
-Parameter maxVolume: Total volume of balls that this box can contain.
Declaration
Swift
public init(maxVolume: Double)
-
Implements the Iterable interface for this box.
-Returns: an Iterator over the Ball objects contained in this box.
Declaration
Swift
public func makeIterator() -> Iterator
-
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.
Declaration
Swift
public func add(_ ball: Ball) -> Bool
Parameters
ball
Ball to be added.
Return Value
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.
-
This method returns an array of all the balls in this box in ascending size, i.e., the smallest Ball first, followed by Balls of increasing size.
Declaration
Swift
public func ballsFromSmallest() -> [Ball]
Return Value
an array of all the balls in this box in ascending size.
-
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 returnfalse
, otherwise.Declaration
Swift
public func remove(_ ball: Ball) -> Bool
Parameters
ball
Ball to be removed.
Return Value
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.
-
Empties the box, i.e. removes all its contents.
Declaration
Swift
public func removeAll()
-
This method returns
true
if this box contains the specified Ball. It will returnfalse
otherwise.Declaration
Swift
public func contains(_ ball: Ball) -> Bool
Parameters
ball
Ball to be checked if its in box
Return Value
true if this box contains the specified Ball. Returns false, otherwise.