I have 5 classes super class ( Employee ) subclasses ( Hourly, Salary ) Also, GU
ID: 3640886 • Letter: I
Question
I have 5 classes
super class ( Employee )
subclasses ( Hourly, Salary )
Also, GUI class and the EmployeeInfo
I initilazed an array in my GUI class like this
( static Employee [] arrayOfEmployee = new Employee [30]; )
my super class is
public class Employee
{
EmoplyeesInfo name;
public Employee()
{
new array[nextElement++] = new HourlyEmployees
(new EmoplyeesInfo(int employeeID,String lName, String fName,
String mName ,String title), hoursWorked, hourlyWage));
}
}
there is an error. How can I creat new employee here.thanks
Explanation / Answer
public class Employee {
int employeeID;
private String lname;
private String fname;
Employee(int employeeID,String lname,String fname) {
this.employeeID=employeeID;
this.lname=lname;
this.fname=fname;
}
public static void main(String[] args) {
// Array of 5 Employees;
Employee[] emp = new Employee[5];
Employee hourly = new Hourly(1,"hourly","Employee 1",40,20);
Employee salary = new Salary(2,"Salary","Employee 1",50000);
emp[0] = hourly;
emp[1] = salary;
salary = new Salary(3,"Salary","Employee 2",50000);
emp[2]=salary;
salary = new Salary(4,"Salary","Employee 3",75000);
emp[3]=salary;
hourly = new Hourly(5,"hourly","Employee 1",40,15);
emp[4]=hourly;
// Print Array of Employees
for(int i=0; i<emp.length; i++) {
if (emp[i] instanceof Hourly) {
Hourly hour = (Hourly)emp[i];
System.out.println("Hourly Employee Salary : "+hour.hourlyWage + " "+hour.hoursWorked);
} else if ( emp[i] instanceof Salary){
Salary sal = (Salary)emp[i];
System.out.println("Salary Employee : "+sal.salary);
}
}
}
}
double salary;
Salary(int employeeID, String lname, String fname, double salary) {
super(employeeID, lname, fname);
this.salary=salary;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.