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.Set; 008import java.util.LinkedHashSet; 009import java.util.Iterator; 010import java.util.Collections; 011 012/** 013 * This is a container can be used to contain Balls. 014 * A given Ball may only appear in a BallContainer once. 015 */ 016public class BallContainer implements Iterable<Ball> { 017 018 // Contents of the BallContainer. 019 private Set<Ball> contents; 020 021 /** 022 * Constructor that creates a new ballcontainer. 023 */ 024 public BallContainer() { 025 contents = new LinkedHashSet<Ball>(); 026 } 027 028 /** 029 * Implements the Iterable interface for this container. 030 * @return an Iterator over the Ball objects contained 031 * in this container. 032 */ 033 public Iterator<Ball> iterator() { 034 // If we just returned the iterator of "contents", a client 035 // could call the remove() method on the iterator and modify 036 // it behind our backs. Instead, we wrap contents in an 037 // "unmodifiable set"; calling remove() on this iterator 038 // throws an exception. This is an example of avoiding 039 // "representation exposure." You will learn more about this 040 // concept later in the course. 041 return Collections.unmodifiableSet(contents).iterator(); 042 } 043 044 /** 045 * Adds a ball to the container. This method returns <tt>true</tt> 046 * if ball was successfully added to the container, i.e. ball is 047 * not already in the container. Of course, you are allowed to put 048 * a Ball into a container only once. Hence, this method returns 049 * <tt>false</tt>, if ball is already in the container. 050 * @param b Ball to be added. 051 * @requires b != null. 052 * @return true if ball was successfully added to the container, 053 * i.e. ball is not already in the container. Returns false, if ball is 054 * already in the container. 055 */ 056 public boolean add(Ball b) { 057 // Your code goes here. Remove the exception after you're done. 058 throw new RuntimeException("Method not implemented"); 059 } 060 061 /** 062 * Removes a ball from the container. This method returns 063 * <tt>true</tt> if ball was successfully removed from the 064 * container, i.e. ball is actually in the container. You cannot 065 * remove a Ball if it is not already in the container and so ths 066 * method will return <tt>false</tt>, otherwise. 067 * @param b Ball to be removed. 068 * @requires b != null. 069 * @return true if ball was successfully removed from the container, 070 * i.e. ball is actually in the container. Returns false, if ball is not 071 * in the container. 072 */ 073 public boolean remove(Ball b) { 074 // Your code goes here. Remove the exception after you're done. 075 throw new RuntimeException("Method not implemented"); 076 } 077 078 /** 079 * Each Ball has a volume. This method returns the total volume of 080 * all the Balls in the container. 081 * @return the volume of the contents of the container. 082 */ 083 public double getVolume() { 084 // Your code goes here. Remove the exception after you're done. 085 throw new RuntimeException("Method not implemented"); 086 } 087 088 /** 089 * Returns the number of Balls in this container. 090 * @return the number of Balls in this container. 091 */ 092 public int size() { 093 // Your code goes here. Remove the exception after you're done. 094 throw new RuntimeException("Method not implemented"); 095 } 096 097 /** 098 * Empties the container, i.e. removes all its contents. 099 */ 100 public void clear() { 101 // Your code goes here. Remove the exception after you're done. 102 throw new RuntimeException("Method not implemented"); 103 } 104 105 /** 106 * This method returns <tt>true</tt> if this container contains 107 * the specified Ball. It will return <tt>false</tt> otherwise. 108 * @param b Ball to be checked if its in container 109 * @requires b != null. 110 * @return true if this container contains the specified Ball. Returns 111 * false, otherwise. 112 */ 113 public boolean contains(Ball b) { 114 // Your code goes here. Remove the exception after you're done. 115 throw new RuntimeException("Method not implemented"); 116 } 117 118}