Java Bootcamp – Workbooks and Challenges
)}

Workbook 3.3

From Java-Bootcamp-Resources -> Module 1 -> 3. Booleans and Conditionals, open Workbook 3.3.

The Quidditch Championship


This is a modified passage from the book: Harry Potter and the Half-Blood Prince.

"If Gryffindor beats Ravenclaw by a margin of three hundred points, they would win the championship. If they win by any number of points or tie, they would come second. If they lose by less than a hundred points, they come third behind Hufflepuff. If they lose by more than a hundred points, they would be in fourth place"

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:

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.


>>In second place, Gryffindor!

Run some test cases


Test case one:

int gryffindor = 850; int ravenclaw = 500;

>>Gryffindor takes the house cup!

Test case two:

int gryffindor = 620; int ravenclaw = 500;

>>In second place, Gryffindor!

Test case three:

int gryffindor = 450; int ravenclaw = 500;

>>In third place, Gryffindor!

Test case four:

int gryffindor = 100; int ravenclaw = 500;

>>In fourth place, Gryffindor!

Good Luck!


Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.