# Hours Worked Problem using Java Homework Solution

Employee Working Hour Calculator

Following are two arrays, time_started, time_ended, of an employee’s working hours over ten business days (Monday to Friday and the following Monday to Friday). Times are based on a 24-hour clock (between 0 and 24).

double []time_started = {2, 4, 5, 7, 8, 8, 10, 11, 19, 22};

double []time_ended = {8, 9, 22, 18, 20, 23, 22, 20, 23, 24};

The hourly pay of the employee is \$25, for hours worked between 9 am and 6 pm. Use the pay multiplier in the table shown below to calculate the hourly pay for hours worked throughout the day.

Times based on a 24 clock Pay Multiplier

12 am to 6 am (0 to 6) 20%

6 am to 9 am (6 to 9) 10%

9 am to 6 pm (9 to 18) 0%

6 pm to 9 pm (18 to 21) 10%

9 pm to 12 pm (21 to 24) 20%

Sample of Expected output:

Time work started = 4

Time work ended = 16

Daily pay = (6 – 4) 25 (1 + 20/100) + (9 – 6) 25 (1 + 10/100) + (16 – 9) 25 = \$317.50

Write a class file called lastname_empData. Write a constructor to initialize the two arrays.

Also, write a method called getDailyPay to compute and return each of the ten daily pay of the employee and a method called getTotalPay to compute and return the sum of the daily pays.

Write a lastname_demo file. Pass the two arrays into lastname_empData class and print each of the daily pays and the total pay of the employee after 10 days.

It is also acceptable to write the entire code in a single file but both methods, getDailyPay, and getTotalPay must be included.

Note: lastname is your lastname. Last name = Dominguez

Hint: your constructor should like follows:public lastname_empData (etcetc){

}

and not like this: public emp data (etcetc){

}

Please pay attention, I am not going to do this for you when correcting assignments.

Solution:

Lastname_empData

public class lastname_empData {

private int[] time_started;

private int[] time_ended;

private int[][] pay_multipliers;

private double hourly_pay;

public lastname_empData(int[] t_started, int[] t_ended, int[][] pay_mult, double rate)

{

time_started = t_started;

time_ended = t_ended;

pay_multipliers = pay_mult;

hourly_pay = rate;

}

public double getDailyPay(int startTime, int endTime)

{

double pay = 0.0;

// get number of days worked

int hours = (startTime - endTime);

double start = startTime;

for(int i = 0; i < pay_multipliers.length; i++)

{

double from = pay_multipliers[i][0];

double to = pay_multipliers[i][1];

double multiplier = pay_multipliers[i][2];

if(to > endTime)

to = endTime;

if(start >= from && start<= to)

{

pay += (to - start)*hourly_pay*(1.0 + multiplier/100.0);

start = to;

}

}

return pay;

}

public double getTotalPay(int startTime, int endTime)

{

double total_pay = 0;

for(int i = 0; i < time_started.length; i++)

{

total_pay += getDailyPay(time_started[i], time_ended[i]);

}

}

}

App:

public class App {

public static void main(String[] args) throws Exception {

int[]time_ended = {8,9,22,18,20,23,22,20,23,24};

int[]time_started = {2, 4, 5, 7, 8, 8, 10, 11, 19, 22};

int[][]pay_multipliers = {

{0, 6, 20},

{6, 9, 10},

{9, 18, 0},

{18, 21, 10},

{21, 24, 20}

};

double rate = 25;

lastname_empData payment = new lastname_empData(time_started, time_ended, pay_multipliers, rate);

System.out.println(payment.getDailyPay(4, 16));

System.out.println(payment.getTotalPay(4, 16));

}

}