Problem overview
This is the first version of the report card project. A student has taken three tests, stored in the int variables test1, test2 and test3. The program must compute the average of the three scores as a whole number and print it after the text Average: .
With the starting scores 85, 92 and 78, the sum is 255 and 255 divided by 3 is exactly 85, so the program prints Average: 85. Later versions of the project build on this one.
Approach: add first, then divide
An average is the sum of the values divided by how many there are. Here that is the three scores added together, divided by 3.
The lesson asks for a whole number, and Java provides one without extra work. When both sides of / are int, the result is an int as well: the part after the decimal point is thrown away, not rounded. For scores of 85, 90 and 90 the sum is 265, and 265 / 3 is 88 in Java, although the exact average is 88.33.
Step by step
- Create
average. Declare anintnamedaveragebelow the three starter variables. Its value is the sum of the tests divided by 3. The addition has to be finished before the division, which means the sum goes in parentheses. - Print the result. Join the text
Average:and the number with+insideSystem.out.println. When+has aStringon one side, Java converts the number to text and glues the two together. The lesson's tests read what the program prints, so the value must reach the screen.
The solution in Java
int test1 = 85;
int test2 = 92;
int test3 = 78;
int average = (test1 + test2 + test3) / 3;
System.out.println("Average: " + average);
The details worth knowing:
- The parentheses are required. Division binds tighter than addition, so
test1 + test2 + test3 / 3divides only the last score. With 85, 90 and 90 that expression gives 205, not 88. - Truncation, not rounding. Integer division drops the fraction. With 99, 99 and 98 the sum is 296 and the result is 98, even though the exact average is 98.67. With 0, 0 and 1 it is 0.
- Doubles behave differently. Dividing by
3.0makes the division usedoubleand gives 88.33333333333333 for 85, 90 and 90. That is not what this lesson wants, because the average is stored in anintand printed as a whole number. - Text plus number. In
"Average: " + averagethe space inside the quotes is part of the output. Leave it out and the output becomesAverage:85.
Edge cases and common mistakes
- Dividing before adding.
test1 / 3 + test2 / 3 + test3 / 3truncates each score separately and can come out lower: three scores of 85 give 84 instead of 85. The parentheses version divides once, after the sum is complete. - Dividing by the wrong number. There are three tests, so the divisor is 3. Counting the variables is the way to check it.
- Forgetting to print. Calculating
averagewithout callingSystem.out.printlnproduces no output, so the lesson's tests find nothing to compare. - Changing the starting values. The program should work for any three scores, not only 85, 92 and 78. The formula uses the variables, not their current numbers.
Testing
- 85, 92 and 78 print
Average: 85. - 85, 90 and 90 print
Average: 88. - 99, 99 and 98 print
Average: 98. - 0, 0 and 1 print
Average: 0.
