Need help coding Dice rolling simulation
Hello all. I've been staring at this program I need to do for the better part of two days now. I'm relatively new to coding, but have had almost no trouble at all until this. After reading up as much as I could on classes and objects, I still can't figure out exactly what the syntax is that I need to use.
Basically I need a die object called into a diepair class and then that called into a simulation class. It needs to tally up the totals for each die roll. I've been given the starting code and have even put in the directions I was given: I'm sure this is relatively simple and yet still can't seem to figure it out.
Code Java:
import java.util.*;
public class Die {
private Random rgen;
private int face;
public Die(Random gen) {
//needs to have a stored reference to the random # generator and a face value between 1 and 6
}
public void roll() {
face = rgen.nextInt(1-6);
//needs the random integer generated and stored in the face field
}
public int getFace() {
return face;
//face needs to be returned
}
}
import java.util.*;
public class DicePair {
public Die die1, die2;
int doubles=0, sum;
Random rgen;
public DicePair (Random gen) {
Die die1 = new Die(rgen);
Die die2 = new Die(rgen);
//construct two die objects and pass the specified random number generator to them
}
public void roll() {
//roll them
}
public int getSum() {
sum = die1(getFace) + die2(getFace);
//return the sum of the face values
}
public boolean isDoubles() {
if(die1(getFace) == die2(getFace))
doubles = doubles + 1;
//return true if double
}
}
import java.util.*;
public class Simulation {
private Random gen;
private DicePair pair;
private int[] tally;
private int doubles;
public Simulation() {
DicePair pair = new DicePair();
tally = new int[2-12];
//create the random number generator, dicepair object, and array to tally the sums
}
public void simulate(int times) {
doubles = 0;
for (count = 0; sim <= 200; ++ count) {
}
//initialize tallies and doubles to zeros, simulate rolls, and tally the number of rolls and count doubles
}
public void report() {
System.out.printf("%nSum Tally%n");
for (count = 2; count < 13; ++count) {
System.outprintf ("%3d %8d ", count, tally[count]);
for (star = 0; star < tally[count]; ++star) {
System.out.printf("*");
}
System.out.printf("%n");
}
//print a report of the simulation
}
public static void main(String[] args) {
Simulation sim = new Simulation();
sim.simulate(200);
sim.report();
}
}
If someone could help me out here, I don't know if it's the syntax that's confusing me or what, but it's been driving me crazy.
Re: Need help coding Dice rolling simulation
Take a look at the Math.random method
Re: Need help coding Dice rolling simulation
calling method must be like die1.getFace() and die2.getFace() instead of die1(getFace) and die2(getFace) also you have to declare count and star
Re: Need help coding Dice rolling simulation
There are many errors in your program.
Read the tutorials, start with a small program and move towards the bigger ones. Don't just jump, trying the examples consisting of multiple classes. You must know the concepts first.
Make your program compile able and post here then if you stuck in some kind of problem, exception etc.
Re: Need help coding Dice rolling simulation
Alright I'm back. I tried fixing up the first Die program and... well, it's compiling at the very least.
Code :
import java.util.*;
public class Die {
private Random rgen;
private int face;
public Die(Random gen) {
rgen = new Random();
face = rgen.nextInt(1-6);
}
public void roll() {
face = rgen.nextInt(1-6);
}
public int getFace() {
return face;
}
}
But now I'm still getting an error on the DicePair.
import java.util.*;
Code :
public class DicePair {
public Die die1, die2;
int doubles=0, sum;
Random rgen;
public DicePair (Random gen) {
Die die1 = new Die(rgen);
Die die2 = new Die(rgen);
}
public void roll() {
die1 = Die.roll();
die2 = Die.roll();
}
public int getSum() {
sum = die1.getFace() + die2.getFace();
}
public boolean isDoubles() {
if(die1 == die2){
doubles = doubles + 1;
}
}
}
It's screwing up on the Die.roll at the very least, but I can't seem to figure out why despite reading up on static methods. What's really frustrating is that I'm pretty sure I could get this program doing what I would like, if I could have done it from scratch instead of having to fill in the blanks like this.
Re: Need help coding Dice rolling simulation
Quote:
I'm still getting an error on the DicePair.
Please post the full text of the error message.
Re: Need help coding Dice rolling simulation
Well, it seems that you don't have concepts of OOP.
Your die1 and die2 are not static in your DicePair class. They are just objects of Die class that are never instantiated. What you do in constructor is, just creating and initializing two Die objects which are killed as soon as you leave the constructor, means no life at all for those two variables. And, roll() in your Die class is returning void, and what are you expecting to get from it is Die. And roll is not the static function as you are calling it like Die.roll().
Your program contains many errors. I would like to recommend you to study OOP first before going deep into this. It will not take more than 4 hours to get you started with OOP.
I hope that i am very clear.