Can somebody show me how this one is done. I don not know how to do the rest. I have attached what I have so far. Would highly appreciate it. I would like it to print out an amount.
Write a rainfall class that stores the total rainfall for each of 12 months into an array of doubles. The program should should have methods that return the following:
1. the total rainfall for the year
2. the average monthly rainfall
3. the month with the most rain
4. the month with the least rain
Demonstrate the class in a complete program
Input Validation: Do not accept negative numbers for monthly rainfall figures.
public class Rainfall {
double monthlyAmount[] = new double[12];
public Rainfall() { }
public void setMonthlyAmount(int monthNo, double amt) {
monthlyAmount[monthNo] = amt;
}
public double getTotalRainfall() {
double total = 0.0;
for (int i = 0; i < monthlyAmount.length; ++i)
total += monthlyAmount[i];
return total;
}
public double getAverageRainfall() {
return getTotalRainfall() / (double) monthlyAmount.length;
}
public double getLeastRainMonth() {
int minIdx = 0;
double minAmt = monthlyAmount[0];
for (int i = 1; i < monthlyAmount.length; ++i) {
if (monthlyAmount[i] < minAmt) {
minAmt = monthlyAmount[i];
minIdx = i;
}
int most_rain_month = 11;
for(int i = 0; i < 11; i++) {
if ( monthlyAmount[i] > monthlyAmount[most_rain_month] )
most_rain_month = i;
}
return minAmt;
}
public double getMostRainMonth() {
int maxIdx = 0;
double maxAmt = monthlyAmount[0];
for (int i = 1; i < monthlyAmount.length; ++i) {
if (monthlyAmount[i] > maxAmt) {
maxAmt = monthlyAmount[i];
maxIdx = i;
}
}
return maxAmt;
}
}