RU beehive logo ITEC dept promo banner
ITEC 120
2009spring
ibarland
nokie
jmdymacek

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs

hw06
volleyball
loops; helper methods

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.

  1. Write a constructor which takes in two team-names. Each VolleyballGame object will have to keep track of the name and score of each team (The score is initially zero to zero, of course).
  2. 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”.)

  3. 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!

  4. Write a method isGameOver, which determines whether the game is still in progress.

    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).

  5. Write a method playUntilOver, which calls playOnePoint until the game is over.
  6. 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.)


    In your implementation, be sure to call isGameOver as part of your answer — don't repeat any code/conditions which are already written in that method.
  7. 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() );
The above code might produce the output:
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!
  
(Of course, depending on the random choices, the other team might have one, and about one game in a million will be over after a mere 21 turns.)

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.)      

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


©2009, Ian Barland, Radford University
Last modified 2009.Apr.15 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme