About

5.4 Worked Example: the Grade Calculator

Theory: Worked Example, the Grade Calculator

This lesson puts EP (5.2), BVA (5.3) and positive/negative tests together on the professor’s second big example. Follow it step by step: it is the shape of a complete EP answer, and the professor told the class to work through it themselves.

From the slides: the specification.

  • The exam mark is out of 75. The coursework mark is out of 25.
  • Total = exam + coursework (out of 100).
  • Total ≥70\ge 70 → A. 50 to under 70 → B. 30 to under 50 → C. Under 30 → D.
  • If a mark is outside its range → fault message (FM).
  • All inputs are integers.

Step 1: Partitions from the Ranges

Each mark is a range, so each gets 1 valid + 2 invalid partitions (the first guideline of 5.2).

Exam mark (out of 75)VALID: 0 ≤ mark ≤ 75mark < 0invalidmark > 75invalid075Coursework mark (out of 25)VALID: 0 ≤ mark ≤ 25mark < 0invalidmark > 25invalid025
The input partitions on a number line. Watch the = signs: 0, 75 and 25 are valid.

Step 2: Partitions from the Type

All inputs must be integers, so a non-integer is invalid. The professor picked two kinds of wrong type: a real number (like 30.5) and a letter (like 'Q'). That adds 2 more invalid partitions for each mark. The ranges alone would never have found these: 30.5 is “inside” 0 to 75, yet it is not a legal input.

Step 3: Partitions from the Output

Do not forget the output! The total mark decides the grade, and the output values form partitions of their own along the total-mark line:

FMDCBAFM< 00 to 2930 to 4950 to 6970 to 100> 1000305070100careful: 70 → A but 69 → B; 50 → B but 49 → C; 30 → C but 29 → D
Output partitions on the total-mark line. FM = fault message.

Be careful with the edges: 70 gives A, but 69 gives B; 50 gives B, but 49 gives C; 30 gives C, but 29 gives D. These are exactly the places BVA will aim at.

Outputs that should never appear

There are also outputs the specification never mentions, so the program must never produce them: for example E, A+, or an empty (null) result. These are invalid output partitions. How many of them should you list? The professor said: enough until you feel confident, but not so many that you waste time. In this example, 3.

All the Partitions Together: 19

#WhatPartitionValid or invalid?
1Exam mark0 to 75Valid
2Exam markless than 0Invalid
3Exam markmore than 75Invalid
4Exam marka real number (e.g. 30.5)Invalid
5Exam marka letter (e.g. 'Q')Invalid
6Coursework mark0 to 25Valid
7Coursework markless than 0Invalid
8Coursework markmore than 25Invalid
9Coursework marka real numberInvalid
10Coursework marka letterInvalid
11OutputA (total 70 to 100)Valid
12OutputB (total 50 to 69)Valid
13OutputC (total 30 to 49)Valid
14OutputD (total 0 to 29)Valid
15OutputFM (total less than 0)Valid
16OutputFM (total more than 100)Valid
17OutputE (never specified)Invalid
18OutputA+ (never specified)Invalid
19Outputnull / empty (never specified)Invalid

The two FM outputs are valid outputs: the specification asks for a fault message in those situations, so producing it is correct behaviour.

Step 4: From Partitions to Test Cases

First, write at least one test case per partition, and every one must include its expected output. The professor’s first test case was:

