Good programs keep calculating, deciding and printing apart. A method that only calculates can be checked without looking at any output. The printing code then has little left that can go wrong.
Warm up: the zero case
A percentage needs a division, and that division has a case you must think about before it crashes. Predict what each line does. Percent guards against zero, so what happens when there is nothing to complete?
Console.WriteLine(Percent(1, 4));
Console.WriteLine(Percent(0, 0));
Console.WriteLine(Percent(3, 4));
static int Percent(int done, int total)
{
if (total == 0)
{
return 0;
}
return done * 100 / total;
}Check your prediction
25
0
75The order of operations matters here. done * 100 / total multiplies first, so 1 * 100 / 4 is 25. Written as done / total * 100, the whole-number division 1 / 4 gives 0 before the multiplication happens. Try it.
Build it yourself
A progress report with checks
Write two static methods and check them.
Percent(int done, int total)returns a whole-number percentage. A total of 0 gives 0.Status(int percent)returnsNot startedfor 0,Finishedfor 100 or more, andIn progressotherwise.
Write at least six checks with the Check helper, covering the zero total, 0%, 1 of 4, 3 of 4, 100% and a percentage above 100. Then print the report line Progress: 3/4 (75%) In progress built from those methods, with no numbers typed directly into the text.
int done = 3;
int total = 4;
Console.WriteLine($"{done}/{total}");Hint 1: the idea
Each method answers one question, and Status receives the percentage rather than recalculating it.
Hint 2: a first step
Bring in Percent from the warm-up and add a check for it. Then write Status as a switch expression or an if chain.
Show one solution
int failures = 0;
Check(Percent(1, 0) == 0, "a zero total gives 0");
Check(Percent(0, 4) == 0, "nothing done gives 0");
Check(Percent(1, 4) == 25, "1 of 4 is 25");
Check(Percent(3, 4) == 75, "3 of 4 is 75");
Check(Status(0) == "Not started", "0 is not started");
Check(Status(1) == "In progress", "1 is in progress");
Check(Status(99) == "In progress", "99 is in progress");
Check(Status(100) == "Finished", "100 is finished");
Check(Status(120) == "Finished", "above 100 is finished");
int done = 3;
int total = 4;
int percent = Percent(done, total);
Console.WriteLine($"Progress: {done}/{total} ({percent}%) {Status(percent)}");
void Check(bool passed, string name)
{
if (!passed)
{
Console.WriteLine($"FAILED: {name}");
failures++;
}
}
static int Percent(int done, int total)
{
if (total == 0)
{
return 0;
}
return done * 100 / total;
}
static string Status(int percent)
{
return percent switch
{
0 => "Not started",
>= 100 => "Finished",
_ => "In progress",
};
}Progress: 3/4 (75%) In progressPercent checks for a zero total before dividing, because dividing an int by zero throws. Multiplying by 100 before dividing keeps whole-number division accurate: 1 * 100 / 4 is 25, while 1 / 4 * 100 would be 0. The checks cover each edge of Status, including a value over 100, before the real report runs.
Break Status deliberately by changing >= 100 to > 100, and confirm a check fails. Which of your checks caught it? If Percent were written done / total * 100, which ones would catch that?
Completion is saved in this browser only. It does not measure mastery.