Question #66361

Calculate staff payroll. Create a class named payroll that calculates staff payroll by checking on the hours worked. Use constructor to store value of hours worked and rate. Provide a methods named CalculatePayroll().

Create an extended class named overtime class if hours worked more than 40 hours. Provide a methods named CalculateOT(). For one hour RM 50.

Write an application named staffPayroll that creates an object of each class, demonstrates that all the methods work correctly. Get the user input for hours worked and rate.
1

Expert's answer

2017-03-21T02:44:06-0400

Answer on Question #66361 – Programming & Computer Science | Java

Main class

import java.util.Scanner;
public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        Payroll p1 = create(30,20.5);
        System.out.println("Payroll object with 30 hours and 20.5 rate, it's payment: "+p1.CalculatePayroll());
        Payroll p2 = create(45, 30);
        System.out.println("Payroll object with overtime and 45 hours and 30 rate, it's payment: "+p2.CalculatePayroll() );
        System.out.println("Enter your numbers, first hours, second rate or \"Q\" if you want to quit");
        while(true){
            String str = sc.nextLine();
            if(str.equalsIgnoreCase("Q"))
                break;
            int hours = Integer.parseInt(str);
            double rate = Double.parseDouble(sc.nextLine());
            Payroll p = create(hours,rate);
            System.out.println("Payment for this object is "+p.CalculatePayroll());
        }
    }
    public static Payroll create(int hours, double rate){
        if(hours > 40){
            System.out.println("Creating overtime object");
            return new Overtime(hours, rate);
        }
        System.out.println("Creating payroll object");
        return new Payroll(hours,rate);
    }
}

Payroll class

public class Payroll {
    protected int hours;
    protected double rate;
    public Payroll(int hours, double rate) {
        this.hours = hours;
        this.rate = rate;
    }
    public double CalculatePayroll(){
        return hours*rate;
    }
}

Overtime class

public class Overtime extends Payroll {
    private static final int overtimeRate = 50;
    private int overtimeHours;
    public Overtime(int hours, double rate) {
        super(40, rate);
        overtimeHours = hours - 40;
    }
    public int CalculateOT() {
        return overtimeHours*overtimeRate;
    }
    @Override
    public double CalculatePayroll() {
        return CalculateOT() + (hours * rate);
    }
}

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!
LATEST TUTORIALS
APPROVED BY CLIENTS