我正在尝试创建一个测试器类,但这样做时我收到了一个错误,其中指出:
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));
}
}
嗯,是的。这是方法声明:
public void printData(double grossPay, double tax)
这就是你想如何称呼它:
p1.printData();
您期望
grossPay
和 tax
使用什么值?
说实话,你应该在已经指定了工作时间和小时费率的情况下传递
grossPay
,这似乎很奇怪,但这是另一回事。最直接的问题是您没有提供方法所需的参数。
您的方法需要两个值
grossPay
和 tax
:
public void printData(double grossPay, double tax)
但是您在调用它时没有提供
grossPay
和 tax
:
p1.printData();
在调用方法时包含
grossPay
和 tax
的值,它将正常工作,如下所示:
p1.printData(123.00,2.5);
你的方法的定义是:
public void printData(double grossPay, double tax)
它期望使用两个双参数进行调用,而您则不带参数进行调用:
p1.printData();
将双参数传递给
printData(44.85d, 77.56d)