Reputation: 25
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));
}
}
Upvotes: 0
Views: 18221
Reputation: 535
This is how you are calling the constructor of the object : PayCalculator p1 = new PayCalculator();
This is how the constructor is defined : public PayCalculator(double hourlyRate, double hoursWorked);
Obviously it will give error .
Upvotes: 0
Reputation: 3994
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.
}
}
Upvotes: 1
Reputation: 4242
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().
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.
Upvotes: 1
Reputation: 7854
"The constructor PayCalculator() is undefined"
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.
"The method printData() is undefined for the type PayCalculatorTester"
this method is defined in class PayCalculator
so right syntax should be p1.printData();
Upvotes: 1