# Repetition Patterns -- Practice

```public class Coin      // class has no modifier methods (is "immutable")
{
public Coin(double aValue, String aName) { . . . }
public double getValue() { . . . }
. . .
}

public class CoinCollection
{
private ArrayList<Coin> aList;    // internal representation is an ArrayList

/** Constructs an empty coin collection. **/
public CoinCollection()
{
// Code to construct empty collection is missing!

this.aList = new ArrayList<Coin>();

}

/** Adds a coin to the collection. **/
{
// Code to add a coin to the collection is missing!

}

/** Sums the values in the collection. **/
public double sum()
{
// Code to calculate the sum of the coin values in aList is missing!

double sum = 0;
for ( Coin c : aList )
{
sum += c.getValue();
}

}

/** Determines whether a given value is in the array.
*      @return true if val is in the array; false otherwise
**/
public boolean contains(double coinValue)
{
// Code to determine whether there is a coin with the
// specified value in the collection.

for ( Coin c : aList )
{
if ( c.getValue() == coinValue )
return true;
}
return false;

// Do NOT return false from an else inside the for loop,
// otherwise you will return true or false just based on whether
// the FIRST item in the list is of the correct value.

// Alternative loop:
for ( int i = 0; i < aList.size(); i++ )
{
Coin c = aList.get(i);
if ( c.getValue() == coinValue )
return true;
}
return false;

}

/** Finds the maximum coin value in the array.
**/
public double maxValue()
{
// Code to find maximum value in the collection is missing!

if ( aList.isEmpty() )
return -1;          // docs should have said what to do here

double maxSoFar = aList.get(0).getValue();
for ( Coin c : aList )
{
if ( c.getValue() > maxSoFar )
maxSoFar = c.getValue();
}
return maxSoFar;

}

/** Counts the number of coins in the collection with the given
* value. **/
public int count(double aValue)
{
// Code to count the number of coins in the collection that have
// the given value of aValue in aList

int theCount = 0;
for ( Coin c : aList )
{
if ( c.getValue() == aValue )
theCount++;
}
return theCount;

}
}

```