• (Workers)

Name Hours per day
John 5 6 4 7 8
Jack 7 3 5 7 4
Jill 2 9 4 3 5
James 6 6 2 4 5
Jacob 5 6 6 3 3
Jeremy 5 5 5 8 8
Jim 8 8 4 7 4
Jerry 6 4 5 6 7
Jamie 3 2 6 7 4
• If worker payment is $6.50 per hour, write methods to calculate:
o Average Working hours for every Worker -> Return array
o Average Payment for every worker -> Return array
o Total Working hours for every worker -> Return array
o Total payment for every worker -> Return array
o Sort according to:
 Average Pay
 Average Working hours
 Total Pay
 Total Working hours
Write a test program that tests all the methods according to the table above

To calculate the required values, you can follow these steps:

1. Create a class called "Worker" with the following attributes: name (String) and hours (array of integers).

2. Create a constructor in the "Worker" class to initialize the name and hours attributes.

3. Create a method called "getAverageWorkingHours" in the "Worker" class that calculates the average working hours for a worker. This method will calculate the sum of all the hours and divide it by the total number of working days.

4. Create a method called "getAveragePayment" in the "Worker" class that calculates the average payment for a worker. This method will multiply the average working hours by the hourly rate (which is $6.50 in this case).

5. Create a method called "getTotalWorkingHours" in the "Worker" class that calculates the total working hours for a worker. This method will calculate the sum of all the hours.

6. Create a method called "getTotalPayment" in the "Worker" class that calculates the total payment for a worker. This method will multiply the total working hours by the hourly rate.

7. Create a class called "WorkerAnalysis" which will contain the main method for the test program.

8. In the main method of the "WorkerAnalysis" class, create an array of workers and initialize it with the given data.

9. Use a loop to calculate the average working hours and average payment for each worker by calling the respective methods for each worker.

10. Use another loop to calculate the total working hours and total payment for each worker by calling the respective methods for each worker.

11. Use comparator and sorting methods to sort the array of workers according to the required criteria (average pay, average working hours, total pay, and total working hours).

12. Print the sorted array of workers to display the results.

Here is an example implementation in Java:

```java
public class Worker {
private String name;
private int[] hours;

public Worker(String name, int[] hours) {
this.name = name;
this.hours = hours;
}

public double getAverageWorkingHours() {
int sum = 0;
for (int hour : hours) {
sum += hour;
}
return (double) sum / hours.length;
}

public double getAveragePayment(double hourlyRate) {
return getAverageWorkingHours() * hourlyRate;
}

public int getTotalWorkingHours() {
int sum = 0;
for (int hour : hours) {
sum += hour;
}
return sum;
}

public double getTotalPayment(double hourlyRate) {
return getTotalWorkingHours() * hourlyRate;
}
}

public class WorkerAnalysis {
public static void main(String[] args) {
Worker[] workers = {
new Worker("John", new int[] { 5, 6, 4, 7, 8 }),
new Worker("Jack", new int[] { 7, 3, 5, 7, 4 }),
// Add the other workers here...
};

double hourlyRate = 6.50;

// Calculate average working hours and average payment for each worker
for (Worker worker : workers) {
double averageWorkingHours = worker.getAverageWorkingHours();
double averagePayment = worker.getAveragePayment(hourlyRate);
// Store the average working hours and average payment in the Worker object for future use
worker.setAverageWorkingHours(averageWorkingHours);
worker.setAveragePayment(averagePayment);
}

// Calculate total working hours and total payment for each worker
for (Worker worker : workers) {
int totalWorkingHours = worker.getTotalWorkingHours();
double totalPayment = worker.getTotalPayment(hourlyRate);
// Store the total working hours and total payment in the Worker object for future use
worker.setTotalWorkingHours(totalWorkingHours);
worker.setTotalPayment(totalPayment);
}

// Sort the array of workers according to the required criteria using comparator and sorting methods
Arrays.sort(workers, Comparator.comparing(Worker::getAveragePayment)); // Sort by average payment
// Add other sorting criteria as needed

// Print the sorted array of workers
for (Worker worker : workers) {
System.out.println(worker.getName() + ": Average Pay - " + worker.getAveragePayment()
+ ", Average Working Hours - " + worker.getAverageWorkingHours()
+ ", Total Pay - " + worker.getTotalPayment()
+ ", Total Working Hours - " + worker.getTotalWorkingHours());
}
}
}
```

This implementation calculates the required values and sorts the workers according to the specified criteria. You can customize it further as needed.

Here is a step-by-step guide to solving the problem:

1. First, create a class named Worker with the following attributes:

- name (String)
- hours (Array of integers)

Also, define the necessary constructor, getter, and setter methods for this class.

2. Add a static method named "calculateAverageHours" in the Worker class. This method takes in an array of Worker objects and returns an array containing the average working hours for each worker.

- Create an empty array called "averageHoursArray".
- Loop through each worker in the input array and calculate the average of their hours.
- Add each worker's average working hours to the "averageHoursArray".
- Return the "averageHoursArray".

3. Add another static method named "calculateAveragePayment" in the Worker class. This method takes in an array of Worker objects and returns an array containing the average payment for each worker.

- Create an empty array called "averagePaymentArray".
- Loop through each worker in the input array and calculate the average payment by multiplying their average working hours by the payment rate.
- Add each worker's average payment to the "averagePaymentArray".
- Return the "averagePaymentArray".

4. Add a static method named "calculateTotalHours" in the Worker class. This method takes in an array of Worker objects and returns an array containing the total working hours for each worker.

- Create an empty array called "totalHoursArray".
- Loop through each worker in the input array and calculate the total of their working hours.
- Add each worker's total working hours to the "totalHoursArray".
- Return the "totalHoursArray".

5. Add another static method named "calculateTotalPayment" in the Worker class. This method takes in an array of Worker objects and returns an array containing the total payment for each worker.

- Create an empty array called "totalPaymentArray".
- Loop through each worker in the input array and calculate the total payment by multiplying their total working hours by the payment rate.
- Add each worker's total payment to the "totalPaymentArray".
- Return the "totalPaymentArray".

6. Add a static method named "sortByAveragePay" in the Worker class. This method takes in an array of Worker objects and sorts them in ascending order based on their average pay.

- Use the "java.util.Arrays.sort()" method with a custom Comparator that compares workers based on their average payment.
- Return the sorted array of workers.

7. Similarly, add static methods named "sortByAverageHours", "sortByTotalPay", and "sortByTotalHours" in the Worker class to sort the array of workers based on average working hours, total pay, and total working hours respectively. Use the appropriate Comparator for each sorting method.

8. Finally, create a test program to test all the methods described above.

- Initialize an array of Worker objects with the given name and hours data.
- Call each method from the Worker class and print the results to validate the calculations and sorting.

That's it! You have now written the methods to calculate and sort the worker data. Implementing this step-by-step solution will give you the desired output.