Results 1 to 15 of 15

Thread: Dice Program

  1. #1
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Question Dice Program

    Hi All,

    Please advice on the below issue urgently.........

    Part 1:
    -------
    Write a dice class that takes a variable number of sides. The dice
    class should have an explicit roll method that returns the result of a
    roll:

    Dice d20 = new Dice(20);
    System.out.println(d20.roll()); // prints 14 (for example)

    Optionally, keep track of the last rolled number and provide a get
    method.


    Part 2:
    -------
    Often we want to roll multiple dice of the same type and add a fudge
    factor.

    *** This is not the same as multiplying one roll by a number. ***

    Modify the dice class to allow the following types of interactions:

    // The following simulates rolling 4 20-sided dice and adding 5 to
    // the result:
    System.out.println(d20.roll(4) + 5); // prints 48 (for example)

    // The following simulates rolling 5 20-sided dice and subracting 1
    // from the result:
    System.out.println(d20.roll(5) - 1); // prints 79 (for example)


    Part 3:
    -------
    Wrap it all up in an interface to re-roll the same types of dice:

    Roller x = new Roller(4, 20, 5);
    Roller y = new Roller(4, d20, 5);

    System.out.println(y.roll());
    System.out.println(x.roll());

    will both set up a roller that will return 4 * d20 + 5 roll each time.

    This should always return a number >= 0, so do not allow Roller() to be
    instantiated with values that can result in negative values.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Dice Program

    And your question is?

  3. #3
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: Dice Program

    How to solve part 2.....Part 1 i have solved as

    public class Dice
    {
    private int numFaces;
    Dice(int faces)
    {
    numFaces = faces;

    }

    int roll()
    {
    return 1 + (int)(Math.random() * numFaces) ;

    }
    }



    public class diceGame
    {
    public static void main(String[] args)
    {
    Dice dice2 = new Dice(20);
    System.out.println("dice2 is " + dice2.roll());
    }
    }

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Dice Program

    Create a method that takes two arguments, the number of "rolls" and a modifier.

  5. #5
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: Dice Program

    Hi Friend....

    I am not able to create can you please send the code how to do this...I kindly requesting you please send me

    Thnx

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Dice Program

    No, I can't. Try it, we are not a homework service.

  7. #7
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: Dice Program

    Hi Frnd...

    I kindly requesting please help me on this

    Thnx

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Dice Program

    Come now, you surely know how to write a for loop, right? So how hard is it to write a for loop with the end condition being the first suggested parameter and summing the results of calling your "roll" method and applying the modifier?

    And really, if you really needed to have it spelled out to you that far, then you need to search for a different of field of work. And, if you are still unable to even try doing it, then that field of work needs to contain the use of the sentence "would you like fries with that" or the use of a sledgehammer.

  9. #9
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: Dice Program

    Hi

    I find the solution as below and now i am looking part 3: solution can u advice on this ?

    public class Dice
    {

    private int numFaces;


    Dice(int faces)
    {

    numFaces = faces;

    }

    void roll()
    {

    System.out.println("Simulating rolling 4 20-sided dice and adding 5 to result");
    for(int i=0; i<4; i++)
    {
    int dice1= 1 + (int)(Math.random() * numFaces) ;
    System.out.println("Dice1="+dice1);
    int result1=dice1+5;
    System.out.println("5 add to Dice1 ="+result1);
    }

    System.out.println("****************************** ***************************************");

    System.out.println("Simulating rolling 5 20 sided dice and substraction 1 from the result");
    for(int j=0; j<5; j++)
    {
    int dice2= 1 + (int)(Math.random() * numFaces) ;
    System.out.println("Dice1="+dice2);
    int result2=dice2-1;
    System.out.println("1 Substraction to Dice1 ="+result2);
    }
    }

    and main class as

    public class diceGame
    {
    public static void main(String[] args)
    {

    Dice dice2 = new Dice(20);
    dice2.roll();
    }
    }

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Dice Program

    That will not get you a passing grade. I said to add a new method that calls "roll" from within a for loop using two arguments. As your assignment says
    Modify the dice class to allow the following types of interactions:
    it does not say "Modify the dice class to do only the following:".

  11. #11
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: Dice Program

    Hi,

    Can you send the exact code which i get correct value ?

    Thnx

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Dice Program

    :sigh:
    double :sigh:

    (since the site thought doing it just once was too short of a message)

  13. #13
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: Dice Program

    Hi

    I can not get exactly what you want to say...

    Thnx

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: Dice Program

    Quote Originally Posted by rahulkumarnaik View Post
    I can not get exactly what you want to say...
    Masijade can't give you the code; he's not allowed to do so because it's a secret, unless you can do the silly dance in front of a live audience.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Need help with a dice program. Thanks!!
    By daftwolfie in forum Eclipse
    Replies: 1
    Last Post: 05-15-2011, 10:28 AM
  2. Kindly Help me..DICE PROGRAM
    By aronkevin in forum Advanced Java
    Replies: 1
    Last Post: 10-08-2010, 07:32 AM
  3. Please help..dice java program
    By aronkevin in forum New To Java
    Replies: 3
    Last Post: 10-08-2010, 05:37 AM
  4. Dice Simulation Program
    By touandcim in forum New To Java
    Replies: 3
    Last Post: 03-15-2010, 12:09 PM
  5. Small Dice Program
    By kimmelim in forum New To Java
    Replies: 13
    Last Post: 02-15-2009, 01:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •