Write a RainFall class that stores the total rainfall for each of 12 months of a
ID: 3783073 • Letter: W
Question
Write a RainFall class that stores the total rainfall for each of 12 months of a year into an array of doubles.
The RainFall class constructor method accepts an array of double values representing the amount of rainfall in inches per month.
The program should have 4 methods that return the following:
1) Total rainfall for the year
2) Average monthly rainfall
3) Month with the most rain
4) Month with the least rain
Other requirements that must be met!
FIELDS : monthNames : private java.lang.String[] monthNames
Values : private double[] values
Constructors : RainFall () : public RainFall(double[])
Methods :
getAverage(); public double
getLowestMonth() ; public (This is a String not the value of the month. Month name to be returned)
getHighestMonth ; public (This is a String not the value of the month. Month name to be returned)
getTotal ; public double
Use a String Array with an array initializer list to store the month :
names private String[] monthNames= {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
OUTPUT:
UNIT TESTING
double[] d1 = {1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.10,11.11,12.12} <-- the input
Total : 82.83
Average : 6.9025
HIghest Month : December
Lowest Month : January
Explanation / Answer
Please find the required solution:
package workspace;
public class RainFall {
private String[] monthNames = { "January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December" };
// variable used to store amount of rainfall in inches
double values[];
// One Argument constructor
public RainFall(double[] amountOfRainFall) {
this.values = amountOfRainFall;
}
// getters and setters for rainfall
public double[] getValues() {
if (values == null)
return new double[12];
return values;
}
public void setValues(double[] amountOfRainFall) {
this.values = amountOfRainFall;
}
// method returns computes and returns sum of rainfall in year
public double getTotalRainFall() {
double sum = 0;
for (double eachmonthValue : getValues()) {
sum += eachmonthValue;
}
return sum;
}
// method returns computes and returns average rainfall
public double getAverageRainFall() {
return getTotalRainFall() / values.length;
}
// method month with most rain
public String getHighestMonth() {
double max = values[0];
int result = 0;
for (int i = 1; i < getValues().length; i++) {
if (max < values[i]) {
max = values[i];
result = i;
}
}
return monthNames[result];
}
// method month with min rain
public String getLowestMonth() {
double min = values[0];
int result = 0;
for (int i = 1; i < getValues().length; i++) {
if (min > values[i]) {
min = values[i];
result = i;
}
}
return monthNames[result];
}
public static void main(String[] args) {
double[] d1 = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10,
11.11, 12.12 };
RainFall rainfall = new RainFall(d1);
System.out.println("Total: " + rainfall.getTotalRainFall());
System.out.println("Average: " + rainfall.getAverageRainFall());
System.out.println("Highest Month: " + rainfall.getHighestMonth());
System.out.println("Lowest Month: " + rainfall.getLowestMonth());
}
}
Sample output/testcase:
Total: 82.83000000000001
Average: 6.902500000000001
Highest Month: December
Lowest Month: January
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.