Object-Oriented Programming with Java, part I – Exercise 96

This exercise involves iteration through a static array to print the contents.

My Code

Main


public class Main {

    public static void main(String[] args) {
        // test method here
        int[] array = {5, 1, 3, 4, 2};
        System.out.println(sum(array));
    }

    public static int sum(int[] array) {
        // Write code here
        int sum =0;
        for (int i = 0; i < array.length; i++){
            sum += array[i];
        }
        return sum;
    }
}

Model Code

Main

public class Main {

 

    public static void main(String[] args) {

        // test method here

        int[] array = {5, 1, 3, 4, 2};

        System.out.println(sum(array));

    }

 

    public static int sum(int[] array) {

        // Write code here

        int sum = 0;

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

            sum = sum + array[i];

        }

        return sum;

    }

}

Comments

Almost exactly the same.

Leave a comment