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: 3760944 • 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 dimension 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, and a method returning the total amount of money made by all people over the years.

Explanation / Answer

Program code:

package salary;

import java.util.Random;

import java.util.Scanner;

class Summerstatss {

double sal[][];

int noOfPersons;

int noOfYears;

public Summerstatss(int noOfPersons, int noOfYears) {

this.noOfPersons = noOfPersons;

this.noOfYears = noOfYears;

sal = new double[noOfPersons][noOfYears];

Random randomGenerator = new Random();

for (int i = 0; i < noOfPersons; i++) {

for (int j = 0; j < noOfYears; j++) {

sal[i][j] = randomGenerator.nextDouble() * 1000;

}

}

}

public int indexOfPersonWithMostMoney() {

int index = -1;

double max = sal[0][0];

for (int i = 0; i < noOfPersons; i++) {

for (int j = 0; j < noOfYears; j++) {

if (max < sal[i][j]) {

max = sal[i][j];

index = i;

}

}

}

return index;

}

public int yearWithHeighestSalary() {

int index = -1;

double max = sal[0][0];

for (int i = 0; i < noOfPersons; i++) {

for (int j = 0; j < noOfYears; j++) {

if (max < sal[i][j]) {

max = sal[i][j];

index = j;

}

}

}

return index;

}

void printsal() {

for (int i = 0; i < noOfPersons; i++) {

for (int j = 0; j < noOfYears; j++) {

System.out.printf("%.2f", sal[i][j]);

System.out.print(" ");

}

System.out.println();

}

}

public double totalAmountByAllPersons() {

double total = 0.0;

for (int i = 0; i < noOfPersons; i++) {

for (int j = 0; j < noOfYears; j++) {

total = total + sal[i][j];

}

}

return total;

}

public double[][] getsal() {

return sal;

}

public void setsal(double[][] sal) {

this.sal = sal;

}

}

public class SummerStats {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int noOfPersons;

int noOfYears;

System.out.println("Enter Number of People:");

noOfPersons = sc.nextInt();

System.out.println("Enter Number of Years:");

noOfYears = sc.nextInt();

Summerstatss stats = new Summerstatss(noOfPersons, noOfYears);

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

stats.printsal();

System.out.println(" index Of Person With Most Money:"

+ stats.indexOfPersonWithMostMoney());

int year = stats.yearWithHeighestSalary() + 1;

System.out.println(" year with highest salary:" + year);

System.out.println(" total amount of money by all people:"

+ stats.totalAmountByAllPersons());

}

}

Sample output:

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