9.1: MonetaryCoin 设计并实现一个名为 MonetaryCoin 的类,该类派生自第 5 章中介绍的 Coin 类。在 MonetaryCoin 中存储一个表示其值的整数并添加一个返回其值的方法。添加一个 toString 方法,该方法在 Coin 的字符串表示中附加一个空格,后跟硬币的值。
司机。无需创建主驱动程序类,而是在 MonetaryCoin 类本身中包含一个 main 方法来练习该类的行为。您的方法应该创建一个包含 7 个 MonetaryCoin 对象的数组,其值为 1,5,10,25,50,100,100,然后遍历该数组,翻转每个硬币。然后该方法应再次遍历数组,调用 getValue 方法并将结果值相加。然后应将每个硬币的字符串表示形式单独打印在一行上,然后在一行上单独打印计算的值的总和(前面带有标签“总价值:”。
这就是问题所在,到目前为止我输入的代码是:
public class MonetaryCoin extends Coin
{ private int value;
public int getValue()
{ return value; }
public String toString()
{ return super.toString() + " " + value; }
public static void main(String [] args)
{ int sum = 0;
MonetaryCoin[] coin = {1,5,10,25,50,100,100};
for(int i = 0;i<coin.length;i++)
{ coin[i].flip(); }
for(int i = 0;i<coin.length;i++)
{ sum += coin[i].getValue();
System.out.println(coin[i].toString()); }
System.out.println();
System.out.println("Total Value: " + sum); } }
我收到一条错误响应:
MonetaryCoin.java:9: incompatible types
found : int
required: MonetaryCoin
MonetaryCoin[] coin = {1,5,10,25,50,100,100};
^
MonetaryCoin.java:9: incompatible types
found : int
required: MonetaryCoin
MonetaryCoin[] coin = {1,5,10,25,50,100,100};
^
MonetaryCoin.java:9: incompatible types
found : int
required: MonetaryCoin
MonetaryCoin[] coin = {1,5,10,25,50,100,100};
^
MonetaryCoin.java:9: incompatible types
found : int
required: MonetaryCoin
MonetaryCoin[] coin = {1,5,10,25,50,100,100};
^
MonetaryCoin.java:9: incompatible types
found : int
required: MonetaryCoin
MonetaryCoin[] coin = {1,5,10,25,50,100,100};
^
MonetaryCoin.java:9: incompatible types
found : int
required: MonetaryCoin
MonetaryCoin[] coin = {1,5,10,25,50,100,100};
^
MonetaryCoin.java:9: incompatible types
found : int
required: MonetaryCoin
MonetaryCoin[] coin = {1,5,10,25,50,100,100};
我怎样才能让它工作?谢谢。