Search code examples
javaarraylistbluej

What do I need to change for this function to calculate the corrrect average?


I'm very new to Java, and I'm trying to create a class Average whose object calculates the average of a number of integers (objects of another class Integers) that can be added to it via the addNumber method first.

I can see that the correct numbers are added to the object and that the counter works correctly too, but I just can't seem to figure out how to calculate the sum of the ArrayList so that I can then calculate the average of these numbers by dividing its sum by my counter.

For this line here, I keep getting the error message that Integer cannot be converted to int:

for (int i : variables)

Which makes sense, but so far none of the ways I tried to convert it to one seems to work. I actually copied that one line of code from here (https://coderanch.com/t/673671/java/correctly-Sum-contents-ArrayList-Integer) but for me that really doesn't seem to work. Do I need to import anything besides import java.util.*; ?

public class Average
{
    // Ints given by the user
    private ArrayList<Integer> variables;
    private int count = 0;
    private int sum;
    
    // Resulting average
    private int average;
   

    /**
     * When creating object, takes numbers from class Integer
     */
    public Average()
    {
        variables = new ArrayList<Integer>();
    }
    /**
     * Add variables to calculate averages from
     */
    
    public void addNumber(Integer newNumber)
    {
        variables.add(newNumber);
        count ++;      
    }
    /**
     * Method to calculate the average of the given variables
     */
    
    public int calcAverage(ArrayList<Integer> variables)
    {
        for (int i : variables)
        {
            sum += i;
        }
        return sum;
        average = sum / count;
    }    
}

Solution

  • There are a few issues with the code you shared here.

    First, from an API perspective, why would calcAverage take an ArrayList<Inetger> as a parameter if the class has the variables data member? Such a method should either be static or take no argument and calculate the average of the member.

    Second, from a coding perspective, it returns the sum before assigning the value to the average, making the line average = sum / count unreachable code, and causing a compilation error.

    Third, from a mathematical perspective, since sum and count are ints, their division will be performed as integer division, giving you the wrong result in most cases.

    Finally, the Average class doesn't have any access methods to retrieve the numbers added to it, or modification methods to modify this list other than adding numbers to it. Taking this API under consideration, it may be redundant to hold a list of numbers. Instead, you could just hold the sum and count of the numbers being added, and return their average on demand:

    public class Average {
        private int count = 0;
        private int sum = 0;
        
        /**
         * Add variables to calculate averages from
         */
        public void addNumber(int newNumber) {
            sum += newNumber;
            count++;
        }
    
        /**
         * Method to calculate the average of the given variables
         */
        public double calcAverage() {
            return ((double) sum) / count;
        }    
    }