import java.io.*; /** * Summer adds the values in a after application of the function func * * @author CS1711 Instructors * @version 2003-10-15 */ public class Summer { // instance variables private int[] a; private int sum; /** * Constructor for objects of class Summer */ public Summer(int aSize) { // initialise instance variables a = new int[aSize]; } /** * this gets terminal input for new values in a * */ public void readInput() { try{ InputStreamReader reader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(reader); System.out.println("Enter " + a.length + " numbers, one per line:"); for (int i = 0; i < a.length; i++){ System.out.print(">"); String input = console.readLine(); a[i] = Integer.parseInt(input); } } catch(IOException exception) { System.out.println("Enter a number:"); } } /** * does the summing * * @return the sum of a[i] */ public int sumup() { // init sum to 0, then add up sum = 0; for (int i =0; i < a.length; i++){ sum += func(i); } return sum; } /** * applies a function to a[i] * * @return the function applied to a[i] */ public int func(int i) { return a[i]*a[i]; } /** * tests the sumup and readInput methods * */ public void sumTester() { // init set a[i] = i for (int i =0; i < a.length; i++){ a[i] = i+1; } int theSum = sumup(); System.out.println("The sum is " + theSum); readInput(); theSum = sumup(); System.out.println("The sum of entries is " + theSum); } }