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

## Dice Program

Hi All,

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. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11

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

## 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. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11

## Re: Dice Program

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

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

## 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. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11

## Re: Dice Program

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

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

## Re: Dice Program

Hi Frnd...

Thnx

8. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11

## 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. Member
Join Date
Nov 2011
Posts
8
Rep Power
0

## 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("****************************** ***************************************");

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. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11

## 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. Member
Join Date
Nov 2011
Posts
8
Rep Power
0

## Re: Dice Program

Hi,

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

Thnx

12. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11

## Re: Dice Program

:sigh:
double :sigh:

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

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

## Re: Dice Program

Hi

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

Thnx

14. ## Re: Dice Program

Originally Posted by rahulkumarnaik
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

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

## Re: Dice Program

Hi Help me on part 2 and part 3

#### Posting Permissions

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