Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
This video will present the solution to workbook 3.3.
We're going to visualize the runtime again using code.
So please make sure that you launch the exact folder that contains your java file.
Do not, under any circumstance launch all of the resources at once.
If your file explorer looks like this, you will not be able to visualize their runtime.
All right.
First, we need a variable that calculates the margin of points Gryffindor stored over Ravenclaw.
So we can say int margin is equal to the amount of points by which Gryffindor stored over Ravenclaw.
And the margin of points that Gryffindor scored over the other team will determine their ranking.
So if Gryffindor wins by a margin of 300 points or greater.
Then we're going to say Gryffindor takes the House Cup.
Now, if this condition fails, that means Gryffindor did not earn enough points.
So the next condition we're going to check for is if they won by any margin of points or tied.
So if margin is greater than or equal to zero.
If that happens to be the case, then they are in second place.
So we're going to print.
In second place Gryffindor.
However, if the points margin happens to be less than zero, if Gryffindor actually loses to Ravenclaw,
then this condition will also be false.
So the next thing we have to check for is how much did they lose by if they lose within 100 points,
if they lose by a margin of 100 points or less, then they are in third.
So here we can say else if margin is greater than or equal to -100.
Then they are in third and third place Gryffindor.
Now, finally, if Gryffindor loses by more than 100 points, then this condition is also going to fail.
So the last possible scenario that we can represent using else is where Gryffindor is in last place.
Perfect.
Now we can visualize the runtime.
And if you want to visualize the runtime for a series of if elseif and else, what you want to do is
only put one breakpoint at the very beginning.
All right.
And you know what?
We'll put breakpoints here as well.
And our first test case will be Gryffindor winning by a margin of 200 points.
So let us visualize the runtime.
Dismissing this window.
All right.
Cleaning up a little.
So here it creates a points variable of 400 and then Ravenclaw score 200 points.
So Gryffindor won by a margin of 200 points.
A margin of 200 is not greater than or equal to 300.
So unfortunately they are not in first place.
Please don't press continue.
We want to step over everything step by step.
So we'll press the step over button.
This is how you visualize a series of if if you put one breakpoint and then you keep stepping over.
And now a margin of 200 is greater than or equal to zero.
So this will evaluate to true.
And Gryffindor is in second place and now you can press continue in test case number two, Gryffindor
will score 850 points, whereas Ravenclaw scores 500.
Let us re visualize the runtime.
This time Gryffindor won by a points margin of 350.
350 is greater than or equal to 300.
So this condition evaluates to true, which means this statement will run.
Gryffindor are the champions.
All right.
Test.
Case number three is where Gryffindor scores 620 points, but Ravenclaw only scores 500.
Let us visualize the runtime.
This time Gryffindor won by a margin of 120 points.
Now we're going to step over the chain of F, l, f, and L statements.
So the margin of 120 is not greater than or equal to 300.
This evaluates the false margin of 120 is greater than or equal to zero.
This evaluates the true and Gryffindor will come in second place.
All right.
Test case number four is when Gryffindor scores 450 points, but Ravenclaw actually has more points
than them.
So let's visualize the runtime.
So here it appears that Gryffindor lost by a margin of 50 points.
-50 is not greater than or equal to 300.
This evaluates the false the if statement does not execute -50 is not greater than or equal to zero.
So this also evaluates the false.
-50 is greater than or equal to -100.
So Gryffindor did manage to only lose within 100 points, which means they will be in third place.
Test case number five.
100.
500.
This time Gryffindor loses by 400 points.
So the margin of points is -400.
That's how much they lost by.
This is going to evaluate the false so the statement will not execute.
This also evaluates the false because -400 is not greater than or equal to zero.
This is also going to evaluate the faults because they did not lose within -100.
They lost by 400 points.
So we default to the statement where they are in fourth place.
And that's all.
I hope you enjoyed this breakpoint session.