Source of ReturnArrayDemo.java


  1: 
  2: import java.util.*;
  3: 
  4: /**
  5:  A program to demonstrate a method returning an array.
  6: */
  7: public class ReturnArrayDemo
  8: {
  9:     public static void main(String[] args)
 10:     {
 11:         System.out.println("Enter your score on exam 1:");
 12:         Scanner keyboard = new Scanner(System.in);
 13:         int firstScore = keyboard.nextInt( );
 14:         int[] nextScore = new int[3];
 15:         int i;
 16:         for (i = 0; i < nextScore.length; i++)
 17:             nextScore[i] = 80 + 10*i;
 18:         double[] averageScore;
 19:         averageScore = averageArray(firstScore, nextScore);
 20:         for (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[] averageArray(int firstScore,
 30:                                               int[] nextScore)
 31:     {
 32:         double[] temp = new double[nextScore.length];
 33:         int i;
 34:         for (i = 0; i < temp.length; i++)
 35:             temp[i] = average(firstScore, nextScore[i]);
 36:         return temp;
 37:     }
 38: 
 39:     public static double average(int n1, int n2)
 40:     {
 41:         return (n1 + n2)/2.0;
 42:     }
 43: }