# Please help... Can somebody fix this for me?

Printable View

• August 10th, 2011, 04:35 PM
Mini83
Please help... Can somebody fix this for me?
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;
}
}
• August 10th, 2011, 05:09 PM
Norm
Re: Please help... Can somebody fix this for me?
Please edit your code and wrap it in code tags. See: BB Code List - Java Programming Forums

Does the program compile without errors? If not, copy and paste them here.
What does the program currently output? Please copy and paste its output here.

Quote:

I don not know how to do the rest.
Please explain what parts are missing and why you are having problems with them.