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