Termination Phase: Calculating and Displaying the Class Average

When the loop terminates, line 26 performs the averaging calculation and assigns its result to the variable average. Line 29 uses System.out’s printf method to display the text "Total of all 10 grades is " followed by variable total’s value. Line 30 then uses printf to display the text "Class average is " followed by variable average’s value. When execution reaches line 31, the program terminates.

Notice that this example contains only one class, with method main performing all the work. In this chapter and in Chapter 3, you’ve seen examples consisting of two classes—one containing instance variables and methods that perform tasks using those variables and one containing method ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.