# Dice Program

• 11-13-2011, 10:49 AM
rahulkumarnaik
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.
• 11-14-2011, 09:43 AM
Re: Dice Program
• 11-14-2011, 10:32 AM
rahulkumarnaik
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());
}
}
• 11-14-2011, 11:13 AM
Re: Dice Program
Create a method that takes two arguments, the number of "rolls" and a modifier.
• 11-14-2011, 11:30 AM
rahulkumarnaik
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
• 11-14-2011, 11:31 AM
Re: Dice Program
No, I can't. Try it, we are not a homework service.
• 11-14-2011, 11:34 AM
rahulkumarnaik
Re: Dice Program
Hi Frnd...

Thnx
• 11-14-2011, 12:57 PM
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.
• 11-14-2011, 01:07 PM
rahulkumarnaik
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();
}
}
• 11-14-2011, 01:39 PM
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
Quote:

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-14-2011, 02:52 PM
rahulkumarnaik
Re: Dice Program
Hi,

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

Thnx
• 11-14-2011, 03:31 PM
Re: Dice Program
:sigh:
double :sigh:

(since the site thought doing it just once was too short of a message)
• 11-14-2011, 04:38 PM
rahulkumarnaik
Re: Dice Program
Hi

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

Thnx
• 11-14-2011, 05:02 PM
JosAH
Re: Dice Program
Quote:

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
• 11-15-2011, 08:05 PM
rahulkumarnaik
Re: Dice Program
Hi Help me on part 2 and part 3