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

Write a class (and a client class to test it) that encapsulates statistics for s

ID: 3812963 • Letter: W

Question

Write a class (and a client class to test it) that encapsulates statistics for summer job salaries for a group of people over several years. Your only instance variable should be a two-dimensional array of values representing salaries. Dimension 1 represents the people and dimen­sion 2 represents the year of the summer job. Your constructor can simply take two integers representing the number of people and the number of years, then randomly generate the salaries and fill the array. You should include the following methods: a method returning the index of the person having made the most money over the years a method returning the year when the highest salary was earned a method returning the total amount of money made by all the people over the years

Explanation / Answer


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Sam
*/
public class Salaries {
    double sal[][];
    int m,n;

    public Salaries(int m, int n) {
        this.m = m;
        this.n = n;
      
        sal = new double[m][n];
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                sal[i][j] = Math.random()*10000;
    }
  
    public int getRichestPerson(){
        double highest;
        int index;
        double temp = 0;
        for (int i = 0; i<n; i++)
                temp += sal[0][i];
        highest = temp;
        index = 0;
        for(int i = 1; i < m; i++){
            temp = 0;
            for (int j = 0; j< n; j++)
                temp += sal[i][j];
            if (temp > highest){
                highest = temp;
                index = i;
            }
        }
        return index;
    }
    public int getRichestYear(){
        double highest;
        int index;
        double temp = 0;
        for (int i = 0; i<m; i++)
                temp += sal[i][0];
        highest = temp;
        index = 0;
        for(int i = 1; i < n; i++){
            temp = 0;
            for (int j = 0; j< m; j++)
                temp += sal[j][i];
            if (temp > highest){
                highest = temp;
                index = i;
            }
        }
        return index;
    }
  
    public double getTotal(){
        double temp = 0;
      
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                temp += sal[i][j];
        return temp;
    }
}

class mainTestClass{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter number of person: ");
        int m = Integer.parseInt(br.readLine());
        System.out.println("Enter number of year: ");
        int n = Integer.parseInt(br.readLine());
        Salaries s = new Salaries(m, n);
      
        System.out.println("Highest paid person is at index: "+s.getRichestPerson());
        System.out.println("Highest paid year is at index: "+s.getRichestYear());
        System.out.println("total: "+s.getTotal());
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote