0

I'm trying to write a payroll program that simply receives input, does some calculations and displays the output for EACH employee.

I have two types of employees, hourly and salary. I created a class for each type that extends to an abstract employee class:

public abstract class Employee{
  private String name;
  private String type;
  private  int hours;
  private double rate;
}


public class SalaryEmployee extends Employee{

  public SalaryEmployee(String type, String name, int hours, double rate){
   this.type = type;
    this.name = name;
    this.hours = hours;
    this.rate = rate;
  }

  public void print(){

  }
}

In my main method, when I try to create my objects using the code below:

  Employee employee;
  if(type.equals("hourly"))
  {
    employee = new HourlyEmployee(type, name, hours, rate);
  }
  else
  {
    employee = new SalaryEmployee(type, name, hours, rate);
  }

I get the following error when I try to compile:

Lab3.java:53: not a statement
        SalaryEmployee employee = new SalaryEmployee(type, name, hours, rate);
         ^
Lab3.java:53: ';' expected
        SalaryEmployee employee = new SalaryEmployee(type, name, hours, rate);
                      ^
2 errors

Any idea what is happening here? I've looked around for messing semi colons or braces, but haven't found anything yet.

Thanks for looking.

4
  • 1
    You don't get much out of your polymorphism unless you have "Employee" type on the left side. By the way, the type field is redundant. Commented May 30, 2012 at 23:52
  • 1
    errday errday, get dem pnts, errday Commented May 31, 2012 at 0:40
  • It is better if you can upload the complete code segment of your case. Because the line SalaryEmployee employee = new SalaryEmployee(type, name, hours, rate); can't find in given code segment Commented May 31, 2012 at 5:19
  • Your 2nd edition of this question makes it invalid. The error you report here was referring to code that you have "fixed." Please, leave the code as it was, if this is what you wanted to ask (I guess this is the case, as you have already accepted an answer), or update the error that you get, if any. Commented May 31, 2012 at 8:50

2 Answers 2

2

It is illegal to declare a variable in an if that does not use braces. It looks like what you meant to write is something like this:

Employee employee;
if(type.equals("hourly"))
  employee = new HourlyEmployee(type, name, hours, rate);
else
  employee = new SalaryEmployee(type, name, hours, rate);

Most coding conventions support always using braces after an if or else.

Sign up to request clarification or add additional context in comments.

1 Comment

After changing it to the code above, I get the error message: non-static variable this cannot be referenced from a static context referencing the employee = new Hourly... line. Also, can't braces be omitted if there is only one statement following the if/else?
2

The only error I can see is that your SalaryEmployee class cannot access the base class variables because they are private. You should make them protected.

Although, the error you are getting would be different.. If you are still getting the error after changing, your error is probably lying elsewhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.