Results 1 to 2 of 2
Thread: Help with my assignment java
- 07-24-2007, 02:18 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
Help with my assignment java
I have an assignment:
Design and implement a class called PairOfDice, composed of tow die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that return the current sum of the die.
Create a driver class called RollingDice2 to instantiate and use a PairofDice object.
This is what I have so far I just wanted to see if I'm at least on the right track for this program.
Java Code:public class PairofDie { private final int MAX = 6; private int faceValue; public Die1() { faceValue = 1; } public int roll() { faceValue = (int)(Math.random() * Max) + 1; return faceValue; } public void setFaceValue (int value) { faceValue = value; } Public int getFaceValue() { return faceValue; } public String toString() { string result = Integer.toString (faceValue); return result; } } public Die2() { faceValue = 1; } public int roll() { faceValue = (int)(Math.random() * Max) + 1; return faceValue; } public void setFaceValue (int value) { faceValue = value; } Public int getFaceValue() { return faceValue; } public String toString() { string result = Integer.toString (faceValue); return result; } }
- 08-07-2007, 06:59 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 35
- Rep Power
- 0
Ok, to start, you'll need these variables for your PairofDie class:
Java Code:private Die die1; private Die die2;
Java Code:class Die { private final int MAX = 6; private int facevalue; public Die() { /* Constructor */ } public void roll() { facevalue = (int)(Math.random() * MAX) + 1; } }
Then in your your PairofDie constructor, you'd create your two dice:
Java Code:public PairofDie() { die1 = new Die(); die2 = new Die(); }
Java Code:public void rolldice() { die1.roll(); die2.roll(); }
After all that, you'll create that RollingDice class that'll have the main method which will instantiate and utilize the PairofDie class.
3 files (and classes) in all: Die.java, PairofDie.java, and RollingDice.java.
Greetings.
Similar Threads
-
Java assignment - couple methods don't know how to figure out
By Snowboardmylife in forum New To JavaReplies: 1Last Post: 04-16-2008, 11:52 AM -
Java assignment
By xtianah77 in forum New To JavaReplies: 1Last Post: 02-18-2008, 12:54 AM -
Please help... assignment for school
By confused2000 in forum New To JavaReplies: 3Last Post: 11-12-2007, 09:12 AM -
for Assignment plz help
By assamhammad in forum New To JavaReplies: 1Last Post: 11-06-2007, 09:35 PM -
java assignment, need help bad.
By carlos123 in forum New To JavaReplies: 1Last Post: 11-06-2007, 05:53 PM
Bookmarks