RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab08b
printing
Pair O' Dice

For this assignment, work individually or with a partner. You should check this program off before you leave lab today; if you need to, you can check it off during the first five minutes of the next lab. (In that case, you may may want to be sure both partners have a copy of what's been written, as you leave lab.)

  1. We have seen System.err.println in passing, which prints out a message to the error log. Its cousin, System.out.println similarly prints a message, but it does it by changing pixels on the console screen.
    Example:
    System.out.println( "Hello, cool world." );
    
    int n = 3;
    System.out.println( "The value of n is " + n );
    
    
    Die d = new Die();
    System.out.println( "d is " + d.toString() );
    
    Try this out in the code pad. Note that it println a void function -- don't confuse ‘changing pixels on the console screen’ with ‘returning an answer’; they are unrelated!
  2. Download this jar file for Die and PairODice. (how to open .jar files in BlueJ)
  3. Create a Die object, and call several of its methods. (Look at its documentation, to see what to do.)
  4. Today, you (with a partner) will implement a class PairODice, which contains two six-sided Dice.
    (Design Recipe step 1: How will we represent this class? What fields will it have? What is the type of those fields?)
  5. Write a method roll which rolls both dice in the pair.
    Don't repeat code! (Your object has two Die objects inside of it. What do each of objects fields already know how to do?)
    (You don't need to make tests for this method. In fact, why is that difficult, to assert what the expected result should be? This will be true of most of the methods we write today.)
  6. Write a a method getTotal which returns what the pair of dice is currently showing (a number in 2-12).
  7. Write a method nameOfRoll which looks at the two dice and returns a String: Hints
  8. Next, change the toString() method for Die (not for PairOfDice!) so that it returns a String representing the die graphically, using periods or lowercase os or whatever. (Feel free to make yours look even better than the samples below.)

    Note: To best see your formatted output, print to the console window: something like System.out.println( someDie.toString() );.
    (While the important part is returning a String, you can call this method any way you like, the proper way to print a newline character is a bit philosophical; if you look at a string from BlueJ's method-call window, you'll see the newline printed as ‘\n’, and if you call toString from the codepane, BlueJ adds a set of quotation marks around the whole thing, which disrupts your formatting.)

  9. Change PairODice's toString include a picture of each die, subtitled with a text description. Here are two samples:
    1. 
        .
        
        
      . . .
      
      . . . 
      
      Lucky 7
      
    2. .   .
        . 
      .   .
       
      .   
        .  
          .
      8 (from a 5 and a 3)
      
    Do not repeat code! This is a one-line method, since you have already written other methods to do most all of the real work, and this method is just putting those other results together.
  10. Finally, write a method void showTests(): this method rolls the dice and displays the result (by passing the result of toString to System.out.println), and then rolls-and-displays again.
  11. Optional — if you have time:
    We would like to know whether or not a roll is good, and whether or not a roll is bad. (This information might be used by other methods to decide, say, whether to print a smiley-face or perhaps play a certain sound file.)
  12. Optional: Look inside class Die to see how it generates its random number. Change the class so that a Die can have 8 sides or 20 sides or 7 sides -- make the constructor take an argument specifying how many sides the Die has!

    To think about, for the future:
    How might we be able to print out the face of a Die which has (many) more than six sides?


1Well, you would think not being greater than three is the same as being less than three, but actually there is a little glitch: try this with double x = Double.NaN, which stands for “not a number”. (For example, Math.log(-1) returns NaN.) NaN is not greater than three, nor is it less-than-or-equal to three. Heck, NaN isn't even equal to itself?!)      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Mar.06 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme