2

So I am writing a class for this rainfall assignment, I think I could get it working but I am having a problem with the final portion of the program "Rainfall rain = new Rainfall();".

I know that I probably have some logical errors in the code so far, but I am focusing on trying to at least get it to print so I can fix those issues. Thanks!

/**
Rainfall.java calculated the total annual and average
monthly rainfall from the array. This program also returns
the month with the most rainfall and the month with the least rainfall.
*/


public class Rainfall 
{

  //set integer month to 12
  private int month = 12;

  private double[] months;

/**
    Constructor
    @param scoreArray An array of test scores
*/
  public Rainfall(double[] rainfallArray)
  {
     months = rainfallArray;
  }
//Get total annual rainfall
  public double getTotal() 
  {
     double total = 0;

    //for loop to go through the entire array to calculate the total amount of rainfall
     for (int i = 0; i < month; i++) 
     {
        total = total + months[i];
     }

     return total;
  }

//Calculate average monthly rainfall
  public double getAverage()
  {
     double total = 0; //To hold the current total amount of rainfall
     double average; //To hold the average amount of rainfall

     for (int i = 0; i < month; i++) 
     {
        total = total + months[i];
     }

    //Calculate average rainfall 
     average = total / (months.length + 1);

     return average;
  }
//Get month with the most rain
  public double getMost()
  {
     double most; //To hold the most amount of rainfall

    //set the first month in the array
     most = months[0];

     int m=0;


     for (int i = 0; i < month; i++) 
     {
        if (months[i] < most) 
        {
           m=i;
        }
     }
     return most;
  }

//Get month with the least rain
  public double getLeast()
  {
     double least; //To hold the least amount of rainfall
     int m=0;

    //set the first month in the array
     least = months[0];


     for (int i = 0; i < month; i++) 
     {
        if (months[i] < least) 
        {
           m = i;
        }
     }

     return least;
  }

  public static void main(String[ ] args) 
  {

     Rainfall rain = new Rainfall();
     rain.setMonths(); 

    //Display results of the Total, Avg, and Most and Least calculations of rainfall
     System.out.println("The total rainfall for the year: " + rain.getTotal());
     System.out.println("The average rainfall for the year: " + rain.getAverage());
     System.out.println("The month with most rain: " + rain.getMost());
     System.out.println("The month with least rain: " + rain.getLeast());
  }


}
1
  • Thanks for your help, I actually was doing this all wrong lol. I redid it and made sure I asked for the input from the user for the months rather than try to access an already existing array of numbers. The instructions were very, very confusing on the first try. Commented May 29, 2013 at 4:08

3 Answers 3

3

Change your constructor to use a varargs parameter:

public Rainfall(double... rainfallArray) {
    months = rainfallArray; // a varargs comes in as an array, ie double[]
}

Then when you construct it, you can pass any number of doubles in (including none).

ie, all of these would be fine:

Rainfall r1 = new Rainfall();
Rainfall r2 = new Rainfall(4);
Rainfall r3 = new Rainfall(4, 5, 6, 15);

Note that in the case of passing no parameters, ie new Rainfall() a array is still passed in, but it is of length zero.


If you wanted to assert that there was a particular number of months passed in, add this to your constructor:

if (rainfallarray.length != 12)
    throw new IllegalArgumentException(
        "Expecting 12 months of data, but only got " + rainfallarray.length);
Sign up to request clarification or add additional context in comments.

6 Comments

His constructor is looking for an array, not individual values.
@Blaine a varargs parameter IS an array!
...however if you want to ensure there are 12 months of data... ;)
@Blaine It's actually a convenience approach to your answer. Same end result, just easier to code because you don't have to create the array - it's implicitly created for you. Under the covers, it's probably the same code (haven't decompiled to check)
@ShadowCreeper You wold have to check the size of the array, just as you would if an array was formally passed in. See edit for such code
|
1

The problem is your constructor is expecting a double array called rainfallArray and the constructor does not contain any arguments; it's empty. Initialize with a double array and it will construct.

Like so:

double [] rain = {1.3, 2.4, 3.5, 6.2}; 
 Rainfall rain = new Rainfall(rain);
... rest of code in main

Also, your for loops should look like:

for (int i = 0; i < months.size(); i++) 
     {
        if (months[i] < most) 
        {
           m=i;
        }
     }

If you do it the way you had it before, you will get null pointer exceptions if you don't have an array that is 12 elements in size. Also, keep in mind that arrays are 0 based, so you would want to start your for loop with 1, or set your month variable to 11 if you use the logic you are using now.

Comments

0

Your Rainfall class requires data, this you have decided in the constructor (it must have an array of doubles) and you are violating your own rule when you try and construct a Rainfall object without data, thus the error. You must pass it an array of doubles (even if it doesn't contain data) for the object to exist in a valid state (as defined per your rule, the constructor).

So new Rainfall() is invalid. You must pass it a double array.

double[] data = new double[12];
//fill in data, do stuff
Rainfall rain = new Rainfall(data);

Comments

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.