001/** 002 * This is part of HW0: Environment Setup and Java Introduction for CSE 331. 003 */ 004package hw0; 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 * @return true if ball was successfully added to the box, 049 * i.e. ball is not already in the box and if the box is not 050 * already full. Returns false, if ball is already in the box or 051 * if the box is too full to contain the new ball. 052 */ 053 public boolean add(Ball b) { 054 // Your code goes here. Remove the exception after you're done. 055 throw new RuntimeException("Method not implemented"); 056 } 057 058 /** 059 * This method returns an iterator that returns all the balls in 060 * this box in ascending size, i.e., return the smallest Ball 061 * first, followed by Balls of increasing size. 062 * @return an iterator that returns all the balls in this box in 063 * ascending size. 064 */ 065 public Iterator<Ball> getBallsFromSmallest() { 066 // Your code goes here. Remove the exception after you're done. 067 throw new RuntimeException("Method not implemented"); 068 } 069 070 /** 071 * Removes a ball from the box. This method returns 072 * <tt>true</tt> if ball was successfully removed from the 073 * container, i.e. ball is actually in the box. You cannot 074 * remove a Ball if it is not already in the box and so ths 075 * method will return <tt>false</tt>, otherwise. 076 * @param b Ball to be removed. 077 * @return true if ball was successfully removed from the box, 078 * i.e. ball is actually in the box. Returns false, if ball is not 079 * in the box. 080 */ 081 public boolean remove(Ball b) { 082 return ballContainer.remove(b); 083 } 084 085 /** 086 * Each Ball has a volume. This method returns the total volume of 087 * all the Balls in the box. 088 * @return the volume of the contents of the box. 089 */ 090 public double getVolume() { 091 return ballContainer.getVolume(); 092 } 093 094 /** 095 * Returns the number of Balls in this box. 096 * @return the number of Balls in this box. 097 */ 098 public int size() { 099 return ballContainer.size(); 100 } 101 102 /** 103 * Empties the box, i.e. removes all its contents. 104 */ 105 public void clear() { 106 ballContainer.clear(); 107 } 108 109 /** 110 * This method returns <tt>true</tt> if this box contains 111 * the specified Ball. It will return <tt>false</tt> otherwise. 112 * @param b Ball to be checked if its in box 113 * @return true if this box contains the specified Ball. Returns 114 * false, otherwise. 115 */ 116 public boolean contains(Ball b) { 117 return ballContainer.contains(b); 118 } 119 120}