// File: DataListTester.java (requires Java 1.5 or newer) // Purpose: reads any number of integer test scores entered by the user, // and computes and prints the average, largest score, and deviation // of each score from the average. // The DataList class has an instance variable that is an ArrayList, and // methods that add a value to the list, return the largest value, return // the average value, and print a table showing each value and its deviation // from the average. // Demonstrates // 0. a generic "ArrayList of Integer" // 1. traversing an ArrayList using a for statement // 2. ArrayList methods size, add, and get // 3. Autoboxing and autounboxing import java.util.* ; // for ArrayList and Scanner class DataList { private ArrayList list ; // ArrayList instance var // constructor creates an empty list public DataList() { list = new ArrayList() ; } // add a score to the list public void add(int score) { list.add(score) ; // autoboxing } // is the list empty? public boolean isEmpty() { return list.size() == 0 ; } // compute and return the average of the scores public double getAverage() { int sum = 0 ; // initialize accumulator // for each item on the list for (int index = 0 ; index < list.size() ; index++) { sum = sum + list.get(index) ; // autounboxing } // compute the floating point average double average = (double)sum / list.size() ; return average ; } // return the largest score public int getMaximum() { if ( isEmpty() ) // if empty list, return 0 return 0 ; int maxPos = 0 ; // assume largest is in position 0 // for all other items on the list for (int index = 1 ; index < list.size() ; index++) { // if current int > largest so far... if ( list.get(index) > list.get(maxPos) ) maxPos = index ; // update position of largest } // return the int at position "maxPos" of list return list.get(maxPos) ; } // print table showing all values and their deviations from the average public void printTable() { if ( this.isEmpty() ) // if no data entered... { System.out.println("Sorry, there was no data entered!") ; return ; // ...we're outa here! } // print headings System.out.println ("\n" + Align.right("Score",10) + Align.right("Deviation",14)) ; System.out.println (Align.right("=====",10) + Align.right("=========",14)) ; double average = getAverage() ; // compute average // for each item on the list... for (int index = 0 ; index < list.size() ; index++) { // get next item int score = list.get(index) ; // compute deviation of current score from average double difference = score - average ; System.out.println( Align.right(score, 10) + Align.right(difference,14,2) ) ; } System.out.println() ; } } public class DataListTester { public static void main (String[] args) { DataList data = new DataList() ; Scanner scan = new Scanner(System.in) ; // "priming" read System.out.println("Enter any number of scores separated by spaces " + "on any number of lines.\n(When done, enter " + "^Z on a line by itself)\n") ; while ( scan.hasNext() ) { int score = scan.nextInt() ; data.add(score) ; } if ( ! data.isEmpty() ) // if there was data, get statistics { data.printTable() ; double average = data.getAverage() ; System.out.println("Average score = " + Align.left(average,0,2) ) ; System.out.println("Largest score = " + data.getMaximum() ) ; } else { System.out.println( "ERROR - No data!" ) ; } } } /* sample output from 2 runs Enter any number of scores separated by spaces on any number of lines. (When done, enter ^Z on a line by itself) 81 93 87 94 92 ^Z Score Deviation ===== ========= 81 -8.40 93 3.60 87 -2.40 94 4.60 92 2.60 Average score = 89.40 Largest score = 94 Enter any number of scores separated by spaces on any number of lines. (When done, enter ^Z on a line by itself) ^Z ERROR - No data! */