ExamReview

import java.util.*;

public class ExamReview

{

    public int forEachLoop(ArrayList<Integer> arr)

    {

        // Add up the elements of the array parameter

        

        int sum = 0;

        

        for (int foo : arr)

        {

            sum = sum + foo;

        }

        

        return sum;

    }

    

    public static int nthTerm (int a, int r, int n)

    {

        if (n < 1)

            return -1;

        else

        {

            int result = a; // in case n is 1

            

            while (n > 1) // Want loop to run (n - 1) times

            {

                result = result * r;

                n = n - 1;

            }

            

            return result;

        }

        // return a * (r^(n-1))

    }

    

}

This page was last modified on 8/25/09