public class ReturnArrayDemo
1:
2: import java.util.Scanner;
3:
4: /**
5: A demonstration of a method that returns an array.
6: */
7: public class ReturnArrayDemo
8: {
9: public static void main(String[] args)
10: {
11: Scanner keyboard = new Scanner(System.in);
12: System.out.println("Enter your score on exam 1:");
13: int firstScore = keyboard.nextInt( );
14: int[] nextScore = new int[3];
15:
16: for (int i = 0; i < nextScore.length; i++)
17: nextScore[i] = firstScore + 5 * i;
18:
19: double[] averageScore = getArrayOfAverages(firstScore, nextScore);
20: for (int i = 0; i < nextScore.length; i++)
21: {
22: System.out.println("If your score on exam 2 is " +
23: nextScore[i]);
24: System.out.println("your average will be " +
25: averageScore[i]);
26: }
27: }
28:
29: public static double[] getArrayOfAverages(int firstScore, int[] nextScore)
30: {
31: double[] temp = new double[nextScore.length];
32: for (int i = 0; i < temp.length; i++)
33: temp[i] = getAverage(firstScore, nextScore[i]);
34:
35: return temp;
36: }
37:
38: public static double getAverage(int n1, int n2)
39: {
40: return (n1 + n2) / 2.0;
41: }
42: }