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

1. The WorkHours class should have the following fields: a. monthlyHours - an ar

ID: 3528578 • Letter: 1

Question

1. The WorkHours class should have the following fields: a. monthlyHours - an array of integers, one for each month of the year The WorkHours class should have the following methods: a. toString - displays the array as nicely formatted text, ex. The monthly work hours are: 160, 140, 155, 170, 133, 160, 160, 120, 160, 170, 160, and 140. b. totalHours - returns the total number of hours worked for the year. c. meanHours - returns the mean of the monthly work hours as a double d. mostHours - returns the month with the most hours worked (first occurrence if there's a tie) as an integer (0 = January, 1 = February, etc.) e. leastHours - returns the month with the least hours worked (first occurrence if there's a tie) as an integer (0 = January, 1 = February, etc.) 2. Write a separate program to test the functionality (each of the methods) of the WorkHours class. Remember to pay attention to proper Java style and convention. Submit .java files to this assignment in Blackboard.

Explanation / Answer

import java.util.*;

class WorkHours
{
private int[] monthlyHours;
public WorkHours()
{
monthlyHours = new int[12];
}
public void set_month_data(int k, int data)
{
monthlyHours[k] = data;
}
public void toString()
{
System.out.println(" The monthly work hours are:");
for(int i=0; i<12; i++)
{
if(i==11)
System.out.print(" and " + monthlyHours[i] );
else
System.out.print(" " + monthlyHours[i] );
}
}
public int totalHours()
{
int sum = 0;
for(int i=0; i<12; i++)
sum = sum + monthlyHours[i];
return sum;
}
public double meanHours()
{
return (totalHours()/12.0);
}

public int mostHours()
{
int max = monthlyHours[0];
int i = 0;
for(int j=1; j<12; j++)
{
if(monthlyHours[j] > max)
{
max = monthlyHours[j];
i = j;
}
}
return i;
}

public int leastHours ()
{
int min = monthlyHours[0];
int i = 0;
for(int j=1; j<12; j++)
{
if(monthlyHours[j] < min)
{
min = monthlyHours[j];
i = j;
}
}
return i;
}

public static void main(String[] args)
{
WorkHours wh = new WorkHours();
Scanner in = new Scanner(System.in);
System.out.println( " enter 12 month data ");
for(int i=0; i<12; i++)
wh.set_month_data(i,in.nextInt());
System.out.println( " total hours are " + wh.totalHours());
System.out.println( " data is " + wh.toString());
}

}