The following is a program in which an
array of four Movie
instances is defined, data are wired in via an
array initializer, the sum of the ratings is determined in a for
loop, and the average of those ratings is reported via a print statement.
The Movie
class involved is the one defined in
Segment 316:
public class Demonstrate { public static void main (String argv[]) { Movie movies[] = {new Movie(5, 6, 3), new Movie(8, 7, 7), new Movie(7, 2, 2), new Movie(7, 5, 5)}; int sum = 0; for (int counter = 0; counter < movies.length; ++counter) { sum = sum + movies[counter].rating(); } System.out.print("The average rating of the " + movies.length); System.out.println(" movies is " + sum / movies.length); } } --- Result --- The average rating of the 4 movies is 16