# Object-Oriented Programming with Java, part I – Exercise 64

This program uses the methods sum and average to calculate the average from the arraylist.

## My code

``````
import java.util.ArrayList;

public class AverageOfNumbers {

// Copy here the method sum from previous assignment
public static int sum(ArrayList<Integer> list) {
int tot = 0;
for (int num : list) {
tot = tot + num;
}
}

public static double average(ArrayList<Integer> list) {
// write code here

return ((float)sum(list)/list.size());
}

public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();

System.out.println("The average is: " + average(list));
}
}
``````

## Model code

``````import java.util.ArrayList;

public class AverageOfNumbers {

// Copy here the method sum from previous assignment

public static int sum(ArrayList<Integer> list) {

int sum = 0;

for (int number : list) {

sum = sum + number;

}

return sum;

}

public static double average(ArrayList<Integer> list) {

// write code here

// forst the sum of variables is calculated and saved in double variable

double sum = (double) sum(list);

// average is then easy to get with dividing sum by the list length

return sum / list.size();

}

public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<Integer>();

System.out.println("The average is: " + average(list));

}

}``````