RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsjava.lang docsjava.util docs

hw07
volleyball
loops; helper methods

Due at the start of class Nov.03 (Wed).

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. (4pts) 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. (8pts) Write a toString method, and an equals method that compares all fields between two VolleyballGames. (You need only one test case for each of these functions.)
  3. (4pts) Write a method playOnePoint(boolean awardToHome), which adds a point1 to the first team if awardToHome is true, otherwise to the second team. Include at least two tests.

  4. (4pts) Make a second, overloaded version of playOnePoint which awards a point to either the first team or the second, at random.
    (You don't need test cases for this.) This function should call the previous version, playOnePoint(boolean), to avoid repeated code.

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

    Extra credit: Make the odds of winning a point proportional to the number of vowels2in the team's name. For example, if “Team VB!” (two vowels) plays “Over the Top” (four vowels), the probability3of the first team scoring is only 2/(2+4) = 1/3.
    Be sure to use particularly good use of (tested) helper functions if going for extra-credit points.

  5. (4pts) Write a method isGameOver, which determines whether the game is complete, or 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).

    Include sufficient test cases, including one for each type of unusual situation (“corner case”) you can think of.

  6. (4pts) Write a method playUntilOver, which calls playOnePoint() until the game is over. No test cases needed (since the expected result is random), but you should test it by hand a few times.
  7. (4pts) The method makeAnnouncement returns a message which could be tweeted 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 already-written code!

    Since your code calls methos that are previously-tested (and are thus known to be correct), 3 test cases will suffice here.

  8. (4pts) Finally: write a function which plays n games, and returns the fraction of times the first team wins.
    (We expect the answer to be around 0.5, but of course it won't be exactly that.)

    (4pts) As test cases, have your program print out the following table. Note that each time you run it, the table might give different results. Paste the result of three different runs into your program file (as a comment).

    nfraction of first-team winsdistance from 0.5
    10                                
    100                                
    1000                                
    10000                                
    100000                                
    (Don't worry about aligning/formatting your table, though if you want you can use and/or the escape code for a tab character, \\t.)
    Can you detect a pattern in the third column? (Can you quantify the pattern?)

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." );
System.out.println( "Actual: " + 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." );
System.out.println( "Actual: " + vg.makeAnnouncement() );
    
vg.playOnePoint();
System.out.println( "Expect most likely, unless somebody got skunked: " + "Game still in progress." );
System.out.println( "Actual: " + vg.makeAnnouncement() );

vg.playUntilOver();
System.out.println( "Expect that one side has definitely won: " );
System.out.println( "Actual: " + vg.makeAnnouncement() );
The above code might produce the output:
Expect: Game still in progress.
Actual: Game still in progress.
Expect: Game still in progress.
Actual: Game still in progress.
Expect: Game still in progress.
Expect most likely, unless somebody got skunked: Game still in progress.
Actual: Game still in progress.
Expect that one side has definitely won:
Actual: 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.)      

2 It is left to your discretion, whether ‘Y’ counts as a vowel.      

3 If the probability is 2/(2+4), we say the odds are 2:4; so yes technically the odds really are proportional to the number of vowels, even though the probability isn't proportional.      

homeinfolectslabsexamshws
tutor/PIsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Nov.23 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme