0

我需要为我的代码创建一个测试器类,但我不知道如何做到这一点,有人可以帮助我吗?我试过编译这个,但我收到了这些消息:

发现2个错误:

Error: The constructor PayCalculator() is undefined

Error: The method printData() is undefined for the type PayCalculatorTester

我的代码:

 {
    PayCalculator p1 = new PayCalculator();
    p1.setHourlyRate(8.25);
    p1.setHoursWorked(45);    
    printData();
  }

支付计算器类

public class PayCalculator

{
  private double hourlyRate;
  private double hoursWorked;

  /**
   * 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));
  }

}
4

4 回答 4

1

“构造函数 PayCalculator() 未定义”

因为您已经在类中定义了唯一带有参数的构造函数。Java 说,如果您为构造函数提供参数,则不提供不带参数的默认构造函数。所以你应该明确地提供那个。或使用您已声明的那个。

“PayCalculatorTester 类型的 printData() 方法未定义”

这个方法是在类中定义的,PayCalculator所以正确的语法应该是p1.printData();

于 2013-03-08T04:13:19.063 回答
1

1,如果您没有在类中定义任何构造函数,编译器将为您隐式定义一个默认构造函数(不带参数)。如果您明确定义了一个构造函数。编译器不会再这样做了。所以你不能使用默认的构造函数 PayCalculator()。

参考:为您的类提供构造函数

2、printData是PayCalculator的实例方法,需要使用PayCalculator实例调用,即p1.printData()。就像 setHourlyRate 和 setHoursWorked 一样。

于 2013-03-08T04:09:26.197 回答
1

那只是因为您的参数列表与您的方法的定义不匹配。

此外,您的代码中似乎存在细微的不一致。试试这个:

import java.io.*;
import java.util.*;

class PayCalculator
{
    private double hourlyRate;
    private double hoursWorked;

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

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

    /**
    * 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 (hoursWorked <= 40)
                return hourlyRate * hoursWorked;
            else
                return ((40 * hourlyRate) + ((hourlyRate * 2) * hoursWorked - 40));
        if (hoursWorked <= 60)
            return hourlyRate * hoursWorked;
        else
            return 60 * hourlyRate;
    }

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

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

    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));
    }
}

class Test
{
    public static void main(String args[])    //main() method required to execute.
    {
        PayCalculator p1 = new PayCalculator(8.25,45);    //default constructor only exists if you do not define your own constructor.
        double myGrossPay = p1.calculateGrossPay();
        p1.printData(myGrossPay,p1.determineTaxRate(myGrossPay));    //requires said parameters.
    }
}
于 2013-03-08T04:10:06.577 回答
0

这就是您调用对象构造函数的方式: PayCalculator p1 = new PayCalculator();

这就是构造函数的定义方式: public PayCalculator(double hourlyRate, double hoursWorked);

显然它会报错。

于 2013-03-08T04:54:03.747 回答