Reputation: 11
Okay so this is for homework. I don't want a complete answer, just someone to help nudge me in the right direction. I am fairly new to Java, so please take it easy :P Okay so my professor is having us do a Dice simulation program with OOP. This is just one portion of the entire problem, but it's the part I'm stuck on, so if you need more I can provide more. But the instructions say:Make a Roll function that will simulate rolling each die, and return the total This function should only allow values between the appropriate range (i.e. if each die only has 4 faces, only numbers 1 - 4 should be allowed). What I have so far for this particular function is this:
public double Roll()
{
for(int i = 0; i < numDice; i++)
{
double total = Math.random()*numFaces + 1;
}
return total;
I don't know where to go from here, and I don't know what really to do. This is my first programming class and it's way over my head :P So like I said, if I could just get pointed in the right direction (using dummy talk, cause I'm still having a hard time grasping this whole thing) that would be awesome. And I can provide more of the actual problem if need be.
Upvotes: 0
Views: 599
Reputation: 12206
You could create a class named Die
to represent a die. This fits with OOP as 'die' would be one of your nouns. Now think of what configuration and behavior a Die
might have. You may wish to have a Die
have a configurable number of faces, but default to 6 if it isn't specified. You also want to be able to roll a die. Lets see what we have so far. Implementation left up to the reader.
public class Die {
public Die(){
this(6);
}
public Die(int faces){
//TODO: Write a constructor that takes number faces as an argument.
}
public int roll(){
//TODO: Implement a roll for this die, considering the number of faces.
}
}
Ok, so you have the beginning of a Die class. Now you might think, wouldn't it be cool if I could group die together as Dice
(a set of dice if you will).
public class Dice {
public Dice(Die... dice){
//TODO: Implement constructor that takes an array of die objects.
}
public int roll(){
//TODO: Roll all the dice, sum and return the result.
}
}
Next you might think, man wouldn't it be sweet if I could treat Dice
and a single Die
as a single type, I mean they both can be rolled. Let us create an interface called Rollable
that specified the roll behavior and have both Dice
and Die
implement it.
public interface Rollable {
int roll();
}
Then go back and change the class declarations to be.
public class Die implements Rollable
and
public class Dice implements Rollable
Now code that needs to roll things only needs to worry about Rollable
instead of Die
or Dice
Upvotes: 1
Reputation: 1294
Just noticed you used double , replace double with int or short. Look at the following tutorial.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Your also not so hot with naming conventions. Read this.
http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html
You don't need to roll for each face, only for each dice. Do this.
Please note, for this you could use shorts as well.
public int roll() {
return new Random().nextInt(6) + 1; // Returns number between 1 and 6
}
If you want to roll multiple dice you could do this.
public int roll(int amount) {
int total = 0;
for(int i = 0; i < amount; i++) {
total += new Random().nextInt(6) + 1;
}
return total;
}
Upvotes: 2
Reputation: 3302
You're defining total
inside the for
loop, which makes it invisible outside it. Also, you're overwriting it (=) instead of adding to it (+=) on each die throw.
public double Roll()
{
double total = 0;
for(int i = 0; i < numDice; i++)
{
total += Math.random()*numFaces + 1;
}
return total;
}
I would also recommend to follow Java style and rename the method roll
(methods start with a lowercase letter). The return type also strikes me as odd, I would probably use int
in a similar situation.
Upvotes: 1
Reputation: 13834
You need to define total outside the for loop and then do total+=… inside the loop
Upvotes: 0
Reputation: 13351
First of all, you need to declare the total outside the for-loop. Then initialize it to zero.
Then, inside the for-loop. Increase the variable.
If numFaces
should be able to be different for each die, you will need to get the value for it inside the for-loop, on each iteration.
Also, you should ask yourself: Which possible values do I want for the total
variable? Should it be int
or double
?
Upvotes: 0