CS 201 - Computer Science I - Fall 2008
Lab 11 - loops
Loyola College >
Department of Computer Science >
Dr. James Glenn >
CS 201 >
Labs >
Lab 11
Due
Wednesday, December 3rd at 11:59pm.
Labs submitted one day late will be assessed a
20% penalty. Labs will not be accepted more than one day late.
Objectives
Assignment
Create a class called TwoDice that models two six-sided dice
(you can think of one die as green and one as red if you wish). Your class
must have the following:
- two fields to keep track of what number is showing on each die;
- a constructor that takes no arguments and initializes the fields
to random numbers between 1 and 6;
- a constructor that takes two int arguments and sets the
two fields to indicate that those are the numbers showing on the dice;
- a method called getTotal that returns the total showing
on the two dice;
- a method called isPair that returns true if the
numbers showing on the dice are the same and false otherwise;
- a method called contains that takes an int argument
and returns true or false according to whether one
of the two dice is showing that number;
- a method called roll to assign random numbers to the two dice;
and
- a method called toString that returns a String that
contains the numbers showing on the two dice, so if the TwoDice
object has one 2 and one 6 then the String returned is
"2-6" (this method enables us to use TwoDice objects
in println statements).
Complete the main method in DiceTest to thoroughly
test your methods.
Remember to compile and test incrementally. You will need to write the
two constructors and getTotal in order to perform the initial
tests in DiceTest. Once those are working you can try writing
the other methods one by one (it may be a good idea to write toString
early).
Files
Exercises
For each method, determine if it is used to ask the object a question or
if it is used to tell the object to do something. Write each
question or command in plain English.
Extra Credit
Add code as appropriate to validate the values that are passed as arguments
to make sure that the dice are never showing a number outside the range
1,...,6. Add code to DiceTest to test your validation.
Submissions
Submit the your source code for the TwoDice class and your
completed DiceTest class as
attachments to e-mail sent to your instructor. Submit the answers to the
exercises on paper, as an attachment, or in the body of your e-mail.