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
- Create
score. Declare anintnamedscorebelow 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 namedfinalExam, becausefinalis a reserved word in Java. - Print the result. Print
Score:followed byscorewithSystem.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)ishomework * 0, because20 / 100is 0 in integer division, so the whole part is 0. Always multiply before dividing. - Using
0.2.homework * 0.2is adouble(18.0 for a mark of 90), which does not fit into anintvariable 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.
