Start learning free

LESSON 198 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Report Card V1 in Java: Average with Integer Division

Practice Read the full explanation

Compute a student's average score across three tests using integer division so the result is a whole number.

You start with:

int test1 = 85;
int test2 = 92;
int test3 = 78;

Build:

Average: 85

Rules:

  • Compute the integer average of test1, test2, and test3 using integer division (int divided by int already drops the remainder).
  • Print "Average: " followed by the average, for example System.out.println("Average: " + average);

Java's int / int truncates, so (test1 + test2 + test3) / 3 already gives an integer.

int test1 = 85;
int test2 = 92;
int test3 = 78;

int average = (test1 + test2 + test3) / 3;
System.out.println("Average: " + average);

STEP 1 OF 2
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Report Card V1 in Java, explained

    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

    1. Create average. Declare an int named average below 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.
    2. Print the result. Join the text Average: and the number with + inside System.out.println. When + has a String on 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 / 3 divides 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.0 makes the division use double and gives 88.33333333333333 for 85, 90 and 90. That is not what this lesson wants, because the average is stored in an int and printed as a whole number.
    • Text plus number. In "Average: " + average the space inside the quotes is part of the output. Leave it out and the output becomes Average:85.

    Edge cases and common mistakes

    • Dividing before adding. test1 / 3 + test2 / 3 + test3 / 3 truncates 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 average without calling System.out.println produces 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.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free