public class ArrayDemo1 { public static void main(String[] args) { int[] scores = new int[5]; // declaring & instantiating an // array of student exam scores int sum = 0; // sum of exam scores double average = 0.0; // average of exam scores scores[0] = 99; // assignment statements scores[1] = 53; scores[2] = 77; scores[3] = 101; scores[4] = 81; System.out.println("The length of the array is " + scores.length); // be careful to type length instead of length() with arrays // length is a public property for arrays // (rather than a method like it is in the String class) // **************************************************** // display test scores for (int i = 0; i < scores.length; i++) // do not hardwire with i < 5 { System.out.print(scores[i] + " "); } // ***************************************************** // finding total of test scores for (int i = 0; i < scores.length; i++) { sum += scores[i]; } // computing average test score average = (double) sum/scores.length; // must cast to a double to avoid truncation due to integer division System.out.println("\nThe exam average is " + average); // ******************************************************* // adding a curve to all grades below an A for (int i = 0; i < scores.length; i++) { if (scores[i] < 90) { scores[i] += 5; } } // ****************************************************** // finding the max exam score int max = scores[0]; // maximum exam score for (int i = 1; i < scores.length; i++) { if (scores[i] > max) { max = scores[i]; } } // Note that max is initialized to scores[0] rather than the value 0 // Note also that the for loop begins with i = 1 instead of i = 0 System.out.println("The max student test score " + max); // ******************************************************* // finding an F boolean found = false; // flag variable to find failing exam score int position = -1; // array position of 1st failing exam score for (int i = 0; i < scores.length; i++) { if (scores[i] < 60) { found = true; position = i; break; } } if (found) // unnecessary to type out if (found == true) { System.out.println("The first failing exam score was found " + "in position " + position); } else { System.out.println("No failing exam scores were found."); } // ******************************************************* // initializing a parallel array with student names String[] names = {"Alice", "Bob", "Chris", "Dave", "Emma"}; if (position > -1) { System.out.println(names[position] + " failed the exam with a " + " score of " + scores[position]); } } }