Start learning free

LESSON 198 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Report Card V2 in Java: Weighted Score with int Math

Practice Read the full explanation

Combine variables and arithmetic to compute a student's weighted final score.

Weights: Homework 20%, Midterm 30%, Final 50%. Each weighted part is rounded down to a whole number before the parts are added.

You start with:

int homework = 90;
int midterm = 82;
int finalExam = 88;

Build:

Score: 86

Rules:

  • Compute score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100.
  • Each part is rounded down because int divided by int drops the remainder (the third variable is named finalExam).
  • For the starting data: 18 + 24 + 44 = 86.
  • Print "Score: " followed by the score, for example System.out.println("Score: " + score);

The third variable is named finalExam. int / int truncates, so each weight already rounds down.

int homework = 90;
int midterm = 82;
int finalExam = 88;

int score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100;
System.out.println("Score: " + score);

STEP 1 OF 2
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Report Card V2 in Java, explained

    Problem overview

    Version 2 of the report card computes a weighted final score. Three marks count for different shares of the result: homework is worth 20%, the midterm 30% and the final exam 50%. The marks are in int variables named homework, midterm and finalExam.

    Each weighted part is rounded down to a whole number before the parts are added. For the starting marks 90, 82 and 88, the parts are 18, 24 and 44, so the score is 86 and the program prints Score: 86.

    Approach: mark times weight, divided by 100

    A weight of 20% means 20 out of 100, so a part is the mark times the weight, divided by 100. For the homework that is homework * 20 / 100. Java evaluates * and / from left to right, so the multiplication happens first and the division second.

    The order matters because of integer division. Multiplying first keeps the number large, and the division then drops only what is left over: 90 * 20 is 1800, and 1800 / 100 is exactly 18. Each part is a whole number, and the three parts are added at the end. The weights 20, 30 and 50 add up to 100, so together the parts make up the whole score.

    Step by step

    1. Create score. Declare an int named score below the three starter variables. Write the three parts out one at a time, each as the mark times its weight divided by 100, and join them with +. The third variable is named finalExam, because final is a reserved word in Java.
    2. Print the result. Print Score: followed by score with System.out.println, joining the text and the number with +.

    The solution in Java

    int homework = 90;
    int midterm = 82;
    int finalExam = 88;
    
    int score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100;
    System.out.println("Score: " + score);
    

    Java details that matter here:

    • Operator precedence. Multiplication and division bind tighter than addition. The line therefore adds three finished parts, and no parentheses are needed.
    • Each part is rounded down. The lesson's rule is that every part is truncated before the addition. With marks 83, 77 and 91 the parts are 16, 23 and 45, so the score is 84. Adding the three products first and dividing once, (homework * 20 + midterm * 30 + finalExam * 50) / 100, gives 85 for those marks. Both forms print 86 for the starting marks, but they are different calculations, and only the first matches the rule.
    • Everything stays in int. No decimal numbers appear, so no value is stored with a fractional part, and nothing needs a cast.

    Edge cases and common mistakes

    • Dividing the weight first. homework * (20 / 100) is homework * 0, because 20 / 100 is 0 in integer division, so the whole part is 0. Always multiply before dividing.
    • Using 0.2. homework * 0.2 is a double (18.0 for a mark of 90), which does not fit into an int variable without a cast, and it breaks the rounding rule.
    • Mixing up the weights. Putting 50 on the homework and 20 on the final exam gives a different score. Each weight belongs to its own mark.
    • A perfect score. Marks of 100, 100 and 100 give 20 + 30 + 50 = 100.

    Testing

    • 90, 82 and 88 print Score: 86.
    • 100, 100 and 100 print Score: 100.
    • 83, 77 and 91 print Score: 84.
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free