Design and implement a Java class (name it SummerStats.java) that tracks statist
ID: 3771072 • Letter: D
Question
Design and implement a Java class (name it SummerStats.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
If you have any further requirements, just get back to me. I tried to answer most of your methods. But running out of time by now.
Here is the SummerStats.java:
import java.io.*;
import java.util.*;
class SummerStats
{
private double [][] Salaries;
SummerStats(int numOfPeople, int numOfYears)
{
Salaries = new double[numOfPeople][numOfYears];
Random rd = new Random();
for(int i = 0; i < numOfPeople; i++)
for(int j = 0; j < numOfYears; j++)
Salaries[i][j] = rd.nextInt(500000)/100.0;
}
//A method to return the index of the person having made the most money over the years.
public int PersonMadeTheMost()
{
double Most = 0;
int personMadeTheMost = 0;
for(int i = 0; i < Salaries.length; i++)
{
double personTotal = 0;
for(int j = 0; j < Salaries[0].length; j++)
personTotal += Salaries[i][j];
if(i == 0)
{
personMadeTheMost = i;
Most = personTotal;
}
else
if(personTotal > Most)
{
personMadeTheMost = i;
Most = personTotal;
}
}
return personMadeTheMost;
}
//A method to return the index of the year when the highest salary was earned.
public int YearWhenHighestSalEarned()
{
double highestSalary = Salaries[0][0];
int yearIndex = 0;
for(int i = 0; i < Salaries.length; i++)
for(int j = 0; j < Salaries[0].length; j++)
if(Salaries[i][j] > highestSalary)
{
highestSalary = Salaries[i][j];
yearIndex = j;
}
return yearIndex;
}
//A method to return the total amount of money made by a person (specified row index).
public double TotalMadeByPerson(int personIndex)
{
double sum = 0;
for(int i = 0; i < Salaries[0].length; i++)
sum += Salaries[personIndex][i];
return sum;
}
//A method to return the total amount of money made by all the people over the years.
public double GrandTotalSalary()
{
double sum = 0;
for(int i = 0; i < Salaries.length; i++)
for(int j = 0; j < Salaries[0].length; j++)
sum += Salaries[i][j];
return sum;
}
//A method to return the index of the person who made the highest salary in a given year (specified by column index).
public int PersonMadeHighestInYear(int yearIndex)
{
int personIndex = 0;
double highestSalary = Salaries[0][0];
for(int i = 0; i < Salaries.length; i++)
if(Salaries[i][yearIndex] > highestSalary)
{
highestSalary = Salaries[i][yearIndex];
personIndex = i;
}
return personIndex;
}
//A method to return a 1-Dimensional array of the average salary for each year.
public double[] AverageSalariesOverYears()
{
double[] AverageSalaries = new double[Salaries[0].length];
for(int i = 0; i < Salaries[0].length; i++)
AverageSalaries[i] = 0;
for(int i = 0; i < Salaries[0].length; i++)
{
for(int j = 0; j < Salaries.length; j++)
AverageSalaries[i] += Salaries[i][j];
AverageSalaries[i] = AverageSalaries[i] / Salaries.length;
}
return AverageSalaries;
}
//A method that return another 2-Dimensional array sorted by the total earning per person over the years.
//public double[][] SortedByAverage()
//A method to print the salaries of the employees.
public void printSalaries()
{
for(int i = 0; i < Salaries.length; i++)
{
for(int j = 0; j < Salaries[0].length; j++)
System.out.printf("%5.2f ", Salaries[i][j]);
System.out.println();
}
}
}
Here is the SummerStatsTest.java:
import java.io.*;
class SummerStatsTest
{
public static void main(String[] args)
{
SummerStats stats = new SummerStats(5, 3);
stats.printSalaries();
System.out.println("Index of a person made the most money over the years is: "+stats.PersonMadeTheMost());
System.out.println("Index of the year in which a person earned highest salary is: "+stats.YearWhenHighestSalEarned());
System.out.printf("Total made by person 0 is: %5.2f ", stats.TotalMadeByPerson(0));
System.out.printf("Total amount of money made by all people over the years is: %6.2f ", stats.GrandTotalSalary());
System.out.println("Index of the person who made the highest salary in the year index 1 is: "+stats.PersonMadeHighestInYear(1));
System.out.print("The average salaries for each year is: ");
double[] avg = stats.AverageSalariesOverYears();
for(int i = 0; i < avg.length; i++)
System.out.printf("YearIndex: %2d Average: %5.2f ", i, avg[i]);
/*
double[][] Salaries = stats.SortedByAverage(stats.AverageSalariesOverYears());
for(int i = 0; i < Salaries.length; i++)
{
for(int j = 0; j < Salaries[0].length; j++)
System.out.printf("%5.2f ", Salaries[i][j]);
System.out.println();
}*/
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.