Java compiler error: "cannot find symbol constructor .."?
i'm writing code for employee, manager, hourly worker for a class assignment but i've hit a problem that i can't figure out, the following is my code for employee followed by hourly worker. the problem is hourly worker won't compile, it's giving a "cannot find symbol constructor employee" error when i try to compile (employee class compiiles without issue. any suggestions please? i think i've been staring at it for so long i can no longer see the problem! thanks. pieter.
EMPLOYEE CLASS -
public class Employee
{
public String firstName;
public String lastName;
public double hourlyRate;
final static double NORMAL_WORKWEEK = 37.5;
public Employee(String firstName, String lastName, double hourlyRate)
{
setFirstName(firstName);
setLastName(lastName);
setHourlyRate(hourlyRate);
}
//Accessor and Mutator Methods for the employee's first name.
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
firstName = firstName;
}
//Accessor and Mutator Methods for the employee's last name.
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
lastName = lastName;
}
//Access and Mutator Methods for the employee's hourly rate.
public double getHourlyRate()
{
return hourlyRate;
}
public void setHourlyRate(double hourlyRate)
{
//If the user input is valid, update the employee's hour rate with the newly input value.
if(hourlyRate > 0)
{
hourlyRate = hourlyRate;
}
//Otherwise prevent an hour rate greater than zero being overwritten
else if(hourlyRate <=0)
{
if(hourlyRate <= 0)
{
hourlyRate = 0;
}
decorateConsole();
//Alert the user to their mistake.
System.out.println("Error ! ! ! - An attempt to set the employee " + this.firstName + " " + this.lastName + "'s hourly rate to zero was detected.\n");
decorateConsole();
}
}
public void printState()
{
decorateConsole();
System.out.println("[FIRST NAME] = " + firstName + " [LAST NAME] = " + lastName + " [HOURLY RATE] = " + hourlyRate + "\n");
decorateConsole();
}
public void decorateConsole()
{
System.out.println("+-< EMPLOYEE INFO >-------------------------------------------------------------------------------------------------------------------------+\n");
}
HOURLY WORKER CLASS -
public class HourlyWorker extends Employee
{
private double wage;
private double hours;
public HourlyWorker(String firstName, String lastName, double hourlyWage, double hoursWorked)
{
super(firstName, lastName);
this.wage = wage;
this.hours = hours;
}
public void setWage (double hourlyWage)
{
this.wage = wage;
}
public void getWage()
{
return wage;
}
public void setHours (double hours)
{
this.hours = hours;
}
public double getHours()
开发者_StackOverflow {
return hours;
}
}
You don't have a Employee
constructor with two parameters:
super(firstName, lastName);
Try using:
super(firstName, lastName, 0.0);
EDIT as per tony request, here's a more detailed explanation.
With super(firstName, lastName);
you're invoking ( trying to invoke ) a constructor in the class Employee
which has two string parameters.
Reviewing the Employee
class definition, we see you don't have such constructor, but you have one with three parameters:
public Employee(String firstName, String lastName, double hourlyRate)
So, the solution is to invoke that constructor instead. Since you don't have a default value for hourlyRate
we can use 0.0 which is a double.
Other alternative would be to create a two parameter constructor in the Employee
class
public Employee(String firstName, String lastName )
In the HourlyWorker constructor you tried to call the Employee constructor like this:
super(firstName, lastName);
but the Employee class doesn't have a constructor with two parameters. You need to pass a third parameter (hourly rate) like this:
super(firstName, lastName, 42);
In HourlyWorker
you call
super(firstName, lastName);
but the Employee
constructor is
Employee(String, String, double)
The signatures don't match.
EDIT: Incidentally, why does the HourlyWorker have a private wage
member? How is it different (conceptually) to Employee.hourlyRate
?
In your HourlyWorker class you are calling the constructor of Employee {super(firstname, lastname)} with two arguments but in Employee class you do not have any constructors that take two arguments.
You don't have a super constructor that takes only two arguments.
HourlyWorker
's constructor tries to call super(firstName, lastName)
, but there's no such constructor declared in the parent class.
In your HourlyWorker class, you have the following line of code:
super(firstName, lastName);
But, there is no matching constructor in your employee class. Basically, the compiler is looking in your employee class for something like...
public Employee(String firstName, String lastName)
{
...
}
Define a new constructor, or call the constructor you've defined with the parameters you're missing.
You are calling HourlyEmployee's base class constructor (which is Employee's ctor) with 2 arguments instead of the 3 it wants.
Change the line in HourlyEmployee ctor from:
super(firstName, lastName);
to
super(firstName, lastName, hourlyWage);
Also, if you are still wanting to have a constructor like
public Employee(String firstName, String lastName)
{
...
}
and you know the default value from your double hourlyRate
, you can try to write a new constructor like the next one:
`public Employee(String firstName, String lastName) {
Employee(firstName, lastName, 0.0);
}`
There's something fishy going on around the else if
here. It's repeated with another if and some curly braces missing. Indent your code properly and make sure the open curlys matches the closing ones.
if(hourlyRate > 0)
{
hourlyRate = hourlyRate;
}
//Otherwise prevent an hour rate greater than zero being overwritten
else if(hourlyRate <=0)
{
if(hourlyRate <= 0)
{
hourlyRate = 0;
}
decorateConsole();
Also, these kind of lines won't work as intended (you have three of them):
firstName = firstName;
It must be:
this.firstName = firstName;
Like you have in your second class.
精彩评论