CS I Laboratory # 4
Let the Dice Roll!
The Assignment Overview
For this lab, you are to implement a Die class. Your Die class will have methods for constructing, rolling, getting the current roll of the die, and some ObjectDraw-like methods such as move, getX, contains, etc. We will supply you with a simple Yahtzee game (file Yahtzee.java) that uses your Die class. When the Yahtzee program runs, it will look like the following figure.
Getting Started
1. First you need to locate a free computer in either Wright 112 or 339. Create a new folder "lab4" on the " Student Z: " drive in the folder "cs061" which you created in lab 1.
2. Start Internet Explorer by Start | Programs | Internet | Internet Explorer and go to http://www.cs.uni.edu/~fienup/cs061f02/labs/lab4/.
3. Download each of the files "objectdraw.jar", "lab_4.gpj", "Die.java," and "Yahtzee.java" by right-clicking on the name of each file and selecting "Save Target As". Save these files to your newly created "Z:\cs061\lab4" folder. The Die.java file contains the template for a Die class.
4. Start jGrasp by double-clicking on the lab_4.gpj project file that you downloaded to your new lab4 folder. By using this project file you sets up the correct Compiler and run-time settings for the objectdraw package.
5. Click on the "Project" tab at the bottom of the upper-left-hand sub-window of jGrasp. Double click "Yahtzee.java" and "Die.java" file. Enlarge the "Die.java" window.
6. When compiling, compile from the "Yahtzee.java" window. It will compile both the "Yahtzee.java" and "Die.java" files.
Part A
As with the last lab, we want you to practice incrementally development of the Die class. To encourage this approach, we have commented out several sections of the "Yahtzee.java" file. Initially, only the "begin" method is not commented out. Since "begin" only uses the Die constructor, you can develop, test, and debug only the constructor to start. Once the constructor is working, move on to part B of the assignment.
The constructor is called from within "Yahtzee.java" as:
die1 = new Die(DIE_LEFT, TOP_DIES_TOP, canvas);
where the first two parameter are the x and y locations (both of type double) of the die, and the third is a reference to the DrawingCanvas object, called "canvas," associated with the WindowController. To keep the Die class simple, assume that a die is always displayed as a 30 x 30 FramedRect, so we do not pass any "size" parameters.
A template for the Die constructor is already in the Die.java file. Your Die constructor should do the following things:
Once you get part A working, uncomment the Yahtzee.java code associated with part B. This code requires you to implement the following Die methods in the Die.java file.
Method Name | Type of value returned | Parameters and (types) | Description |
getX | double | none | Returns the x coordinate of the die's left edge |
roll | none | none | Randomly changes the die value to a new value between 1 and 6 |
getRoll | int | none | Returns the current die value |
Part C
Once you get part B working, uncomment the Yahtzee.java code associated with part C. This code requires you to implement the following Die methods in the Die.java file.
Method Name | Type of value returned | Parameters (and their types) | Description |
contains | boolean | Location | Returns true if the Location parameter is within the Die object on the screen; otherwise return false. |
move | none | xOffset (double) yOffset (double) |
Repositions the Die object on the screen by the specified xOffset and yOffset |
After implementing all of the above Die methods, recompile the Yahtzee.java code. When you run the Yahtzee program, it should be fully functional, i.e., the dice should be dragable, etc.
Turning in your program
The submission procedure is basically the same as last week.
Good luck and have fun!
Tips for success on Lab 4