Home > database >  How can I fix the constructor in my Employee program?
How can I fix the constructor in my Employee program?

Time:09-25

I currently have an assignment in my coding class that has me stumped. I'm a beginner at coding, and I am currently trying to get a constructor to work. I get the following error: "constructor Employee in class Employee cannot be applied to given types; required: String,int,double found: String reason: actual and formal argument lists differ in length"

Constructor code:

public class Employee {
    
    String name; //Employee name
    int employeeId; // employee id
    double salary; // employee salary
    
    
   //Constructor here
    public Employee(String name, int employeeId, double salary) {
        this.name = name;
        this.employeeId = employeeId;
        this.salary = salary;

Problem corresponding code:

Employee micheal = new Employee ("Micheal, 124, 15000.0");
        
Employee jim = new Employee ("Jim, 124, 10000.0");

The purpose of the program is to display two made up employee's names, ID #'s, and salary. After that's done, I need to have it calculate and set a 10% raise in salary for one of the employee's.

CodePudding user response:

Your arguments are 1 String. Your constructor is looking for String, int, double. You just put the closing quotation mark in the wrong spot.

Employee micheal = new Employee ("Micheal, 124, 15000.0");
Employee jim = new Employee ("Jim, 124, 10000.0");

Should be

Employee micheal = new Employee("Micheal", 124, 15000.0);
Employee jim = new Employee("Jim", 124, 10000.0);

CodePudding user response:

You have to respect typing in java String enclosed within quoted, int and double must be numbers not strings. So Change

Employee micheal = new Employee ("Micheal, 124, 15000.0");

to :

Employee micheal = new Employee ("Micheal", 124, 15000.0);
  •  Tags:  
  • java
  • Related