The ArrayTest class:
Here is a program that solves Part 1
of the Review for Exam 2:
// ArrayTest.java: Review question 1, about simple arrays of int
public class ArrayTest {
public static void main (String[] args) {
// Review part 1.a.
int[] grades = {58, 92, 86, 75, 48, 79, 67};
printArray("Part 1.a.", grades);
// Review part 1.b.
for (int i = 0; i < grades.length; i++)
grades[i] += 3; // or grades[i] = grades[i] + 3;
printArray("Part 1.b.", grades);
// Review part 1.c.
for (int i = 0; i < grades.length; i++)
grades[i] *= grades[i]; // or grades[i] = grades[i]*grades[i];
printArray("Part 1.c.", grades);
// Review part 1.d.
int largest = grades[0];
for (int i = 1; i < grades.length; i++)
if (grades[i] > largest) largest = grades[i];
System.out.println("Part 1.d.: Largest grade: " + largest);
// Review part 1.e.
int indexLargest = 0;
for (int i = 1; i < grades.length; i++)
if (grades[i] > grades[indexLargest]) indexLargest = i;
System.out.println("Part 1.e.: Index of largest grade: " + indexLargest);
// Review part 1.f.
Sort.insertionSort(grades);
printArray("Part 1.f.", grades);
}
private static void printArray(String heading, int[] g) {
System.out.print(heading + ": Array grades: ");
for (int i = 0; i < g.length; i++)
System.out.print(g[i] + " ");
System.out.println();
}
}
/* Output:
Part 1.a.: Array grades: 58 92 86 75 48 79 67
Part 1.b.: Array grades: 61 95 89 78 51 82 70
Part 1.c.: Array grades: 3721 9025 7921 6084 2601 6724 4900
Part 1.d.: Largest grade: 9025
Part 1.e.: Index of largest grade: 1
Part 1.f.: Array grades: 2601 3721 4900 6084 6724 7921 9025
*/
Revision date: 2001-04-08.
(Please use ISO
8601, the International Standard.)