Hey there guys, I have just completed an exercise for uni and I would really like some feedback as I'm pretty new to using java. Areas that I could especially use some input in this instance: readability of the code, the names of the variables, the javaDoc comments, correct use of error statements AND MOST IMPORTANTLY the not3TimesHigher method, which took me ages to do, I'm still not sure whether this is the best way to do it.
A warning before anybody writes anything here: Please do not suggest any overly complex updates to my code, I'm a beginner and I'm required to work within those limitations.
Anyway here is the problem sheet for reference:
A company stores the salaries of its employees in an ArrayList allSalaries, an ArrayList of arrays of type double. Each entry in allSalaries is an array of the 12 salaries of an employee for the 12 months of the year.
Write a class Salaries with the constructor • public Salaries() having no parameters to create an initially empty ArrayList. And write the following methods:
• public void add(double[] employeeSalaries) to add the salaries of one employee to the field variable allSalaries.
• The method public static double average(double[] employeeSalaries) computes the average salary for an employee. Note that any 0 entry should be disregarded, since a 0 means that the employee was not employed in that particular month. For instance, the average of {1000,1000,2000,2000,0,0,0,0,0,0,0,0} should be 1500.0 as the sum of the four non-zero values divided by 4. If all values in the annualSalaries array are zero, the method should throw an IllegalArgumentException.
• The method public ArrayList averageSalaries() generates an ArrayList storing the average salaries of all employees that have at least one non-zero monthly salary. Make use of the method average. Hint: You need to catch possible exceptions thrown by the method average.
• The method public boolean not3TimesHigher() checks whether for each employee with at least one non-zero monthly salary their average salary is not higher than three times the overall average salary of the other employees. That is, you need here the average of the averages.
import java.util.ArrayList;
public class Salaries {
private ArrayList<double[]> allSalaries;
public Salaries() {
allSalaries = new ArrayList<double[]>();
}
public void add(double[] employeeSalaries) {
allSalaries.add(employeeSalaries);
}
/**
* @param takes an array of doubles; each index of the array represents
* the earnings of an employee for that particular month.
*
* @return average salary of an employee.
*/
public static double average(double[] employeeSalaries) {
double totalSalary = 0;
int totalMonths = 0;
for (int i=0; i<employeeSalaries.length; i++) {
if (employeeSalaries[i] > 0) {
totalSalary += employeeSalaries[i];
totalMonths++;
}
if (totalSalary == 0) {
throw new IllegalArgumentException("This chump didn't earn any money!");
}
}
return totalSalary / totalMonths;
}
/**
* Method traverses allSalaries calculating the average salary for each employee
* and appending it to a newly a instantiated ArrayList.
*
* @return ArrayList containing average salaries for all employees with at least one
* monthly salary above 0.
*/
public ArrayList<Double> averageSalaries() {
ArrayList<Double> averageSalaries = new ArrayList<Double>();
try {
for (int i=0; i<allSalaries.size(); i++) {
double avgEmployeeSalary = average(allSalaries.get(i));
averageSalaries.add(avgEmployeeSalary);
}
} catch (IllegalArgumentException e) {
System.out.println("Warning, attempted to add employee with zero earnings.");
}
return averageSalaries;
}
/**
* Method creates a new instance of averageSalaries which it then traverses,
* comparing each index (average employee salary) with the total average value
* of all other indexes * 3.
*
* @return false if any employee average salary is greater than the average of
* all other employee salaries * 3, true otherwise.
*/
public boolean not3TimesHigher() {
ArrayList<Double> avgS = averageSalaries();
for (int i=0; i<avgS.size(); i++) {
double employee = avgS.get(i);
avgS.remove(i);
double[] allOtherEmployees = new double[avgS.size()];
for (int j=0; j<allOtherEmployees.length; j++) {
allOtherEmployees[j] = avgS.get(j);
}
if (employee > (average(allOtherEmployees) * 3)) {
return false;
}
} return true;
}
public static void main(String[] args) {
//double[] bowie = {2456, 1330, 0, 5470};
double[] paul = {5, 8, 4, 6};
double[] ringo = {5, 7, 4, 6};
double[] john = {0, 0, 0, 0};
double[] george = {3, 7, 9, 8};
Salaries a = new Salaries();
//a.add(bowie);
a.add(paul);
//a.add(john);
a.add(ringo);
a.add(george);
//System.out.println(Salaries.average(john));
System.out.println(a.averageSalaries());
System.out.println(a.not3TimesHigher());
}
}