|
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
Due Apr.03 (Fri).
We enter the world of competitive volleyball; for this problem make new class VolleyballGame, which keeps track of the status of one particular game.
Write a method playOnePoint, which adds a point1 to either the first team or the second, at random.
To make a random choice, construct a java.util.Random object, and call its method nextBoolean(). (You might think of that method as “flip a coin, and tell me whether or note the result was heads”.)
Write a method playManyPoints(int n), which calls playOnePoint exactly n times.
Note that in real-life, this isn't a very helpful method; we really want to keep playing until the game is over. (And, we want to stop adding points once that happens.) That's what the next few problems will help us with!
In volleyball, a game is over when
one team reaches 21 (or more) points,
and the difference between the scores
is two or more (that is, there are never ties, and you can't win by just one point).
Hint: some people use Math.abs to determine the difference between
two numbers (without caring which one is bigger).
Write a method hasFirstTeamWon which returns a true/false answer: whether or not the first team has won — that is, whether the game is over and the first team has a higher score. (So if hasFirstTeamWon returns false, it means that either the first team lost or the game is still be continuing.)
The method makeAnnouncement returns a message which could be twittered to fans everywhere: it is either "teamName1 won!", or "teamName2 won!", or "game still in progress." (where the parts in green are replaced with the team's actual name).
Be sure to call your previous methods, and not repeat any code!Here is an example of somebody using the class:
vg = new VolleyballGame( "beachfront boppers", "valley volleyers" ); // At this point the score is 0-0. System.out.println( "Expect game still in progress: "+ vg.makeAnnouncement() ); vg.playOnePoint(); // At this point the score is either 0-1 or 1-0 vg.playOnePoint(); // At this point the score is either 0-2, 1-1, or 2-0. vg.playManyPoints(18); // At this point, twenty points have been played: System.out.println( "Expect game still in progress: "+ vg.makeAnnouncement() ); vg.playOnePoint(); System.out.println( "Game probably still in progress (unless someone got skunked!): " + vg.makeAnnouncement() ); vg.playUntilOver(); System.out.println( "Now, somebody has definitely won: "+ vg.makeAnnouncement() ); |
Expect game still in progress: game still in progress. Expect game still in progress: game still in progress. Game probably still in progress (unless someone got skunked!): game still in progress. Now, somebody has definitely won: valley volleyers won! |
These aren't really test cases. In fact, it becomes difficult to test the nuances of testing whether a game is over (esp. when overtime is entered). I highly recommend writing some extra methods to test this. (For example, I made another constructor which took in two mid-game scores as well as the team names; then I could test my methods.)
1 playOnePoint should always play a point, even if you'd think the game was over (say, 21–3). (It will be the responsibility of other code to not call the method if the game is already over — that will be # below.) ↩
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
©2009, Ian Barland, Radford University Last modified 2009.Apr.15 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |