I am writing a Java code with displays a histogram of rainfall (in millimeter) on days 0 to 6, ie. day0 is Monday and day6 is Sunday. The code is I have written for the average rainfall over 7 days is the following:
public float averageRain() {
int d;
float total = 0.0f;
for (d = 0; d < days; d++)
total=total + rain[d];
return(total / days);
}
But I am stymied on how to solve the following: If you enter a day number which is less than 0 or greater than 6, you get an error message. My task is to modify a method called "addRain" to check whether the day is in range (i.e. 0 or greater, but less than constant days). If the day is valid, add RAINFALL to the total for the day and return true. Otherwise return false. By doing so, when I run the program, the invalid days are ignored. Also, I have to modify the code so that NEGATIVE rainfall values (eg. -3 mm) are NOT accepted. Could anyone tell me how to solve this matter? All I have done so far is this:
public boolean addRain(int day, float rainfall) {
rain[day] += rainfall;
return(true);
}
boolean, is an inadequate description for this question. In fact, this question has nothing to do with boolean expressions at all.