【问题标题】:Compiler error and Money class assignment编译器错误和 Money 类分配
【发布时间】:2013-01-21 10:20:16
【问题描述】:

我不断收到错误“Money 类中的构造函数 Money 不能应用于给定类型;必需:无参数;找到 int、int;原因:实际和正式参数列表的长度不同”

另外这里是我将要评分的程序的目标列表,如果您发现我的代码中有任何其他我可能会在以后遇到的错误,请随时为我指出!

使用 BlueJ 创建一个包含两个整数实例变量(美元和美分)的 Money 类。 提供以下方法:

  1. 用于初始化实例变量的两参数构造函数。构造函数 应该检查美分值是否在 0 到 99 之间,如果不是,则转移一些 美分到美元变量,使其介于 0 和 99 之间。
  2. 将美元初始化为 0 并将美分初始化为 1 的默认构造函数。它应该调用 两参数构造函数。
  3. 一个参数构造函数,在设置美分时初始化美分值 美元值到 0。此构造函数还应检查美分值是否为 在 0 到 99 之间,如果不是,则将一些美分转移到美元变量中 使其介于 0 到 99 之间。
  4. 美元和美分的存取器
  5. 标准 toString 方法
  6. 比较两个 Money 对象是否相同的标准 equals 方法 状态。
  7. 将 Money 对象作为其参数的 plus 方法。它创建并返回一个 新的 Money 对象,表示其 plus() 方法为的对象的总和 被调用和参数。它不会修改两个现有的值 对象。

最后,创建一个创建多个 Money 对象的 MoneyDemo 类。这个演示课 应该测试你已经实现的所有构造函数和方法。

这是我的代码:

public class Money
 {
     private int dollars, cents;
     private static final int CENTS_PER_DOLLAR = 100;

     public void checkMoney(int dollars, int cents)
     {
         while (cents<0) 
         {
             cents += 100;
             dollars--;
         }
         while (cents>99) 
         {
             cents -= 100;
             dollars++;
         }
     }

     public Money()
     {
         this.cents = 1;
         this.dollars = 0;
     }

     public void initializeCents(int cents)
     {
         while (cents<0) 
         {
             cents += 100;
             dollars--;
         }
         while (cents>99) 
         {
             cents -= 100;
             dollars++;
         }
     }

     public int getDollars()
     {
         return dollars;
     }

     public int getCents()
     {
         return cents;
     }

     public String toString()
     {
         String str;
         dollars = dollars + cents / CENTS_PER_DOLLAR;
         cents = cents % CENTS_PER_DOLLAR;
         str = "Your total amount is: " + "$" + dollars + "." + cents +"";
         return str;
     }

     public Money plus(Money y) 
     {
         return new Money(y.dollars, y.cents);
     }
 }

public class MoneyDemo 
 {
     public static void main(String[] args) 
     {

         int dollars = 5;
         int cents = 80;
         Money x = new Money( dollars, cents);
         x.checkMoney();

         dollars = 7;
         cents = 70;
         Money y = new Money( dollars, cents);
         y.checkMoney();

         USMoney z = x.plus(y);

         System.out.println( "x: $" + x.dollars + "." + x.cents + "c");
         System.out.println( "y: $" + y.dollars + "." + y.cents + "c");
         System.out.println( "x.plus(y): $" + z.dollars + "." + z.cents + "c");
     }
 }

【问题讨论】:

    标签: java methods parameters constructor compiler-errors


    【解决方案1】:

    你在打电话

    Money x = new Money( dollars, cents); 有两个参数

    你已经把它定义为

    public Money()  // no argument constructor
    {
         this.cents = 1;
         this.dollars = 0;
    }
    

    那就这样吧:

    public Money() // no argument constructor
    {
         this.cents = 1;
         this.dollars = 0;
    }
    public Money(int dollars,int cents) // parametrized constructor
    {
         this.cents = dollars;
         this.dollars = cents;
    }
    

    这样它既可以用于无参数构造函数,也可以用于具有两个参数的参数化构造函数

    更新:plusmethod`

    public Money plus(Money y) 
    {
        Money m = new Money();
        int totalCents = y.cents + this.cents;
        int totalDollars = y.dollars + this.dollars;
        if(totalCents > 100)
        {
           totalDollars = totalDollars + (int)(totalCents / 100);
           totalCents = (int)(totalCents % 100);
        }
    
        m.dollars = totalDollars;
        m.cents = totalCents;
    
        return m;
    }
    

    【讨论】:

    • 我建议将无参数构造函数的主体更改为this(0, 1);。这样,任何其他构造函数的工作只需要添加到一个构造函数中。
    • 感谢您的帮助,我添加了该行,并且在阅读了一些内容之后,现在这很有意义。但是,我现在在 x.checkMoney(); 行的 MoneyDemo 类中遇到了同样的错误。我没有正确调用 checkMoney 方法吗?我很困惑,因为我已经定义了美元和美分......
    • 您有类似的问题,您将其定义为public void checkMoney(int dollars, int cents),您将其称为m.checkMoney(),没有任何参数
    • 0 否决票接受 在询问了一下之后,我决定尝试清理我的代码。我相信我用构造函数和 toString 方法解决了我的问题,但是我仍然无法理解 equals 和 plus 方法。任何帮助,将不胜感激。这是我的新代码:
    【解决方案2】:

    你得到的错误意味着你没有一个构造函数来接受你试图传递的参数(两个整数)。你唯一的构造函数没有参数:

    public Money()
    

    您需要创建一个新的构造函数,其内容为

    public Money (int cents, int dollars) 
    
        //make sure that cents is between 0 and 99, if not transfer some cents to dollars
    

    你将需要第三个构造函数来满足你的赋值#3:

    public Money (int cents) 
        initialize cents based on the parameter passed into it
        set dollars = 0
    

    您还需要重写 equals() 方法。

    由于这是一项家庭作业,我不会告诉你如何做这些事情。

    如果我能告诉你任何事情,或者更详细地解释任何事情,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 2021-07-06
      • 1970-01-01
      相关资源
      最近更新 更多