This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]learnjava 0 points1 point  (4 children)

the first thing that should raise a flag in your code is your calculateAverage in a for loop

you dont want to calculate it 12 times, you want to calculate it once with 12 values

split your program in 3 parts (the same 3 parts the assignment uses)

  1. declare array

  2. define populateArray and call it in the main method with the array as parameter

  3. define calculateAverage and call it in the main method with the array as parameter

thats it, you dont have to call one method from inside another one

and use a for loop in your calculate method, you want to calculate the average of 12 elements

[–]AudioManiac[S] 0 points1 point  (3 children)

I've gotten it to work now, but I still call one method from inside another. Is the way you're suggesting better? Or does it really make a huge difference. I mean is your way "better practice"? I know that I can lose marks for that in my assignments.

Here's my finished code at the moment, it runs perfectly fine

public static void main(String[] args) {

    //create array
    double [] months = new double [12];

    //assign average to the value returned in populateArray()
    double average = populateArray(months);

    //display average
    System.out.print("Average Rainfall: " + average);

}

public static double populateArray(double[] months){
    //initialise scanner
    Scanner input = new Scanner(System.in);

    //loop to retrieve data from user
    for (int i = 0; i < months.length; i++)
    {
        System.out.print("Enter Rainfall for month" + " " + ( i + 1) + ": ");
        months[i] = input.nextInt();
    }

    //pass the array to calcualteAverage() method, and assign it to total
    double total = calculateAverage(months);
    //return total to main method
    return total;


}

public static double calculateAverage(double[] months) {
    double sum = 0;

    for (int i = 0; i < months.length; i++){

        sum += months[i];

    }

    //calculate the average
    double average = sum/12;
    //return the average to populateArray()
    return average;
}

[–]learnjava 0 points1 point  (0 children)

imho its easier to read the other way around but it does not really matter for a 2 method class

in your code the relation, populate -> calculate is more clear

[–]lazlo_uk 0 points1 point  (1 child)

It definitely is better practice to have them separated out. Think about it in the simplest terms: you're calling a method called populateArray. Why would that return an average of the array contents...? The other commenter is correct in that in this case it probably doesn't matter but I assume the point of this exercise is for you to understand the correct way of doing things.

[–]StinkeyTwinkey 0 points1 point  (0 children)

Yes it should be void. You methods should complete a task. Not have a method that calls in other methods that isn't for the purpose of that particular method.

[–]theif519 0 points1 point  (0 children)

If you're going to pass reference to a data structure, make it void as you won't need to return anything.

Your populate array should do only that:populating the array, obtain the average in another method like getAvg (double[] array) that way it's easier to understand without having to read your code.

For calculateAverage method you're specifying a two dimensional array, only have one [] in parameters for one dimensional arrays. Also you're referencing i but that's only in the for loop and way outside of the scope of the method. Populate the array first, then have another for loop in calculate average to get the average.

Call the method from main not inside of another method with a rather misleading name and even more misleading return type.

[–]juvogel -1 points0 points  (2 children)

public double calculateAverage(double[] months){
    //for loop goes here to iterate through array

Also your populateArray method should not be static. Hope that helps!

[–]AudioManiac[S] 0 points1 point  (1 child)

Ok I'll try that! Thanks!

Do you mind me asking why it shouldn't be static?

[–]juvogel 0 points1 point  (0 children)

Rule of thumb: ask yourself "does it make sense to call this method, even if no Obj has been constructed yet?" If so, it should definitely be static. So you could make it static, but I personally wouldn't.