Calculate statistics for series of values

Calculate statistics for series of values

Programming Assignment

Rainfall Statistics

Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles.
The program should calculate and display the total rainfall for the year, the average monthly rainfall, and
the months with the highest and lowest amounts.
The program should also have the following methods:
You must write the methods below from scratch. Do not rely on any other built-in or pre-existing
methods that appear to provide any of this functionality for you. There is a high value to you in
practicing such logic.
▪ ThegetTotalRainFallmethod calculates the total rainfall. This method should accept a onedimensional array as its argument and returns the total amount of rainfall.
▪ ThegetAverageRainfallmethod calculates the average amount of rainfall. This method should
return the average amount of rainfall.
▪ ThegetHighestMonthmethod determines the month with the highest amount of rainfall. This
method should accept a one-dimensional array as its argument and return the number of the
month with the highest amount of rainfall.
▪ ThegetLowstMonthmethod determines the month with the lowest amount of rainfall. This
method should accept a one-dimensional array as its argument and return the number of the
month with the lowest amount of rainfall.
▪ ThegetRainAtmethod returns a specified value in the array. The method should accept a
specified element number as its argument and return the value stored in this element.
Input Validation: Do not accept negative numbers for monthly rainfall figures.

Solution 

importjava.util.Scanner;

public class RainfallStatistics {

public static void main(String[] args) {

Scanner user = new Scanner(System.in);

double[] rainfalls = new double[12];

// prompt the user to enter the rainfalls for 12 months

for (int i = 0; i <rainfalls.length; i++) {

System.out.printf(“Enter the rainfall (in inches) for month #%d: “, i + 1);

rainfalls[i] = user.nextDouble();

}

System.out.println();

// display the result of functions

System.out.printf(“The total rainfall for the year is %.2f inches.\n”,

getTotalRainFall(rainfalls));

System.out.printf(“The average rainfall for the year is %.2f inches.\n”,

getAverageRainfall(rainfalls));

inthighestMonth = getHighestMonth(rainfalls);

intlowestMonth = getLowestMonth(rainfalls);

System.out.printf(“The largest amount of rainfall was %.2f inches in month %d.\n”,

getRainAt(rainfalls, highestMonth – 1), highestMonth);

System.out.printf(“The smallest amount of rainfall was %.2f inches in month %d.\n”,

getRainAt(rainfalls, lowestMonth – 1), lowestMonth);

}

static double getTotalRainFall(double[] rainfalls) {

double total = 0;

// accumulate the rainfalls in the array

for (int i = 0; i <rainfalls.length; i++) {

total += rainfalls[i];

}

return total;

}

static double getAverageRainfall(double[] rainfalls) {

intlen = rainfalls.length;

// make sure the length is positive to avoid dividing by zero.

if (len> 0) {

returngetTotalRainFall(rainfalls) / len;

}

return 0;

}

staticintgetHighestMonth(double[] rainfalls) {

int month = 0;

for (int i = 0; i <rainfalls.length; i++) {

if (month == 0 || rainfalls[month – 1] < rainfalls[i]) {

// the current month (i) has a larger rainfall

month = i + 1;

}

}

return month;

}

staticintgetLowestMonth(double[] rainfalls) {

int month = 0;

for (int i = 0; i <rainfalls.length; i++) {

if (month == 0 || rainfalls[month – 1] > rainfalls[i]) {

// the current month (i) has a lower rainfall

month = i + 1;

}

}

return month;

}

static double getRainAt(double[] rainfalls, int index) {

// make sure the index is in the range to avoid out of boundary.

if (index >= 0 && index <rainfalls.length) {

return rainfalls[index];

}

return 0;

}

}