Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Need help coding Dice rolling simulation

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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.
    Last edited by helloworld922; November 19th, 2011 at 12:03 AM.


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Need help coding Dice rolling simulation

    Take a look at the Math.random method

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    tanzania
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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.*;

    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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help coding Dice rolling simulation

    I'm still getting an error on the DicePair.
    Please post the full text of the error message.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

Similar Threads

  1. Help with creating a dice rolling program in Java
    By lilmiss in forum Object Oriented Programming
    Replies: 4
    Last Post: October 26th, 2011, 09:27 PM
  2. Command For Rolling Random Number
    By Brickwood in forum Java Theory & Questions
    Replies: 7
    Last Post: March 26th, 2011, 03:18 AM
  3. [SOLVED] Dice Rolling Simulation
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2011, 06:51 PM
  4. Trying to achieve Movie Style Rolling Credits
    By javanoob123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 10th, 2011, 08:02 PM
  5. Die Rolling - Continuing the statement?
    By Override in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 29th, 2010, 01:21 AM