为 Java 创建测试器类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15286505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a Tester Class for Java
提问by Larry Gyamfi
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:
发现2个错误:
Error: The constructor PayCalculator() is undefined
Error: The constructor PayCalculator() is undefined
Error: The method printData() is undefined for the type PayCalculatorTester
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
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));
}
}
回答by longhua
1, If you have NOT defined any constructor in a class, compiler will define a default constructor (without arguments) for you implicitly. If you have define one constructor explicitly. Compiler won't do this anymore. So you couldn't use the default constructor PayCalculator().
1、如果你没有在类中定义任何构造函数,编译器会为你隐式定义一个默认的构造函数(不带参数)。如果您明确定义了一个构造函数。编译器不会再这样做了。所以你不能使用默认的构造函数 PayCalculator()。
Reference: Providing Constructors for Your Classes
参考:为您的类提供构造函数
2, printData is an instance method of PayCalculator, you need to invoke it using a PayCalculator instance, that is, p1.printData(). Just like setHourlyRate and setHoursWorked.
2、printData是PayCalculator的一个实例方法,需要使用PayCalculator实例调用,即p1.printData()。就像 setHourlyRate 和 setHoursWorked 一样。
回答by Roney Michael
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.
}
}
回答by vishal_aim
"The constructor PayCalculator() is undefined"
“构造函数 PayCalculator() 未定义”
because you've defined the only constructor with parameters in your class. Java says if you are providing your constructor with parameters then the default constructor with no parameters is not provided. So you should provide that one explicitly. Or use the one which you have declared.
因为您在类中定义了唯一带参数的构造函数。Java 表示,如果您为构造函数提供参数,则不会提供不带参数的默认构造函数。所以你应该明确地提供那个。或者使用您声明的那个。
"The method printData() is undefined for the type PayCalculatorTester"
“未定义 PayCalculatorTester 类型的方法 printData()”
this method is defined in class PayCalculator
so right syntax should be p1.printData();
这个方法是在类中定义的,PayCalculator
所以正确的语法应该是p1.printData();
回答by Snake Eye
This is how you are calling the constructor of the object : PayCalculator p1 = new PayCalculator();
这是您调用对象构造函数的方式: PayCalculator p1 = new PayCalculator();
This is how the constructor is defined : public PayCalculator(double hourlyRate, double hoursWorked);
这是构造函数的定义方式: public PayCalculator(double hourlyRate, double hoursWorked);
Obviously it will give error .
显然它会给出错误。