0

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);  
                }
2
  • The tag you have selected, boolean, is an inadequate description for this question. In fact, this question has nothing to do with boolean expressions at all. Commented Dec 1, 2013 at 22:51
  • Just goes to go show just how rudimentary my Java skills are, I guess! Commented Dec 1, 2013 at 23:18

1 Answer 1

1

I assume days is a constant with value 7. Replace it with:

private static final int DAYS_IN_WEEK = 7;

Do you want to simply ignore invalid day numbers or rain values? Is this an interactive program? And, do you want to let people enter rain values twice for a single day? I can see someone entering 10mm for Wednesday and then trying to correct it and enter 5mm. Currently, there is no way to do so. If you know how to use exceptions, you can have your method throw java.lang.IllegalArgumentException and report it to the user so he can try again:

public void setRainForDay(int day, float rainfall) {
    if (day < 0 || day >= DAYS_IN_WEEK) {
        // Throw an IAE.
    }
    if (rainfall < 0.0f) {
        // Throw an IAE.
    }
    rain[day] = rainfall;  // Set, not add.
}

The calling routine catches the IAE, reports it to the user, and the user can try again.

Swing

I had not realized that the application was supposed to be a Swing application. In that case, throwing exceptions is not the appropriate method. Instead, one wants to have a model class, one that holds the data, and one wants to have the view classes, and one wants to have controller classes that do the actual work. And, one wants to use PropertyChangeListeners and VetoableChangeListeners. When you type -10 into the rainfall JFormattedTextField, it sends a property change event to the controller, whig must then refuse to set the model class, instead vetoing the change. Actually, it will be simpler to write and apply an InputValidator. It's best not to give the user the opportunity to err.

Sign up to request clarification or add additional context in comments.

3 Comments

Eric, thank you for the reply. Sorry, I should've been a bit clearer, it is an interactive program ("public class Rain extends JFrame implements ActionListener") where the main program "Rain" calls on methods and there are JButtons such as DriestDay and AverageRainFall etc. I'll give your method a try. Thanks again.
I didn't realize this was a Swing application. Then, there's a more appropriate way. I'll edit.
Eric, thanks for the tip about InputValidator. I'm reading up on it right now to see how it can be applied to my program. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.