Олимпиадный тренинг

Задача . 37457


Задача

Темы:
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 List<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 */}
}

Two coins are said to match each other if they have the same name or the same value. You may assume that coins with the same name have the same value and coins with the same value have the same name. A boolean method find is added
to the Purse class:
/** @return true if the purse has a coin that matches aCoin,
* false otherwise
*/
public boolean find(Coin aCoin)
{
    foreach (Coin c in coins)
    {
     /* code to find match */
    }
return false;
}
Which is a correct replacement for /* code to find match */?
I
if (c.Equals(aCoin))
    return true;

II
if ((c.getName()).equals(aCoin.getName()))
    return true;

III
 if ((c.getValue()).Equals(aCoin.getValue()))
    return true;

(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

time 1000 ms
memory 32 Mb
Правила оформления программ и список ошибок при автоматической проверке задач

Статистика успешных решений по компиляторам
Комментарий учителя