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

(In Java) Design and implement a Java class (name it SummerStats.java) that enca

ID: 3569504 • Letter: #

Question

(In Java) Design and implement a Java class (name it SummerStats.java) that encapsulates statistics for summer job salaries for a group of people over several years. The only data field you need is a 2-Dimentional array of values representing salaries. The rows 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 meny made by a person (specified rwo index).

- A method to return the total amount of money made by all he people over the years.

- A method to return the index of the person who made he highest salary in a given year (specified by column index).

- A method to return a 1-Dimentional array of the average salary for each year.

- A method that return another 2-Dimentional 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 words, the array is sorted by the total of each row.

Write a test program to create an object of the class and test all of the class methods on that objects.

Explanation / Answer

Here you go :)

commnet if you have any doubts.

//SummerStats class

import java.util.Arrays;

public class SummerStats
{
   private double[][] salaries;
   public SummerStats(int n,int years)
   {
       this.salaries=new double[n][years];
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<years;j++)
           {
               this.salaries[i][j]=((int)(Math.random()*100000))/10.0;
           }
       }
   }
   public int highSalary()
   {
       double max=Double.MIN_VALUE;
       int person=-1;
       for(int i=0;i<this.salaries.length;i++)
       {
           for(int j=0;j<this.salaries[i].length;j++)
           {
               if(this.salaries[i][j]>max)
               {
                   max=this.salaries[i][j];
                   person=i;
               }
           }
       }
       return person;
   }
   public double totalSalaryByPerson(int n)
   {
       double sum=0;
       for(int i=0;i<this.salaries[n].length;i++)
       {
           sum+=this.salaries[n][i];
       }
       return sum;
   }
   public double totalSalary()
   {
       double sum=0;
       for(int i=0;i<this.salaries.length;i++)
           sum+=this.totalSalaryByPerson(i);
       return sum;
   }
   public int highSalaryOfYear(int year)
   {
       double sal=Double.MIN_VALUE;
       int person=-1;
       for(int i=0;i<this.salaries.length;i++)
           if(this.salaries[i][year]>sal)
           {
               sal=this.salaries[i][year];
               person=i;
           }
       return person;
   }
   public double[] avgSalaryOfYear()
   {
       double[] avg=new double[this.salaries.length];
       for(int i=0;i<this.salaries.length;i++)
       {
           for(int j=0;j<this.salaries[i].length;j++)
           {
               avg[i]+=this.salaries[i][j];
           }
           avg[i]=avg[i]/this.salaries[i].length;
       }
       return avg;
   }
   public double[][] sortedEarnings()
   {
       double[][] sort=new double[this.salaries.length][];
       sort=Arrays.copyOf(this.salaries, this.salaries.length);
       for(int i=0;i<this.salaries.length;i++)
       {
           for(int j=i;j<this.salaries.length;j++)
           {
               if(this.totalSalaryByPerson(i)<this.totalSalaryByPerson(j))
               {
                   double[] dum=sort[i];
                   sort[i]=sort[j];
                   sort[j]=dum;
               }
           }
       }
       return sort;
   }
   public static void main(String[] args)
   {
       SummerStats ss=new SummerStats(5, 5);
      
       System.out.println("Salaries are: ");
       for(int i=0;i<ss.salaries.length;i++){System.out.println(Arrays.toString(ss.salaries[i]));}
      
       System.out.println("Total Salary by person: ");
       for(int i=0;i<ss.salaries.length;i++){System.out.println("Person "+(i+1)+" : "+ss.totalSalaryByPerson(i));}
      
       System.out.println("Average Salary over years: ");
       System.out.println(Arrays.toString(ss.avgSalaryOfYear()));
      
       System.out.println("Sorted Salaries: ");
       double[][] sorted=ss.sortedEarnings();
       for(int i=0;i<ss.salaries.length;i++){System.out.println(Arrays.toString(sorted[i]));}
      
      
      
   }
}