Answer to Question #282182 in Java | JSP | JSF for bbfhdj

Question #282182

Create an EMPLOYEE class having First Name, Last Name, socialSecurityNumber(SSN). Class SalariedEmployee extends class Employee having weeklySalary as a member. An overloaded method CalculateEarnings to calculate a SalariedEmployee’s earnings. Class HourlyEmployee also extends Employee and have hourlyWage and hoursWorked as data members with overloaded method CalculateEarnings. Class CommissionEmployee having commissionRate and grossSales as data members extends class Employee with overloaded method CalculateEarnings.

In main class make ArrayList of Employees to display:

1)Employee with highest Earning.2)Employee with Lowest Earning.3)Average Earnings of all employees.4)Earning of employees on odd positions.5)Display Earning according to alphabetical order.


1
Expert's answer
2021-12-23T00:29:27-0500
public class Employee {
    private String firstName;
    private String secondName;
    private String ssn;
    private static int count = 0;

    public Employee() {
        firstName = "Name" + count++;
    }

    public String getFirstName() {
        return firstName;
    }

    public double calculateEarnings() {
        return 0;
    }
}


public class SalariedEmployee extends Employee{
    public double weeklySalary;

    public double calculateEarnings(){
        return 1;
    }
}


public class HourlyEmployee extends Employee{
    private double hourlyWage;
    private double hoursWorked;


    public double calculateEarnings(){
        return 2;
    }
}


public class CommissionEmployee extends Employee{
    private double commissionRate;
    private double grossSales;

    public double calculateEarnings(){
        return 3;
    }
}


import java.util.ArrayList;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new SalariedEmployee());
        employees.add(new HourlyEmployee());
        employees.add(new CommissionEmployee());
        double total = 0;
        double max = Integer.MIN_VALUE;
        Employee maxEmployee = null;
        double min = Integer.MAX_VALUE;
        Employee minEmployee = null;
        for (int i = 0; i < employees.size(); i++) {
            if (employees.get(i).calculateEarnings() < min) {
                minEmployee = employees.get(i);
                min = minEmployee.calculateEarnings();
            }
            if (employees.get(i).calculateEarnings() > max) {
                maxEmployee = employees.get(i);
                max = maxEmployee.calculateEarnings();
            }
            total += employees.get(i).calculateEarnings();
        }
        System.out.println("Highest: " + maxEmployee.getFirstName() + " " + maxEmployee.calculateEarnings());
        System.out.println("Lowest: " + minEmployee.getFirstName() + " " + minEmployee.calculateEarnings());
        System.out.println("Average: " + total / employees.size());
        System.out.println("Odd: ");
        for (int i = 0; i < employees.size(); i += 2) {
            System.out.println(employees.get(i).getFirstName() + " " + employees.get(i).calculateEarnings());
        }
        employees.sort(Comparator.comparing(Employee::getFirstName));
        System.out.println("Alphabetic: ");
        for (Employee employee : employees) {
            System.out.println(employee.getFirstName() + " " + employee.calculateEarnings());
        }

    }
}

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