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;
    }
        return tot;
    }
    

    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>();
        list.add(3);
        list.add(2);
        list.add(7);
        list.add(2);

        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>();

        list.add(3);

        list.add(2);

        list.add(7);

        list.add(2);

 

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

    }

}

Comments

My code is basically the same as the model code although I have used float and they have used double.

Leave a comment