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

Program#2(25 points): Design and implement a Java class (name it summers tats) t

ID: 3707819 • Letter: P

Question

Program#2(25 points): Design and implement a Java class (name it summers tats) that tracks statistics for summer job salaries for a group of employees over several years. The only data field in the class is a two- dimensional array of values representing the salaries. The rows represent employees and the columns represent the years. The class constructor method takes two integers representing the number of employees and the number of years, creates the array, randomly generates the annual salaries (between $2,000 and $50,000), and fills the array The class defines the following methods: 1. A method named mostMoney() to return the index of the person having made the most money over the years 2. A method named highestsalaryYear to return the index of the year when the highest salary was earned. 3. A method named totalEarnings to return the total amount of money made by a specified person (i.e., specific row index passed as a parameter to the method) 4. A method named allEarnings) to return the total amount of money made by all the employees over the years. 5. A method named highestSalaryPerson to return the index of the person who made the highest salary in a given year (specific by column index passed as a parameter to the method). 6. A method named averageSalaryEachYear to return a one-dimensional array of the average salary for each year 7. A method named printSalaries to print the salaries array(employees' data) in tabular format as shown below (the sample output assume 3 employees and 4 years). Write a test program named testSummerStats to create an object of this class and test all of the class methods on that objects. The outputs should be organized as follows (assuming three employees and four years). Note the outputs below the employee data are from each of the methods above in that order Employee Data: Yearl Year2 Year3 Year4 Employee $37,320 Employee2 $17,100 Employe3 $49,300 44, 320 $30,220 $48,210 14, 630 $44,550 45, 420 $32, 650 31,750 49,800 Employee3 made most money over the years Highest salary was made in Year4 Total earning by Employeel is $128,920 Total earning by all Employees is $445,270. In year3, Employee3 has the highest salary The average salary for year: Yearl Year2 Year3 Year4 $34,573.33 $40,916.67 $34,866.67 $38,066. 67

Explanation / Answer

import java.util.Scanner;

import java.util.Random;//To generate random number

class SummerStats

{

int[][] employee;//declaring array to store employee year wise salaries

SummerStats(int numEmp,int numYear)

{

employee = new int[numEmp][numYear];//declaring array size

generateData(numEmp,numYear);//generating salaries of employees

}

public void generateData(int numEmp,int numYear)

{

Random rand = new Random();//rand object to generate random number

int i,j;

int sal;

for(i=0;i<numEmp;i++)

{

for(j = 0;j<numYear;j++)

{

sal = rand.nextInt(50000)+2000;//generating number between 2000 - 50000

employee[i][j] = sal;//storing in particular employee year

}

}

}

public int mostMoney()

{

//finds mostMoney earned Employee in all years

int i,j,sum,max=-1;

int rows = employee.length;

int cols = employee[0].length;

int pos = -1;

for(i = 0;i<rows;i++)

{

sum = 0;

for(j = 0;j<cols;j++)

{

sum+=employee[i][j];//sums the i th employee salary of each year

}

if(max == -1)

{

max = sum;

pos = i;

}

if(max<sum)//compares with previous maximum salary if yes then max and pos is changed

{

max = sum;

pos = i;

}

}

return pos;

}

public int highestSalaryYear()

{

int i,j;

int rows = employee.length;

int cols = employee[0].length;

int sum,max = -1,pos = -1;

for(i = 0;i<cols;i++)

{

sum = 0;

for(j = 0;j<rows;j++)

{

sum+=employee[i][j];

}

if(max == -1)

{

max = sum;

pos = i;

}

if(max<sum)

{

max = sum;

pos = i;

}

}

return pos;

}

public int totalEarnings(int pos)

{

int i,j;

int rows = employee.length;

int cols = employee[0].length;

if(pos<rows)

{

int sal = 0;

for(i = 0;i<cols;i++)

{

sal+=employee[pos][i];

}

return sal;

}

else

{

return -1;

}

}

public int allEarnings()

{

int i,j;

int rows = employee.length;

int cols = employee[0].length;

int sal = 0;

for(i = 0;i<rows;i++)

{

for(j = 0;j<cols;j++)

{

sal+=employee[i][j];

}

}

return sal;

}

public int highestSalaryPerson(int year)

{

int i,j;

int rows = employee.length;

int cols = employee[0].length;

if(year<cols)

{

int pos = -1,max =-1,sum;

for(i = 0;i<rows;i++)

{

if(max==-1)

{

pos = i;

max = employee[i][year];

}

if(max<employee[i][year])

{

max = employee[i][year];

pos = i;

}

}

return pos;

}

else

{

return -1;

}

}

public float[] averageSalaryEachYear()

{

int i,j;

int rows = employee.length;

int cols = employee[0].length;

float sum;

float [] avrgSal = new float[cols];

int pos=0;

for(i = 0;i<rows;i++)

{

sum= 0;

for(j = 0;j<cols;j++)

{

sum+=employee[i][j];

}

avrgSal[pos] = sum/cols;

pos++;

}

return avrgSal;

}

public void print()

{

int i,j;

int rows = employee.length;

int cols = employee[0].length;

System.out.println("Employee Data: ");

for(i=0;i<cols;i++)

{

System.out.print(" Year"+i);

}

System.out.println(" ----------------------------------------------------");

for(i = 0;i<rows;i++)

{

System.out.print("Employee"+i+" ");

for(j = 0;j<cols;j++)

{

System.out.print(employee[i][j]+" ");

}

System.out.println("");

}

}

public static void main(String args[])

{

SummerStats ob = new SummerStats(3,3);

ob.print();

System.out.println("");

System.out.println("Employee"+(ob.mostMoney())+" made most money over ther years");

System.out.println("Hightes salary was made in Year"+(ob.highestSalaryYear()));

System.out.println("Total earning by Employee0 is $"+ob.totalEarnings(0));

System.out.println("Total earning by all Employees is $"+ob.allEarnings());

System.out.println("In Year 2, Employee"+ob.highestSalaryPerson(2)+" has the gighest salary");

float[] avgSal = ob.averageSalaryEachYear();

int i;

for(i=0;i<avgSal.length;i++)

{

System.out.print(" Year"+i);

}

System.out.println(" -----------------------------------------------------");

for(i=0;i<avgSal.length;i++)

{

System.out.print(" $"+avgSal[i]);

}

}

}