Design and implement a Java class (name it Summer Stats. java) that tracks stati
ID: 3693046 • Letter: D
Question
Design and implement a Java class (name it Summer Stats. java) that tracks statistics for summer job salaries for a group of people over several years. The only data field you need is a 2- Dimenssional array of values representing salaries. The rows the represents the people and the columns represent the years. The constructor method takes two integers representing the number of people and the number of years, then randomly generates the annual salaries and fills the array. Other class methods include the following: A method to return the index of the person having made the most money over the years. A method to return the index of the year when the highest salary was earned. A method to return the total amount of money made by a person (specified row index). A method to return the total amount of money made by all the people over the years. A method to return the index of the person who made the highest salary in a given year (specified by column index). A method to return a 1-Dimensional array of the average salary for each year. A method that return another 2-Dimensional array sorted by the total earning per person over the years. For example, if person in index 4 has the most earning over the years, then that would be the first row in the returned array. In other word, the array is sorted by the total of each row. Write a test program to create an object of this class and test all of the class methods on that objects. Use proper labels in your output. Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable.Explanation / Answer
Any queries please comment
public class SummerStats
{
private int [][] salaries;
public SummerStats(int numPeople, int numYears)
{
salaries = new int[numPeople][numYears];
java.util.Random r = new java.util.Random();
for(int i=0; i<numPeople; i++)
{
for(int j=0; j<numYears; j++)
{
salaries[i][j] = r.nextInt(100000);
}
}
}
public int [][] getSalaries()
{
return salaries;
}
public int getMostMoneyOverYears()
{
int [] tmp = new int[salaries[0].length];
int k = 0;
for(int i=0; i<salaries.length; i++)
{
for(int j=0; j<salaries[i].length; j++)
{
tmp[k] += salaries[i][j];
}
k++;
}
return getMax(tmp);
}
private int getMax(int [] arry)
{
int sal1=0;
int idx=-1;
for(int i=0; i<arry.length; i++)
{
if(sal1 <arry[i])
{
sal1= arry[i];
idx = i;
}
}
return idx;
}
public int getHighestSalary()
{
int [][] tmp = new int [salaries.length][2];
int max = 0;
int idxtmp = -1;
int p=0;
for(int i=0; i<salaries.length; i++)
{
for(int j=0; j<salaries[i].length; j++)
{
if(max < salaries[i][j])
{
max = salaries[i][j];
idxtmp = j;
}
}
tmp[p][0] = max;
tmp[p][1] = idxtmp;
p++;
}
max = 0;
idxtmp = -1;
for(int i=0; i<tmp.length; i++)
{
if(max < tmp[i][0])
{
max = tmp[i][0];
idxtmp = tmp[i][1];
}
}
return idxtmp;
}
public int getTotalSalary (int person)
{
int totalSalary = 0;
for(int i=0; i<salaries[person].length; i++)
{
totalSalary+=salaries[person][i];
}
return totalSalary;
}
public int getAllSalaries()
{
int sum = 0;
for(int i=0; i<salaries.length; i++)
{
for(int j=0; j < salaries[i].length; j++)
{
sum += salaries[i][j];
}
}
return sum;
}
}
import java.util.Scanner;
public class summerclasstest
{
public static void main(String [] args)
{
SummerStats ssObj = new SummerStats(2, 3);
int [][] sal = ssObj.getSalaries();
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
System.out.println("Salary at " + i + " " + j + " is $" + sal[i][j]);
}
}
System.out.println();
System.out.println("Index of the big baller: " + ssObj.getMostMoneyOverYears());
System.out.println();
System.out.println("Index of the year with the highest salary: " + ssObj.getHighestSalary());
System.out.println();
Scanner scan = new Scanner(System.in);
System.out.println("Enter the person name you want to know the salary ");
int person = scan.nextInt();
System.out.println();
System.out.println("Total money made by person at row " + person + " is " + ssObj.getTotalSalary(person));
System.out.println("The total salaries of all individuals is " + ssObj.getAllSalaries());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.