我需要为我的代码创建一个测试人员类,但我不知道如何做到这一点,有人可以帮助我吗?我已经尝试过编译,但我收到了这些消息:
发现了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 Class
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));
}
}
答案 0 :(得分:1)
1,如果你没有在类中定义任何构造函数,编译器将隐式定义一个默认构造函数(不带参数)。如果您已明确定义一个构造函数。编译器不再这样做了。因此,您无法使用默认构造函数PayCalculator()。
参考:Providing Constructors for Your Classes
2,printData是PayCalculator的实例方法,您需要使用PayCalculator实例调用它,即p1.printData()。就像setHourlyRate和setHoursWorked一样。
答案 1 :(得分: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.
}
}
答案 2 :(得分:1)
“构造函数PayCalculator()未定义”
因为您已经在类中定义了唯一带有参数的构造函数。 Java说如果你为构造函数提供参数,那么不提供没有参数的默认构造函数。所以你应该明确地提供那个。或者使用你声明的那个。
“类型为PayCalculatorTester”
的方法printData()未定义此方法在类PayCalculator
中定义,因此正确的语法应为p1.printData();
答案 3 :(得分:0)
这是你调用对象的构造函数的方法: PayCalculator p1 = new PayCalculator();
这是构造函数的定义方式: 公共PayCalculator(double hourlyRate,double hoursWorked);
显然会出错。