die simulator
I am doing a die simulation with the requirements;
Instance Variables:
sides ?holds the integer number of sides on the die
faceValue ?holds the integer from 1 to sides that is currently showing face-up on the die
Class Constructor-
Initializes the value of faceValue to 1 and the value of sides to 6
Methods:
int getSides()
This method simply returns the integer number of sides on the die.
int getFaceValue( )
This method simply returns the integer faceValue showing on the die.
void setSides(__?__)
This method takes as a parameter, the integer number of sides the die should have, and sets the instance variable sides to this value. In addition to providing the body of this method, you will have to complete the method signature which indicates the data type and name of the method parameter used inside the body of the method.
void rollDie( )
This method rolls the Die, thus setting the faceValue of the Die.
If anyone could give me any tips on how to get this started, i would appriciate it
[1116 byte] By [
cdubeaua] at [2007-11-27 8:30:24]

class DieSimulation {
// instance variables
// constructor(s)
// method(s)
}
Just fill in the blanks.
It is quite straightforward. The requirements tell you exactly how to write the only class needed. The only method that needs more than one line of code is rollDie(), in which you'll need to pick a random value -- for this you can use java.util.Random.
Add also a main() method to the class to test your code.
hmm, was thinking along the lines of
public class DieSimulator {
public static void main(String[] args) {
System.out.println("Aaaaaaaarrrrrggggghhh");
}
}
"Maybe he was dictating".
Lord Bedevere.
import java.util.Random;
class die{
private int side,faceValue;
die() {
side = 6;
faceValue=1;
}
int getFaceValue(){
return faceValue;
}
void setSides(int side){
this.side=side;
}
void roll(){
faceValue=(int)(Math.random()*(side+1));
}
}
class DieTry{
public static void main(String args[]){
die t=new die();
t.setSides(5);
for(int i=0;i<5;i++){
t.roll();
System.out.println("Face Value..."+t.getFaceValue());
}
}
}
Hope this would help you...
> Hope this would help you...Doing the OP's homework won't help him learn Java at all and may let him in the bad habit of cheating. Furthermore your code has errors.Message was edited by: java_knight
> hmm, was thinking along the lines of
> > public class DieSimulator {
>public static void main(String[] args) {
>System.out.println("Aaaaaaaarrrrrggggghhh");
> }
> }
>
>
> "Maybe he was dictating".
> Lord Bedevere.
A normal die has 6 sides. Your program only works if the number is three:
"then shalt thou count to three, no more, no less. Three shall be the number thou shalt count, and the number of the counting shall be three. Four shalt thou not count, neither count thou two, excepting that thou then proceed to three. Five is right out. "
There are chances that the roll method in above class Die will return 0 for face value ..
coz the radom number returned is between 0.0 and 1.0 so it can even return 0.01 which will result in a facevalue of 0
so its better to use ,
void roll(){
faceValue=(int)(Math.random()*side) + 1;
}
> There are chances that the roll method in above class
> Die will return 0 for face value ..
> coz the radom number returned is between 0.0 and 1.0
> so it can even return 0.01 which will result in a
> facevalue of 0
>
> so its better to use ,
>
> void roll(){
> faceValue=(int)(Math.random()*side) + 1;
java.util.Random would be better.
jverda at 2007-7-12 20:20:59 >

> > There are chances that the roll method in above
> class
> > Die will return 0 for face value ..
> > coz the radom number returned is between 0.0 and
> 1.0
> > so it can even return 0.01 which will result in a
> > facevalue of 0
> >
> > so its better to use ,
> >
> > void roll(){
> > faceValue=(int)(Math.random()*side) + 1;
>
>
> java.util.Random would be better.
Just wondering, whats so good about the util.Random ?
> > java.util.Random would be better.> > Just wondering, whats so good about the util.Random ?It's chock full of methody goodness!
> > > java.util.Random would be better.> > > > Just wondering, whats so good about the util.Random> ?> > It's chock full of methody goodness!loving it
> Just wondering, whats so good about the util.Random ?
Math.random delegates to util.Random.
util.Random has methods for some common operations (e.g., a random int between 0 and N).
util.Random gives better distributions than a naive multiplication of Math.random's results. (Don't ask me why--I don't know ******** about the theory and don't remember enough about the article to give you a link.)
jverda at 2007-7-12 20:20:59 >
