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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 29

Thread: Help altering dice program?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help altering dice program?

    Here is the driver:

    import javax.swing.JOptionPane;
    public class DiceExperiment{
      public static void main(String[] args){
        String casino=
          JOptionPane.showInputDialog("enter a toss count");
        int tosses=Integer.parseInt(casino);
        Dice d=new Dice();
        d.multiToss(tosses);
        d.displayScoreboard();
      }
    }


    Here is the class

    public class Dice{
      private int[] scoreboard=new int[13];
      public Dice(){
        initializeScoreboard();}
     
      public void initializeScoreboard(){
        for(int j=0;j<13;j++)scoreboard[j]=0;
      }
     
      public int tossDie(){
        return (1+ (int)(6*Math.random()));
      }
     
      public int throwDice(){
        return(tossDie()+tossDie());
      }
     
      public void multiToss(int tossCount){
        int score;
        for(int j=0;j<tossCount;j++){
          score=throwDice();
          scoreboard[score]++;
        }
      }
     
      public int[] getScoreboard(){return scoreboard;}
     
      public void displayScoreboard(){
        for(int j=2;j<13;j++)
          System.out.println("toss of "+ j + " " + scoreboard[j]);
        System.out.println("toss count:");
      }
     
    }


    So this code rolls a pair of dice a number of times (# of times read from imput pane) and then reports how many times each total resulted from the rolls. I want to alter it so I enter a number into the pane and the dice continue to be rolled until each possible value of the dice throws (2,3,4,5....12) is reached at least the entered number of times. Poor description I know but heres a sample of what I want for an output...

    on the input 12
    2 30
    3 12
    4 58
    5 66
    6 93
    7 110
    8 96
    9 70
    10 47
    11 32
    12 19
    toss count: 633

    Any help is appreciated!


  2. #2
    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: Help altering dice program?

    heres a sample of what I want for an output.
    Can you post the current output for comparison so we can see what is wrong?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    Here is the output now:

    on the input 10...


    2 0
    3 2
    4 0
    5 1
    6 1
    7 2
    8 2
    9 1
    10 1
    11 0
    12 0
    toss count: 10

  4. #4
    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: Help altering dice program?

    Can you explain what is wrong with that output? What do you want to see in the second column.
    I assume the first column is just a row number.
         System.out.println("toss of "+ j + " " + scoreboard[j]);
        System.out.println("toss count:");
    What program did you execute to get what was post in post#3?
    The posted code has println()s that would have printed differently.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    Sorry the Println's are just supposed to be :

    System.out.println( j + " " + scoreboard[j]);


    In the second column I want to report the number of times each value was rolled, up until each value reaches a certain integer ( which is the value read in from display window). so if the integer 12 is entered into the window it should print :

    2 30
    3 12
    4 58
    5 66
    6 93
    7 110
    8 96
    9 70
    10 47
    11 32
    12 19

  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: Help altering dice program?

    Does that mean that the program should continue executing the loop until all the roll counts are >= the value entered by the user?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    thats correct

  8. #8
    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: Help altering dice program?

    Then there needs to be code inside the loop that tests if all the counts are at the desired level.
    A good solution would be to call a method that returned a boolean: true if the counts are at the desired level,
    false if the counts are not.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    where would you recommend i add that?

  10. #10
    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: Help altering dice program?

    The loop needs to continue until the condition is met.
    Use a while loop that continues executing until the method that checks if the condition is met returns true.
    while(not done) {
    compute next dice count
    count it
    end loop

    The "not done" is where the method is called to test if the loop should end.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    So to do this Im gonna use this method minValue which takes the parameter of the int value from the display window. Whats wrong with this?


    public void minValue(int value){
    int score=0;
    while(score<value)
    score=throwDice();
    scoreboard[score]++;

    }

  12. #12
    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: Help altering dice program?

    Whats wrong with this?
    That only tests 1 of the possible counts.
    What are the valid values of the score variable? Does it ever get > 12?
    Where are the counts that you want to compare against?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    so i made some changes but it still produces the same output as before:

    Heres the Driver
    import javax.swing.JOptionPane;
    public class DiceExperiment{
      public static void main(String[] args){
        String casino=
          JOptionPane.showInputDialog("enter a min value");
        int value=Integer.parseInt(casino);
        Dice d=new Dice();
        d.minValue(value);
        d.displayScoreboard();
      }
    }


    --- Update ---

    and heres the class
    public class Dice{
      private int[] scoreboard=new int[13];
      public Dice(){
        initializeScoreboard();}
     
      public void initializeScoreboard(){
        for(int j=0;j<13;j++)scoreboard[j]=0;
      }
     
      public int tossDie(){
        return (1+ (int)(6*Math.random()));
      }
     
      public int throwDice(){
        return(tossDie()+tossDie());
      }
     
      public void multiToss(){
        int score=0;
          score=throwDice();
          scoreboard[score]++;
        }
     
      public void minValue(int value){
        for(int j=0; j<scoreboard.length;j++){
          if(scoreboard[j] != value)multiToss();
          else 
            break;
        }
        }
     
      public int[] getScoreboard(){return scoreboard;}
     
      public void displayScoreboard(){
        for(int j=2;j<13;j++)
          System.out.println(+ j + " " + scoreboard[j]);
      }
     
    }

  14. #14
    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: Help altering dice program?

    What happened to the loop in multiToss() that was supposed to continue executing until all the counts were at desired value? See post#10
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    i thought i wouldn't need it since I added the minValue() method which contains a for loop testing to see if all of the scoreboard values are less than the entered int

    that if statement in minValue should be

    if(scoreboard[j] < value)multiToss();

  16. #16
    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: Help altering dice program?

    Putting two statements on the same line sort of hides the second statement and makes the code hard to read:
    if(scoreboard[j] != value)multiToss();

    Would that statement stop any additions to the count for that rolls value?
    Would the results shown in the second column of the report then be all the same?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    oh yeah i see what you mean. Whats the best way of fixing this problem?

  18. #18
    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: Help altering dice program?

    First get a design to describe what the code is to do. Then work on coding it.
    I suggested an approach in posts #8 & #10
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    ok thank

  20. #20
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    I altered the methods a bit more. I've got a compliation error at the moment....but would something like this be effective?

     
     public void multiToss(){
        int score;
        while(minValue()==true)
          score=throwDice();
          scoreboard[score]++;
        }
     
      public boolean minValue(int value){
        for(int j=2;j<13;j++){
          if(scoreboard[j] < value)
            return true;
          else 
            return false;
        }

  21. #21
    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: Help altering dice program?

    Yes, that looks closer.
    minValue returns false too soon. It should wait until all of the elements have been tested.

    You should Add a comment to the code saying what the method is supposed to do.
    The name minValue() is misleading.
    I'd think that the method would return the minimum value from the list.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    Ok. I'm having a compilation error in one of the methods

    public void multiToss(){
        int score;
        while(minValue()==true)
          score=throwDice();
          scoreboard[score]++;
        }


    I think it has something to do with the minValue() method taking an int as a parameter


    1 error found:
    File: C:\Users\Rich\JAVA FILES\Dice.java [line: 20]
    Error: The method minValue(int) in the type Dice is not applicable for the arguments ()
    Last edited by richardman54; October 31st, 2013 at 11:55 AM. Reason: I think it has something to do with the minValue method taking an int as a parameter

  23. #23
    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: Help altering dice program?

    If minValue() requires an int parameter, the code needs to give it a parameter.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    I thought I did this in the driver:
    import javax.swing.JOptionPane;
    public class DiceExperiment{
      public static void main(String[] args){
        String casino=
          JOptionPane.showInputDialog("enter a min value");
        int value=Integer.parseInt(casino);
        Dice d=new Dice();
        d.minValue(value);
        d.displayScoreboard();
      }
    }

     public boolean minValue(int value){
        for(int j=2;j<13;j++){
          if(scoreboard[j] < value)
            return true;
        }


    Shouldn't the value read in from the display window be passed into the minValue method?

  25. #25
    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: Help altering dice program?

    File: C:\Users\Rich\JAVA FILES\Dice.java [line: 20]
    Error: The method minValue(int) in the type Dice is not applicable for the arguments ()
    The compiler found a call to minValue() that did not have an arg.
    You must ALWAYS pass an arg to minValue(). minvalue will use the value passed to it in the current call to the method, not one passed in a previous call.

    The code still needs a comment saying what minValue() does.
    When does it return true?
    When does it return false?

    Those comments from the design of the program will help you write the code that you intended when you did the design.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Roll Dice Program Button
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2013, 03:51 PM
  2. Dice score program
    By ksahakian21 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 23rd, 2012, 05:25 PM
  3. 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
  4. Java Dice Program
    By ebone in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 22nd, 2011, 11:07 PM
  5. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM