Question is based on the Coin and Purse classes given below:
/* A simple coin class */
public class Coin
{
private double value;
private String name;
//constructor
public Coin(double coinValue, String coinName)
{
value = coinValue;
name = coinName;
}
/** @return the value of this coin */
public double getValue()
{ return value; }
/** @return the name of this coin */
public String getName()
{ return name; }
/** @param obj a Coin object
* @return true if this coin equals obj; otherwise false
*/
public boolean equals(Object obj)
{ return name.equals(((Coin) obj).name); }
//Other methods are not shown.
}
/* A purse holds a collection of coins */
public class Purse
{
private List<Coin> coins;
/** Creates an empty purse. */
public Purse()
{ coins = new ArrayList<Coin>(); }
/** Adds aCoin to the purse.
* @param aCoin the coin to be added to the purse
*/
public void add(Coin aCoin)
{ coins.add(aCoin); }
/** @return the total value of coins in purse */
public double getTotal()
{ /* implementation not shown */}
}
Here is the getTotal method from the Purse class:
/** @return the total value of coins in purse */
public double getTotal()
{
double total = 0;
/* more code */
return total;
}
Which of the following is a correct replacement for /* more code */?
(A)
for (Coin c : coins)
{
c = coins.get(i);
total += c.getValue();
}
(B)
for (Coin c : coins)
{
Coin value = c.getValue();
total += value;
}
(C)
for (Coin c : coins)
{
Coin c = coins.get(i);
total += c.getValue();
}
(D)
for (Coin c : coins)
{
total += coins.getValue();
}
(E)
for (Coin c : coins)
{
total += c.getValue();
}