home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.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.)
-
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!
-
Download this jar file for Die
and PairODice.
(how to open .jar files in BlueJ)
-
Create a Die object, and call several of its methods.
(Look at its documentation, to see what to do.)
-
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?)
-
Create the fields for PairODice,
-
Draw a picture of what
some particular PairODice object
might look like;
-
write the constructor,
and
-
make one instance
and inspect it.
-
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.)
-
Write a a method getTotal which returns
what the pair of dice is currently showing (a number in 2-12).
-
Write a method nameOfRoll which looks at the
two dice and returns a String:
-
"Lucky 7" if they total 7;
-
"Snake Eyes" if they are double 1s.
-
"Boxcars" if they are double 6s
-
"Double 2s"
or
"Double 3s" , etc.,
if they are doubles (which aren't already classified as
snake-eyes or boxcars).
Hint: All the (remaining) doubles can be handled as a single case.
-
"Six, plus change" if either number is a six (and
none of the above categories apply)
-
Optional: "acey-deucy"
if one die was a 1 and the other a 2.
(That's the lowest possible roll which isn't doubles.)
-
Otherwise, return a string like
"8 = 5+3".
Optional: When writing this sum, have the
bigger die is listed first (5, in this case).
Hint: you can either use an if statement
to tests which die is bigger,
or you can use Math.max and Math.min.
Either way, don't have repeated code.
Hints
-
Remember the
if-else-if code
from lecture.
-
write just a couple of these cases and test,
before writing all of them.
-
To help yourself test:
At the start of the method,
put in a temporary System.out.println which tells
you what the current dice are showing,
so that you can tell whether your method is catching the cases correctly.
Be sure to remove these debugging printlns once it's working!
-
To avoid having to write “.getFace()” all the time,
you can call getFace() just once for each die,
remembering each result in a local variable with a short name
(like “f1”).
-
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.)
-
Change PairODice's toString include
a picture of each die, subtitled with a text description.
Here are two samples:
-
.
. . .
. . .
Lucky 7
-
. .
.
. .
.
.
.
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.
-
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.
-
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.)
-
Write a method boolean isGood(),
which returns true if (and only if) the roll totals seven, or is doubles.
-
Write a method boolean isBad() ,
which returns true if (and only if) the roll totals four or less,
and is not otherwise considered a good roll.
Hint: you can (but don't need to) use
! (“not”), which negates
any boolean expression. For example, !(x > 3.0)
is the same1 as (x <= 3.0).
-
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?!)
↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs