2013-03-08 94 views
-1

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

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

} 
+1

[Duplicate Account](http://stackoverflow.com/users/2121254/user2121254)? – SudoRahul 2013-03-08 04:14:25

+0

P.S.我以前的回答不正确。如果你不能自己纠正错误,请检查新的。 – 2013-03-08 04:45:20

回答

1

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

参考:Providing Constructors for Your Classes

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

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

“的构造PayCalculator()是未定义”

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

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

这种方法在课堂PayCalculator所以正确的语法定义应该是p1.printData();

0

这是你如何调用该对象的构造函数: PayCalculator p1 =新的PayCalculator();

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

显然它会给出错误。