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