Answer to Question #242960 in Java | JSP | JSF for Chadwin Fritz

Question #242960

Most employees earn R50 000 per year. Managers earn R20 000 extra per year. ► Most employees gjet 15 workdays leaves a year. Managers get an extra 3 days leave per year. Secretaries must work 40 per week.

Employee -name: String EmployeeDeconstructors +Employee(String) ceconstructor +oString(): String setName(String) : void + getName(): String +gotSalary(): double "getHours(): double *getteaveDays(): int HeaveApplication); void +rkl): 1. Code the classes Employee, Manager and Secretary. 2. Code an application with a main method. In this class you must code an array of type Employee with a maximum size of 20. a. Add a minimum of 10 objects to the array. You may hard-code these object values. The ob ects must be a mixture of Employee, Manager and Secretary. b. Display all the objects on the screen. Calculate, and then display the average salary of all employees. Extra Calculate and display the cost of the leave to the company for each employee.


1
Expert's answer
2021-09-27T18:12:42-0400
public class Employee {
    private String name;
    private double salary;
    private int leaveDays;
    private double workHours;


    public Employee(String name) {
        this.name = name;
        salary = 50000;
        leaveDays = 15;
        workHours = 35;
    }

    public void setName(String name) {
        this.name = name;
    }

    protected String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public int getLeaveDays() {
        return leaveDays;
    }

    public void setLeaveDays(int leaveDays) {
        this.leaveDays = leaveDays;
    }

    public double getWorkHours() {
        return workHours;
    }

    public void setWorkHours(double workHours) {
        this.workHours = workHours;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", leaveDays=" + leaveDays +
                ", workHours=" + workHours +
                '}';
    }
}



public class Manager extends Employee {
    public Manager(String name) {
        super(name);
        setSalary(getSalary() + 20000);
        setLeaveDays(getLeaveDays() + 3);
    }
}


public class Secretary extends Employee {
    public Secretary(String name) {
        super(name);
        setWorkHours(40);
    }
}


import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Employee[] employees = new Employee[20];
        String[] names = {"Tom", "Lisa", "Bob", "Roger", "Tim", "Leo", "Nick", "David", "Sarah", "Lucie"};
        double totalSalary = 0;
        for (int i = 0; i < names.length; i++) {
            employees[i] = new Random().nextBoolean() ?
                    (new Random().nextBoolean() ? new Manager(names[i]) : new Employee(names[i]))
                    : new Secretary(names[i]);
            totalSalary += employees[i].getSalary();
        }
        for (int i = 0; i < names.length; i++) {
            System.out.println(employees[i]);
        }
        System.out.println("Average salary: " + totalSalary / names.length);
        double salaryPerDay;
        for (int i = 0; i < names.length; i++) {
            salaryPerDay = employees[i].getSalary() / 365;
            System.out.println(employees[i].getName() + " leave cost: " + employees[i].getLeaveDays() * salaryPerDay);
        }


    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS