Write a class (and a client class to test it) that encapsulates statistics for s
ID: 3810244 • 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:
Overloaded Constructor, accessor, mutator, toString(), and equals()
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
SalaryStatistics.java
import java.util.Arrays;
import java.util.Random;
public class SalaryStatistics {
// Declaring instance variable
private int sal[][];
// Parameterized constructor
public SalaryStatistics(int no_of_people, int no_of_years) {
super();
this.sal = new int[no_of_people][no_of_years];
Random r = new Random();
for (int i = 0; i < no_of_people; i++) {
for (int j = 0; j < no_of_years; j++) {
sal[i][j] = r.nextInt((75000 - 30000) + 1) + 30000;
}
}
}
// Zero argumented constructor
public SalaryStatistics() {
super();
}
// Getters and setters
public int[][] getSal() {
return sal;
}
public void setSal(int[][] sal) {
this.sal = sal;
}
/*
* this method will return the index of person who earned most salary over
* the years
*/
public int mostMoneyPersonIndex() {
int sum = 0;
int index = 0;
int personTotalSal[] = new int[sal.length];
int max;
for (int i = 0; i < sal.length; i++) {
sum = 0;
for (int j = 0; j < sal[0].length; j++) {
sum += sal[i][j];
}
personTotalSal[i] = sum;
}
max = personTotalSal[0];
for (int i = 0; i < personTotalSal.length; i++) {
if (max < personTotalSal[i]) {
max = personTotalSal[i];
index = i;
}
}
return index;
}
/*
* This method will return in which year the person receiver the highest
* salary
*/
public int highestSalaryYear() {
int max = sal[0][0];
int year = 0;
for (int i = 0; i < sal.length; i++) {
for (int j = 0; j < sal[0].length; j++) {
if (max < sal[i][j]) {
max = sal[i][j];
year = j;
}
}
}
return year;
}
// This method will return the total salary earned by all the people
public int totalMoney() {
int sum = 0;
for (int i = 0; i < sal.length; i++) {
for (int j = 0; j < sal[0].length; j++) {
sum += sal[i][j];
}
}
return sum;
}
// This method will display all persons salary over the years
@Override
public String toString() {
for (int i = 0; i < sal.length; i++) {
for (int j = 0; j < sal[0].length; j++) {
System.out.print(sal[i][j] + " ");
}
System.out.println();
}
return "";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SalaryStatistics other = (SalaryStatistics) obj;
if (!Arrays.deepEquals(sal, other.sal))
return false;
return true;
}
}
___________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
int no_of_people, no_of_years;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the inputs entered by the user
System.out.print("Enter the no of people :");
no_of_people=sc.nextInt();
System.out.print("Enter the no of Years :");
no_of_years=sc.nextInt();
//Creating the Class object
SalaryStatistics ss=new SalaryStatistics(no_of_people, no_of_years);
//Displaying the salaries of all persons
System.out.println("Displayiong the salaries of the persons in each year :");
ss.toString();
System.out.println("Index of the Person who made highest salary over the years is "+(ss.mostMoneyPersonIndex()+1));
System.out.println("The year when the highest salary was earned :"+(ss.highestSalaryYear()+1));
System.out.println("Total money earned by all the persons is :"+ss.totalMoney());
}
}
______________________
Output:
Enter the no of people :5
Enter the no of Years :6
Displayiong the salaries of the persons in each year :
49261 73511 30654 44701 41727 74210
38144 64184 67407 61341 34686 37533
66751 57445 69423 72853 47341 50107
43635 30956 54350 68140 53215 73289
33066 59988 42001 66310 37188 68989
Index of the Person who made highest salary over the years is 3
The year when the highest salary was earned :6
Total money earned by all the persons is :1612406
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.