One test per partition gives 19 test cases. Can we do better? Yes. Look at the test above: it covers three partitions at once, the valid exam mark (#1), the valid coursework mark (#6) and output B (#12). When partitions overlap like this, one test can cover several of them. By choosing tests carefully, the slides reduce the 19 tests to 11.

Combining safely: valid freely, invalid one at a time

How far can you combine? The safe rule is: combine valid partitions freely, but test invalid inputs one at a time. If one test contains two bad inputs, the first error message can hide the second: the program may reject the exam mark and never even look at the coursework mark, so a missing check on the coursework goes unnoticed. This is the same idea as masking in MC/DC (3.5).

Following that rule gives a reduction you can always defend, 12 tests:

TestExamCourseworkTotalExpectedPartitions covered
1602080A#1, #6, #11
2441559B#1, #6, #12
3301040C#1, #6, #13
410515D#1, #6, #14
5−105−5FM#2, #15
68025105FM#3, #16
730.510–FM#4
8'Q'10–FM#5
940−1–FM#7
104030–FM#8
114012.5–FM#9
1240'Q'–FM#10

Check: every one of the 19 partitions appears in the last column or in the E/A+/null check ✓. The slides go one step further, to 11, by letting tests cover even more overlapping partitions; the 12-test version keeps every invalid input alone, so no error message can mask another.

Exam radar: EP is for input and output. A complete EP answer includes: valid and invalid ranges, invalid types (real number, letter), valid outputs (A, B, C, D, FM) and unspecified outputs (E, A+, null). Then reduce the number of tests by covering overlapping partitions together.

Common mistake: stopping after the input ranges. Many answers list only “exam valid / below / above” and “coursework valid / below / above” (6 partitions) and forget the types, the outputs and the outputs that should never appear, which is 13 of the 19 partitions.


Worked Examples

Exercise 1

Why does the grade example include outputs like E and A+?

Show solution ↓
Solution

Because EP covers outputs too, not only inputs. E and A+ are never mentioned in the specification, so the program must never produce them: they are invalid output partitions. We check that no test ever produces them. If one did (say a total of 100 printed “A+”), we would have found a bug.

Exercise 2

Exam = 44 and coursework = 15. Work out the expected output and list which of the 19 partitions this single test covers.

Show solution ↓
Solution

Both marks are in range (0≤44≤750 \le 44 \le 75 and 0≤15≤250 \le 15 \le 25) and both are integers, so the input is valid.

total=44+15=59\text{total} = 44 + 15 = 59

59 is in “50 to under 70”, so the expected output is B.

It covers #1 (valid exam mark), #6 (valid coursework mark) and #12 (output B): three partitions with one test.

Exercise 3

A student combines two invalid inputs into one test: exam = 80, coursework = 'Q', expected FM. The program shows “fault message”. Has the student shown that the coursework type check works? Explain.

Show solution ↓
Solution

No. Exam = 80 is already outside its range, so a fault message is expected from the exam mark alone. The program may have rejected the exam mark first and never checked the coursework at all. The first error masks the second, just like masking in MC/DC: we cannot tell which check produced the result.

To show the coursework type check works, test it alone with a valid exam mark, for example exam = 40, coursework = 'Q' → expected FM. That is why the safe rule is: combine valid partitions freely, but test invalid inputs one at a time.

Exercise 4

Apply BVA to the grade calculator. Give the boundary values for both input marks, and pairs of valid inputs that test each grade edge on the total-mark line.

Show solution ↓
Solution

Inputs (on, just below, just above each edge):

  • Exam mark (0 to 75): −1, 0, 1 and 74, 75, 76.
  • Coursework mark (0 to 25): −1, 0, 1 and 24, 25, 26.

Output edges (keep both inputs valid, so only the total changes):

EdgeTest (exam, coursework)TotalExpected
D / C(20, 9)29D
(20, 10)30C
C / B(40, 9)49C
(40, 10)50B
B / A(50, 19)69B
(50, 20)70A
lowest valid total(0, 0)0D
highest valid total(75, 25)100A

Each pair sits on either side of a grade edge, so a programmer who wrote total > 70 instead of total >= 70 is caught by (50, 20) → expected A.

Exercise 5

Classify each test as positive or negative and give the expected output: (a) exam 75, coursework 25; (b) exam 0, coursework 0; (c) exam 76, coursework 0; (d) exam 30, coursework 25.5.

Show solution ↓
Solution
TestInputs valid?TotalKindExpected
(a) 75, 25both valid (on the upper edges)100positiveA
(b) 0, 0both valid (on the lower edges)0positiveD
(c) 76, 0exam above 75–negativeFM
(d) 30, 25.5coursework not an integer–negativeFM

Note (a) and (b): the edges 75, 25 and 0 are valid, because the ranges include their end points (0≤0 \le mark ≤75\le 75).

Exercise 6

Exam-style: “Use equivalence partitioning to design test cases for the grade calculator.” Outline what a complete answer must contain.

Show solution ↓
Solution

A complete answer has four parts:

  1. Input partitions from the ranges: exam 0 to 75 valid, below 0 and above 75 invalid; coursework 0 to 25 valid, below 0 and above 25 invalid.
  2. Input partitions from the type: for each mark, a real number and a letter are invalid.
  3. Output partitions: A, B, C, D, FM (total below 0), FM (total above 100) are valid outputs; E, A+ and null are unspecified, invalid outputs. That makes 19 partitions in all.
  4. Test cases with expected outputs, reduced by covering overlapping partitions: valid partitions combined freely (for example (44, 15) → B covers #1, #6, #12), each invalid input tested on its own, and a statement that no test may produce E, A+ or null.

The 12-test set in the lesson is one such answer; adding the BVA values of Exercise 4 makes it stronger.