创建一个测试类的Java [英] Creating a Tester Class for Java
问题描述
我需要创建一个测试类我的code,但我对如何做到这一点不知道,有人可以帮我吗?我试着编写本,但我已经得到了这些消息:
找到2的错误:
错误:构造PayCalculator()是未定义
错误:方法printData()是未定义的类型PayCalculatorTester
我的code:
{
PayCalculator P1 =新PayCalculator();
p1.setHourlyRate(8.25);
p1.setHoursWorked(45);
printData();
}
PayCalculator类
公共类PayCalculator{
私人双hourlyRate;
私人双hoursWorked; / **
*两个参数的构造函数
*添加hourlyRate和hoursWorked
* @参数时薪
* @参数的工作时间
* /
公共PayCalculator(双aHourlyRate,双aHoursWorked)
{
hourlyRate = aHourlyRate;
hoursWorked = aHoursWorked;
} / **
*设置每小时收费
* @返回hourlyRate
* /
公共无效setHourlyRate(双aHourlyRate)
{
hourlyRate = aHourlyRate;
} / **
*获取每小时收费
* @参数hourlyRate
* /
公共双getHourlyRate()
{
返回hourlyRate;
} / **
*设置工作时间
* @返回hoursWorked
* /
公共无效setHoursWorked(双aHoursWorked)
{
hoursWorked = aHoursWorked;
} / **
*得到工作时间
* @参数工时
* /
公共双getHoursWorked()
{
返回hoursWorked;
} 公共布尔workedOvertime()
{
如果(hoursWorked→40)
{
返回true;
}
其他
{
返回false;
}
} 公共双numHoursOvertime()
{
如果(hoursWorked→40)
{
返回hoursWorked - 40;
}
其他
{
返回0;
}
} 公共双calculateGrossPay()
{
如果(hourlyRate< = 10.25)
{
如果(hourlyRate< = 40)
返回hourlyRate * hoursWorked;
}
其他
{
双grossPay =((40 * hourlyRate)+((hourlyRate * 2)* hoursWorked - 40));
返回grossPay;
} 如果(hoursWorked< = 60)
{
返回hourlyRate * hoursWorked;
}
其他
{
返回60 * hourlyRate;
}
} 公共双determineTaxRate(双grossPay)
{
如果(grossPay> = 800)
{
避免双重征税= 0;
税= grossPay * 0.37;
返回税;
}
否则,如果(grossPay> = 400)
{
避免双重征税= 0;
税= grossPay * 0.22;
返回税;
}
其他
{
避免双重征税= 0;
税= grossPay * 0.12;
返回税;
}
} 公共双calculateNetPay(双grossPay,避免双重征税)
{
双calculateNetPay = grossPay - 税;
返回calculateNetPay;
} 公共无效printData(双grossPay,避免双重征税)
{
的System.out.println(工作小时数:+ hoursWorked);
的System.out.println(每小时收费:+ hourlyRate);
的System.out.println(超时工作时数:+ numHoursOvertime());
的System.out.println(加班?+ workedOvertime());
的System.out.println(工资总额+ calculateGrossPay());
的System.out.println(税率+ determineTaxRate(grossPay));
的System.out.println(净工资+ calculateNetPay(grossPay,含税));
}}
这不仅是因为你的参数列表不匹配,你的方法的定义。
此外,还有似乎是在code小调不一致。试试这个:
进口java.io. *;
进口的java.util。*;类PayCalculator
{
私人双hourlyRate;
私人双hoursWorked; / **
*两个参数的构造函数
*添加hourlyRate和hoursWorked
* @参数时薪
* @参数的工作时间
* /
公共PayCalculator(双hourlyRate,双hoursWorked)
{
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
} / **
*获取每小时收费
* @参数hourlyRate
* /
公共双getHourlyRate()
{
返回hourlyRate;
} / **
*得到工作时间
* @参数工时
* /
公共双getHoursWorked()
{
返回hoursWorked;
} 公共布尔workedOvertime()
{
如果(hoursWorked→40)
返回true;
其他
返回false;
} 公共双numHoursOvertime()
{
如果(hoursWorked→40)
返回hoursWorked - 40;
其他
返回0;
} 公共双calculateGrossPay()
{
如果(hourlyRate< = 10.25)
如果(hoursWorked< = 40)
返回hourlyRate * hoursWorked;
其他
返回((40 * hourlyRate)+((hourlyRate * 2)* hoursWorked - 40));
如果(hoursWorked< = 60)
返回hourlyRate * hoursWorked;
其他
返回60 * hourlyRate;
} 公共双determineTaxRate(双grossPay)
{
如果(grossPay> = 800)
返回grossPay * 0.37;
否则,如果(grossPay> = 400)
返回grossPay * 0.22;
其他
返回grossPay * 0.12;
} 公共双calculateNetPay(双grossPay,避免双重征税)
{
返回(grossPay - 税);
} 公共无效printData(双grossPay,避免双重征税)
{
的System.out.println(工作小时数:+ hoursWorked);
的System.out.println(每小时收费:+ hourlyRate);
的System.out.println(超时工作时数:+ numHoursOvertime());
的System.out.println(加班?+ workedOvertime());
的System.out.println(工资总额+ calculateGrossPay());
的System.out.println(税率+ determineTaxRate(grossPay));
的System.out.println(净工资+ calculateNetPay(grossPay,含税));
}
}类测试
{
公共静态无效的主要(字符串ARGS [])//执行所需要的main()方法。
{
PayCalculator P1 =新PayCalculator(8.25,45); //如果你不定义自己的构造函数的默认构造函数时存在。
双myGrossPay = p1.calculateGrossPay();
p1.printData(myGrossPay,p1.determineTaxRate(myGrossPay)); //需要说的参数。
}
}
I need to create a tester class for my code, but I have no idea on how to do this, can someone help me? I've tried compiling this but I've got these messages:
2 errors found:
Error: The constructor PayCalculator() is undefined
Error: The method printData() is undefined for the type PayCalculatorTester
My Code:
{
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));
}
}
That's only because your parameter list does not match your methods' definition.
Also, there seem to be minor inconsistencies in your code. Try this instead:
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.
}
}
这篇关于创建一个测试类的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!