【问题标题】:Creating a Tester Class for Java为 Java 创建一个测试器类
【发布时间】:2013-03-08 04:05:01
【问题描述】:

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

发现 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();
  }

PayCalculator 类

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

}

【问题讨论】:

  • P.S.我之前的回答是错误的。如果您无法自行纠正错误,请检查新的。

标签: java class testing compilation boolean


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

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

    参考:Providing Constructors for Your Classes

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

    【讨论】:

      【解决方案3】:

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

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

      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.
          }
      }
      

      【讨论】:

        【解决方案4】:

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

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

        显然会报错。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-19
          • 1970-01-01
          • 1970-01-01
          • 2019-07-12
          • 1970-01-01
          相关资源
          最近更新 更多