java方法不适用于参数

nfzehxib  于 2021-06-29  发布在  Java
关注(0)|答案(3)|浏览(438)

我试图创建一个tester类,但是在这样做的过程中,我得到了一个错误,该错误指出:

1 error found:

Error: The method printData(double, double) in the type PayCalculator is not
    applicable for the arguments ()

我该怎么修?这是我的测试代码

PayCalculator p1 = new PayCalculator();
    p1.setHourlyRate(8.25);
    p1.setHoursWorked(45.0);    
    p1.printData();

我的主代码

{
  private double hourlyRate;
  private double hoursWorked;

  public PayCalculator()
  {
    hourlyRate = 0.0;
    hoursWorked = 0.0;
  }

  /**
   * Two parameter constructor
   * Add hourlyRate and hoursWorked
   * @param the hourly rate
   * @param the hours worked
   */
  public PayCalculator(double aHourlyRate, double aHoursWorked)
  {
    hourlyRate = aHourlyRate;
    hoursWorked = aHoursWorked;
  }

  /**
   * sets the hourly rate
   * @return hourlyRate
   */ 
  public void setHourlyRate(double aHourlyRate)
  {
    hourlyRate = aHourlyRate;
  }

  /**
   * gets the hourly rate
   * @param hourlyRate
   */
  public double getHourlyRate()
  {
    return hourlyRate;
  }

  /**
   * sets the hours worked
   * @return hoursWorked
   */ 
  public void setHoursWorked(double aHoursWorked)
  {
    hoursWorked = aHoursWorked;
  }

  /**
   * gets the hours worked
   * @param hours worked
   */
  public double getHoursWorked()
  {
    return hoursWorked;
  }

  public boolean workedOvertime()
  {
    if (hoursWorked > 40)
    {
      return true;
    }
    else 
    {
      return false;
    }
  }

  public double numHoursOvertime()
  {
    if (hoursWorked > 40)
    {
      return hoursWorked - 40;
    }
    else 
    {
      return 0;
    }
  }

  public double calculateGrossPay()
  {
    if (hourlyRate  <= 10.25)
    {
      if (hourlyRate <= 40)
        return hourlyRate * hoursWorked;
    }
    else 
    {  
      double grossPay = ((40 * hourlyRate) + ((hourlyRate * 2) * hoursWorked - 40));
      return grossPay;
    }

    if (hoursWorked <= 60)
    {
      return hourlyRate * hoursWorked;
    }
    else
    {
      return 60 * hourlyRate;
    }
  }

  public double determineTaxRate(double grossPay)
  {
    if (grossPay >= 800)
    {
      double tax = 0;
      tax = grossPay * 0.37;
      return tax;
    }
    else if ( grossPay >= 400)
    {
      double tax = 0;
      tax = grossPay * 0.22;
      return tax;
    }
    else
    {
      double tax = 0;
      tax = grossPay * 0.12;
      return tax;
    }
  }

  public double calculateNetPay(double grossPay, double tax)
  {
    double calculateNetPay = grossPay - tax;
    return calculateNetPay;
  }

  public void printData(double grossPay, double tax)
  {
    System.out.println("Hours Worked: " + hoursWorked);
    System.out.println("Hourly rate: " + hourlyRate);
    System.out.println("Number of hours of overtime worked: " + numHoursOvertime());
    System.out.println("Worked overtime? " + workedOvertime());
    System.out.println("Gross pay: " + calculateGrossPay());
    System.out.println("Tax Rate: " + determineTaxRate(grossPay));
    System.out.println("Net Pay: " + calculateNetPay(grossPay, tax));
  }
}
bksxznpy

bksxznpy1#

方法的定义是:

public void printData(double grossPay, double tax)

它要求调用具有两个双参数,而您调用时没有参数:

p1.printData();

将双参数传递给 printData(44.85d, 77.56d)

lnlaulya

lnlaulya2#

嗯,是的。方法声明如下:

public void printData(double grossPay, double tax)

下面是你如何称呼它的:

p1.printData();

您希望使用什么值 grossPay 以及 tax ?
老实说,你不得不通过考试似乎很奇怪 grossPay 已经规定了工作时间和小时工资,但那是另一回事。最直接的问题是没有提供方法所需的参数。

ubof19bj

ubof19bj3#

您的方法需要两个值 grossPay 以及 tax :

public void printData(double grossPay, double tax)

但你没有提供 grossPay 以及 tax 给它打电话时:

p1.printData();

包括 grossPay 以及 tax 调用方法时,它将正常工作,如:

p1.printData(123.00,2.5);

相关问题