Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class Employee { private double[] hours=new double[7]; private String nam

ID: 3791323 • Letter: P

Question

public class Employee {
   private double[] hours=new double[7];
   private String name;
  
   public Employee(double[] hours, String name) {
       super();
   this.hours = hours;
   this.name = name;
   }
  
   public double[] getHours() {
   return hours;
   }
  
   public void setHours(double[] hours) {
       this.hours = hours;
   }
  
   public String getName() {
       return name;
   }
  
   public void setName(String name) {
   this.name = name;
   }
     
   public int numDaysWorked(){
       int total_D=0;
       for(int i=0;i<7;i++){
           if(hours[i]!=0.0){
               total_D=total_D+1;
           }
       }
       return total_D;
   }
  
   public double totalHours(){
       double total_H=0;
           for(int i=0;i<7;i++){
               total_H=total_H+hours[i];
           }

       return total_H;
      
   }
   public double hoursWorkedOnday(int day){
       return hours[day];
   }
  
   public String toString(){
   return name+" Worked for" +numDaysWorked() + " day(s) for a total of "+totalHours() + "hours .";
   }
}

import java.util.Scanner;

public class EmployeeTester {
   private static Scanner employee;
  
   public static void main(String[] args) {
       String name;
       int week_day=0;
      
       employee = new Scanner(System.in);     
       System.out.print("Enter a name : ");
       name=employee.next();
       System.out.print("Enter hours worked (7 values separated by spaces):");
       double hours[]=new double[7];
       for(int i=0;i<7;i++){
           hours[i]=employee.nextDouble();
       }
       Employee emp=new Employee(hours, name);
       System.out.println(emp.toString());
       System.out.print("Enter a day of the week (0-6):");
       week_day=employee.nextInt();
       System.out.println("Hours worked on day "+week_day+" is "+emp.hoursWorkedOnday(week_day));
      
   }
}

The class diagram for HW 1, Problem 2 is shown below. Employee Tester Employee -hours:double[7] main(argsO:string) -name:string +Employee (name: string) +getHours(day:int):double +setHours (day:int, hrs: double) +numaDaysWorked0:int +totalHours0: double +toString0:string 1. Copy the Employee and Employee Tester classes from HW 1, Problem 2 into your project in the Prob2 folder/package 2. Add a wages method to the Employee class. This method accepts a pay rate ($/hr) that the employee earns. The method computes the pay by multiplying the total hours by the pay rate (e.g. no overtime). Hint: all you have to do is multiply the result of totalHours0 by the pay rate. Example (assume pay rate of $10/hr) 3. Add a subclass, HourlyEmployee. Override the wages method so that it computes and returns the pay using the pay rate for the first 40 hours and time-and-a-half for any hours over 40.0. Four examples (assume pay rate of $10/hr): hours M Tu W ITh F Sa Su Reg Wages ICOT Wages Total Wages 10 10 10 10 10 6 0 $400.00 $240.00 S640.00 hours IM Tu TW Th F Sa Su Reg Wages OT Wages Total Wages 10 0 10 10 0 4 0 $340.00 $0.00 $340.00 hours M Tu W ITh IF Sa Su Reg Wages TOT Wages Total Wages 10 10 10 10 100 0 0 $400.00 $150.00 $550.00

Explanation / Answer

import java.util.*;


class Employee {
    private double[] hours=new double[7];
    private String name;
    
    public Employee(double[] hours, String name) {
        super();
        this.hours = hours;
        this.name = name;
    }
  
    public double[] getHours() {
        return hours;
    }
  
    public void setHours(double[] hours) {
        this.hours = hours;
    }
  
    public String getName() {
        return name;
    }
  
    public void setName(String name) {
        this.name = name;
    }
   
    public int numDaysWorked(){
        int total_D=0;
        for(int i=0;i<7;i++){
            if(hours[i]!=0.0){
                total_D=total_D+1;
            }
        }
        return total_D;
    }
  
    public double totalHours(){
        double total_H=0;
            for(int i=0;i<7;i++){
                total_H=total_H+hours[i];
            }

        return total_H;
      
    }
    public double hoursWorkedOnday(int day){
        return hours[day];
    }
    public double wages(double payRate) //compute wages for an employee
    {
       return totalHours()*payRate;
    }
  
    public String toString(){
       return name+" Worked for " +numDaysWorked() + " day(s) for a total of "+totalHours() + " hours.";
    }
}
class HourlyEmployee extends Employee
{
   public HourlyEmployee(double[] hours, String name)
   {
        super(hours,name);
  
   }
   public double wages(double payRate)   // compute wages for hourly employee
    {
       return (40*payRate + (totalHours()-40)*payRate*1.5);
    }
  
  
}
class SalariedEmployee extends Employee
{
   public SalariedEmployee(double[] hours, String name)
   {
        super(hours,name);
  
   }
   public double wages(double payRate) //compute wages for salaried employee
    {
       return (hoursWorkedOnday(0)+hoursWorkedOnday(1)+hoursWorkedOnday(2)+hoursWorkedOnday(3)+hoursWorkedOnday(4))*payRate + (hoursWorkedOnday(5)+hoursWorkedOnday(6))*payRate*1.5;
    }
  
}

class EmployeeTester {
    private static Scanner employee;
  
    public static void main(String[] args) {
        String name,type;
        int week_day=0;
        double payRate;
      
        employee = new Scanner(System.in);         
        System.out.print(" Enter a name : ");
        name=employee.next();
        System.out.print(" Enter an employee type (e,h or s) : ");
        type=employee.next();
        System.out.print(" Enter hours worked (7 values separated by spaces):");
        double hours[]=new double[7];
        for(int i=0;i<7;i++){
            hours[i]=employee.nextDouble();
        }
        System.out.print(" Enter pay rate : ");
        payRate=employee.nextDouble();
        if(type.equals("e"))
        {
        Employee emp=new Employee(hours, name);
        System.out.println(emp.toString());
        System.out.println("Total Wages = $"+ emp.wages(payRate));
        //System.out.print(" Enter a day of the week (0-6):");
        //week_day=employee.nextInt();
        //System.out.println(" Hours worked on day "+week_day+" is "+emp.hoursWorkedOnday(week_day));
        }
        else if(type.equals("s"))
        {
        Employee emp=new SalariedEmployee(hours, name);
        System.out.println(emp.toString());
        System.out.println("Total Wages = $"+ emp.wages(payRate));
        //System.out.print(" Enter a day of the week (0-6):");
       // week_day=employee.nextInt();
        //System.out.println(" Hours worked on day "+week_day+" is "+emp.hoursWorkedOnday(week_day));
        }
        else if(type.equals("h"))
        {
        Employee emp=new HourlyEmployee(hours, name);
        System.out.println(emp.toString());
        System.out.println("Total Wages = $"+ emp.wages(payRate));
        //System.out.print(" Enter a day of the week (0-6):");
        //week_day=employee.nextInt();
        //System.out.println(" Hours worked on day "+week_day+" is "+emp.hoursWorkedOnday(week_day));
        }
      
    }
}

output: