THINK FIRST·CODE LATER

← All labs

Measure branch coverage by hand

Problem

Instrument a method to measure branch coverage. The method under test is StudyBuddy's reminder rule:

static String reminder(int hoursLeft, boolean submitted, boolean optedOut) {
    if (submitted) {                 // decision D1
        return "NONE";
    }
    if (optedOut) {                  // decision D2
        return "NONE";
    }
    if (hoursLeft <= 0) {            // decision D3
        return "OVERDUE";
    }
    if (hoursLeft <= 24) {           // decision D4
        return "URGENT";
    }
    return "NORMAL";
}

Each decision has two outcomes, so there are 8 branch outcomes: D1T D1F D2T D2F D3T D3F D4T D4F (a decision that is never reached has neither outcome covered).

Input: lines hoursLeft submitted optedOut (true/false) until the end of input. For each, print h s o -> RESULT. Then print:

Covered: D1T D1F ...          (in the fixed order above, only covered ones)
Missing: ...                  (in the same order), or "Missing: none"
Branch coverage: c/8 (P%)     P = c * 100 / 8, integer division

Input:

10 false false
5 true false

Output:

10 false false -> URGENT
5 true false -> NONE
Covered: D1T D1F D2F D3F D4T
Missing: D2T D3T D4F
Branch coverage: 5/8 (62%)

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

s8-3-data-1.txt

10 false false
5 true false
What your program must print for s8-3-data-1.txt
10 false false -> URGENT
5 true false -> NONE
Covered: D1T D1F D2F D3F D4T
Missing: D2T D3T D4F
Branch coverage: 5/8 (62%)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < s8-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content s8-3-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.