Workbook 3.3
From the Java-Bootcamp-Resources
, launch the Workbook 3.3
folder.
The Quidditch Championship
This is a modified passage from the book: Harry Potter and the Half-Blood Prince.
We're going to convert this passage into code. The code starts with the number of points scored by gryffindor
and ravenclaw
.
public class Championship {
public static void main(String[] args) {
int gryffindor = 400; //gryffindor points
int ravenclaw = 200; //ravenclaw points
}
1. Calculate the difference in points.
Make an int
variable called margin
. It will calculate the margin of points gryffindor
scored over ravenclaw
.
public class Championship {
public static void main(String[] args) {
int gryffindor = 400; //gryffindor points
int ravenclaw = 200; //ravenclaw points
// margin: amount of points by which gryffindor scored over ravenclaw ;
}
}
2. if - else if - else.
If gryffindor
wins by a margin of 300 points or more, print
:
Gryffindor takes the house cup!
If gryffindor
wins by a margin of any points or ties, print
:
In second place, Gryffindor!
If gryffindor
loses within 100 points, print
:
In third place, Gryffindor!
else
, print
:
In fourth place, Gryffindor!
Run your Code
Test Case 1
int gryffindor = 400; int ravenclaw = 200;
>>In second place, Gryffindor!
Test Case 2:
int gryffindor = 850; int ravenclaw = 500;
>>Gryffindor takes the house cup!
Test Case 3:
int gryffindor = 620; int ravenclaw = 500;
>>In second place, Gryffindor!
Test Case 4:
int gryffindor = 450; int ravenclaw = 500;
>>In third place, Gryffindor!
Test Case 5:
int gryffindor = 100; int ravenclaw = 500;
>>In fourth place, Gryffindor!
Visualizing the Runtime
After you solve this workbook, I still recommend watching the video solution on Udemy.
It will show you how to visualize the runtime using Visual Studio Code.
Solution Code
The solution code is inside the same folder as the starter project.