Ennesby
Ennesby

Reputation: 1

Employee Class: cannot resolve symbol in EmployeeMain

I have to write an Employee Class, and then call it in Main to run the test items.

I keep getting "cannot resolve symbol" on a method, empRate. It exists in the Employee20 Class, but when I call the class and method from EmployeeMain20, I get a "cannot resolve symbol" error.

Net searching has given me all kinds of stuff about IntelliJ bugs, changing file pathways, and transferring projects, all of which just confuses me. IntelliJ was installed on this computer only, no files have been transferred or accessed from other sources, and creating a clean copy a location over didn't solve the problem.

This is the problematic code. The second "empRate" in each line is where I get "cannot resolve symbol".

    Employee20 empRate = new empRate("John", "Smith", 10000, 10, 10);
    Employee20 empRate1 = new empRate("Sue", "Smith", 20000, 10, 10);

Here is the full code for the calling class, EmployeeMain20:

public class EmployeeMain20 {

    //Year added to differentiate from [other] assignments

    public static void main(String[] args){

        Employee20 empRate = new empRate("John", "Smith", 10000, 10, 10);
        Employee20 empRate1 = new empRate("Sue", "Smith", 20000, 10, 10);

        double wage = empRate.getWage();

        System.out.println("Employee name: " + empRate1.firstName +  empRate1.lastName);
        System.out.println("Employee wage: " + "$" + wage);
        //emp.greet();

        System.out.println("===============================================");

        wage = empRate1.getWage(); //wage defined in Employee2020 class

        System.out.println("Employee name: " + empRate1.firstName +  empRate1.lastName);
        System.out.println("Employee wage: " + "$" + wage);
        //emp.greet();

    }
}

Here is the code for the class being called, Employee20:

public class Employee20 {

    //Year added to differentiate from [other] assignments

    String firstName, lastName;
    int baseSalary, overtime, rate;

    public double getWage(){
        double wage = baseSalary * (overtime * rate);
        return wage;
    }

    public void empRate(String fname, String lname, int base, int overTime, int unitrate){
        firstName = fname;
        lastName = lname;
        baseSalary = base;
        overtime = overTime;
        rate = unitrate;
    }


}

I'm having a similar problem on another assignment. I figure if I can find out how solve one, I can solve both.

Upvotes: 0

Views: 804

Answers (5)

Cleo
Cleo

Reputation: 136

As everyone already said,the problem is with your constructor,he needs to have the same name as class.What i want to add is that you don't necessary need to put different names as parameter,you can use "this":

public Employee20(String firstName, String lastName, int baseSalary, int overtime, int rate) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.baseSalary = baseSalary;
    this.overtime = overtime;
    this.rate = rate;
}

You can even generate constructor from your IDE.

Upvotes: 0

Sumit Singh
Sumit Singh

Reputation: 15896

From Providing Constructors for Your Classes

A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type. For example, Bicycle has one constructor:

public class Bicycle{
  public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
  }
}

So below in not valid constructor:

 public void empRate(String fname, String lname, int base, int overTime, int unitrate)

change it to:

public Employee20(String fname, String lname, int base, int overTime, int unitrate)

And create object using that like:

Employee20 empRate = new Employee20("John", "Smith", 10000, 10, 10);

Upvotes: 1

Stultuske
Stultuske

Reputation: 9437

This code

Employee20 empRate1 = new empRate("Sue", "Smith", 20000, 10, 10);

makes no sense. What you are trying to do is: 1. Initialize an Employee20 object 2. Call the instance method empRate on that instance

Change your code to one of the following:

1.

Employee20 one = new Employee20();
one.empRate("Sue", "Smith", 20000, 10, 10);

2.

new Employee20().empRate("Sue", "Smith", 20000, 10, 10);

empRate has void as returntype, so it won't return a value, but probably, you'll want to use it later on. That can be done by using the first approach, and later on calling other methods on the same instance.

Or, just drop the method, and put the initialization in your constructor:

public Employee20(fname, lname, base, overTime, unitrate) {
  firstName = fname;
  lastName = lname;
  baseSalary = base;
  overtime = overTime;
  rate = unitrate;
}

which you then can call through:

Employee20 empRate = new Employee20("Sue", "Smith", 20000, 10, 10);

Upvotes: 0

Guy
Guy

Reputation: 50899

You are using empRate as a constructor, but it isn't. The constructor will look like

public Employee20(String fname, String lname, int base, int overTime, int unitrate)

And the class will look like

public class Employee20 {

    String firstName, lastName;
    int baseSalary, overtime, rate;

    public Employee20(String fname, String lname, int base, int overTime, int unitrate) {
        firstName = fname;
        lastName = lname;
        baseSalary = base;
        overtime = overTime;
        rate = unitrate;
    }

    public double getWage() {
        double wage = baseSalary * (overtime * rate);
        return wage;
    }
}

Create new Employee20

Employee20 empRate = new Employee20("John", "Smith", 10000, 10, 10);

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44844

The constructor of the class needs to be the same as the name of the class, and does not have a return type

public Employee20 (String fname, String lname, int base, int overTime, int unitrate){
    firstName = fname;
    lastName = lname;
    baseSalary = base;
    overtime = overTime;
    rate = unitrate;
}

When you call it you can do as

Employee20 empRate = new Employee20("John", "Smith", 10000, 10, 10);

Upvotes: 1

Related Questions