001 /**
002 * This is part of the Problem Set 0: Introduction for CSE 331.
003 */
004 package ps0;
005
006 import java.lang.Iterable;
007 import java.util.Set;
008 import java.util.LinkedHashSet;
009 import java.util.Iterator;
010 import 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 */
016 public class BallContainer implements Iterable<Ball> {
017
018 // Contents of the box.
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 * @return true if ball was successfully added to the container,
052 * i.e. ball is not already in the container. Returns false, if ball is
053 * already in the container.
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 * Removes a ball from the container. This method returns
062 * <tt>true</tt> if ball was successfully removed from the
063 * container, i.e. ball is actually in the container. You cannot
064 * remove a Ball if it is not already in the container and so ths
065 * method will return <tt>false</tt>, otherwise.
066 * @param b Ball to be removed.
067 * @return true if ball was successfully removed from the container,
068 * i.e. ball is actually in the container. Returns false, if ball is not
069 * in the container.
070 */
071 public boolean remove(Ball b) {
072 // Your code goes here. Remove the exception after you're done.
073 throw new RuntimeException("Method not implemented");
074 }
075
076 /**
077 * Each Ball has a volume. This method returns the total volume of
078 * all the Balls in the container.
079 * @return the volume of the contents of the container.
080 */
081 public double getVolume() {
082 // Your code goes here. Remove the exception after you're done.
083 throw new RuntimeException("Method not implemented");
084 }
085
086 /**
087 * Returns the number of Balls in this container.
088 * @return the number of Balls in this container.
089 */
090 public int size() {
091 // Your code goes here. Remove the exception after you're done.
092 throw new RuntimeException("Method not implemented");
093 }
094
095 /**
096 * Empties the container, i.e. removes all its contents.
097 */
098 public void clear() {
099 // Your code goes here. Remove the exception after you're done.
100 throw new RuntimeException("Method not implemented");
101 }
102
103 /**
104 * This method returns <tt>true</tt> if this container contains
105 * the specified Ball. It will return <tt>false</tt> otherwise.
106 * @param b Ball to be checked if its in container
107 * @return true if this container contains the specified Ball. Returns
108 * false, otherwise.
109 */
110 public boolean contains(Ball b) {
111 // Your code goes here. Remove the exception after you're done.
112 throw new RuntimeException("Method not implemented");
113 }
114
115 }