001/** 002 * This is part of HW0: Environment Setup and Java Introduction for CSE 331. 003 */ 004package hw3; 005 006import java.lang.Iterable; 007import java.util.Iterator; 008 009/** 010 * This is a container can be used to contain Balls. The key 011 * difference between a BallContainer and a Box is that a Box has a 012 * finite volume. Once a box is full, a client cannot put in more Balls. 013 */ 014public class Box implements Iterable<Ball> { 015 016 /** 017 * ballContainer is used to internally store balls for this Box 018 */ 019 private BallContainer ballContainer; 020 021 /** 022 * Constructor that creates a new box. 023 * @param maxVolume Total volume of balls that this box can contain. 024 */ 025 public Box(double maxVolume) { 026 // Your code goes here. Remove the exception after you're done. 027 throw new RuntimeException("Method not implemented"); 028 } 029 030 /** 031 * Implements the Iterable interface for this box. 032 * @return an Iterator over the Ball objects contained 033 * in this box. 034 */ 035 public Iterator<Ball> iterator() { 036 return ballContainer.iterator(); 037 } 038 039 040 /** 041 * This method is used to add Ball objects to this box of 042 * finite volume. The method returns true if a ball is 043 * successfully added to the box, i.e., ball is not already in the 044 * box and if the box is not already full; and it returns false, 045 * if ball is already in the box or if the box is too full to 046 * contain the new ball. 047 * @param b Ball to be added. 048 * @requires b != null. 049 * @return true if ball was successfully added to the box, 050 * i.e. ball is not already in the box and if the box is not 051 * already full. Returns false, if ball is already in the box or 052 * if the box is too full to contain the new ball. 053 */ 054 public boolean add(Ball b) { 055 // Your code goes here. Remove the exception after you're done. 056 throw new RuntimeException("Method not implemented"); 057 } 058 059 /** 060 * This method returns an iterator that returns all the balls in 061 * this box in ascending size, i.e., return the smallest Ball 062 * first, followed by Balls of increasing size. 063 * @return an iterator that returns all the balls in this box in 064 * ascending size. 065 */ 066 public Iterator<Ball> getBallsFromSmallest() { 067 // Your code goes here. Remove the exception after you're done. 068 throw new RuntimeException("Method not implemented"); 069 } 070 071 /** 072 * Removes a ball from the box. This method returns 073 * <tt>true</tt> if ball was successfully removed from the 074 * container, i.e. ball is actually in the box. You cannot 075 * remove a Ball if it is not already in the box and so ths 076 * method will return <tt>false</tt>, otherwise. 077 * @param b Ball to be removed. 078 * @requires b != null. 079 * @return true if ball was successfully removed from the box, 080 * i.e. ball is actually in the box. Returns false, if ball is not 081 * in the box. 082 */ 083 public boolean remove(Ball b) { 084 return ballContainer.remove(b); 085 } 086 087 /** 088 * Each Ball has a volume. This method returns the total volume of 089 * all the Balls in the box. 090 * @return the volume of the contents of the box. 091 */ 092 public double getVolume() { 093 return ballContainer.getVolume(); 094 } 095 096 /** 097 * Returns the number of Balls in this box. 098 * @return the number of Balls in this box. 099 */ 100 public int size() { 101 return ballContainer.size(); 102 } 103 104 /** 105 * Empties the box, i.e. removes all its contents. 106 */ 107 public void clear() { 108 ballContainer.clear(); 109 } 110 111 /** 112 * This method returns <tt>true</tt> if this box contains 113 * the specified Ball. It will return <tt>false</tt> otherwise. 114 * @param b Ball to be checked if its in box 115 * @requires b != null. 116 * @return true if this box contains the specified Ball. Returns 117 * false, otherwise. 118 */ 119 public boolean contains(Ball b) { 120 return ballContainer.contains(b); 121 } 122 123}