In: Computer Science
Create a program named DiceGame.Java (in Java lagnuage of course)
The problem/background info: Suppose someone comes to you and says “I have a 6-sided Die and different one that has 4 sides. Both are fair dice. I would like to propose a game. We will roll both dice and I will give you $3.00 if the total is less than 5 and $8.00 if the total is exactly equal to 5. But if the total is greater than 5, you give me $2.00.” Should you do it? Should you do it if the second die had 6 sides?
Your program will simulate this: You
are given a class called Die, which has a default constructor
(makes it 6-sided) and has a parameterized constructor that accepts
a number of sides (minimum 4 sides or else it will throw an
exception). It also has a method called roll() which
rolls itself and returns the (int) result; it also has a method
called getNumSides() which returns the (int) number of sides it
has. You will write a program called DiceGame.java that
simulates running the game over and over by doing the
following:
Notes:
NumberFormat money = NumberFormat.getCurrencyInstance();
( you will have to import java.text.NumberFormat; )
Then use your instance whenever you print your_calculation, like this
System.out.println(money.format(your_calculation));
When formatted, negative money will be in parentheses rather than having a minus sign.
// This class will simulate a Die (half a pair of dice)
public class Die
{
//------- data
private int numSides;
private java.util.Random rGen;
//------- constructors
public Die()
{
this(6); //call parameterized constructor, as if 6 were passed in
}
public Die(int theSides)
{
if (theSides < 4)
throw new IllegalArgumentException("A Die cannot have less than 4
sides");
numSides = theSides;
rGen = new java.util.Random(numSides);
}
//------- methods
//getNumSides - returns the number of sides
public int getNumSides()
{
return numSides;
}
//roll - returns a random number from 1-numSides. The seed for the
random number generator should be the numSides
// (so everyone's results are the same...)
public int roll()
{
return rGen.nextInt(numSides) + 1;
}
}
Example1:
How many sides should the second die have?
4
How many times should we roll the dice?
87654
How often should we print results?
10000
What is your name?
Stephanie
Experiment by: Steph
Rolls: 10000 Average winning per roll: $0.90
Rolls: 20000 Average winning per roll: $0.91
Rolls: 30000 Average winning per roll: $0.92
Rolls: 40000 Average winning per roll: $0.91
Rolls: 50000 Average winning per roll: $0.92
Rolls: 60000 Average winning per roll: $0.91
Rolls: 70000 Average winning per roll: $0.91
Rolls: 80000 Average winning per roll: $0.91
Rolls: 87654 Average winning per roll: $0.91
Example2:
How many sides should the second die have?
6
How many times should we roll the dice?
400
How often should we print results?
100
What is your name?
Joe
Experiment by: Joe
Rolls: 100 Average winning per roll: ($0.45)
Rolls: 200 Average winning per roll: ($0.30)
Rolls: 300 Average winning per roll: ($0.37)
Rolls: 400 Average winning per roll: ($0.33)
Rolls: 400 Average winning per roll: ($0.33)
Program
import java.text.NumberFormat;
import java.util.Scanner;
class Die {
//------- data
private int numSides;
private java.util.Random rGen;
//------- constructors
public Die() {
this(6); // call parameterized
constructor, as if 6 were passed in
}
public Die(int theSides) {
if (theSides < 4)
throw new
IllegalArgumentException("A Die cannot have less than 4sides");
numSides = theSides;
rGen = new
java.util.Random(theSides);
}
//------- methods
//getNumSides - returns the number of sides
public int getNumSides() {
return numSides;
}
//roll - returns a random number from 1-numSides. The seed for
the
//random number generator should be the numSides
// (so everyone's results are the same...)
public int roll() {
return rGen.nextInt(numSides) +
1;
}
}
public class DiceGambleSimulation {
public static void main(String[] args) {
// for taking console input
Scanner sc = new
Scanner(System.in);
// for changing value to
currency
Die sixSided = new Die();// permenant die for all game
// taking user input
System.out.println("How many sides
should the second die have?");
int sides = sc.nextInt();
System.out.println("How many times
should we roll the dice?");
int roll = sc.nextInt();
System.out.println("How often
should we print results?");
int resultPrint =
sc.nextInt();
sc.nextLine();
System.out.println("What is your
name?");
String name = sc.nextLine();
// int sides = 4;
// int roll = 87654;
// int resultPrint=10000;
// String name="Stephanie";
Die customeSide = new Die(sides);//
custome die by user
double moneyCal = 0, avg;
int totalRun = 0, rollCal;
int newTotal = roll /
resultPrint;//calcualting how many time the main loop will
run
double[] ar = new
double[newTotal];
int count =0;
System.out.println("Experimented
by: " + name);
while (newTotal > 0) {// main
loop
for (int i = 0;
i < resultPrint; i++) {// loop for calcuating money
// getting roll value
rollCal = sixSided.roll() +
customeSide.roll();
// chaing how much won or lost and adding it to
moneyCal
if (rollCal < 5)
moneyCal += 3;
else if (rollCal == 5)
moneyCal += 8;
else
moneyCal += -2;
}
totalRun +=
resultPrint;// total times the roll happened
// money
calculation
avg = moneyCal /
resultPrint;
//putting it
inside array for overall average
ar[count] = avg;
count++;
printResult(totalRun, avg);
moneyCal =
0;
newTotal--;
}
//calculating the average of all
money
for(int i=0; i<ar.length; i++)
{
moneyCal+=ar[i];
}
avg = moneyCal / ar.length;
printResult(roll, avg);
sc.close();
}
//for printing the formated results
public static void printResult(int rolls, double avg)
{
NumberFormat money =
NumberFormat.getCurrencyInstance();
System.out.print("Rolls: " + rolls
+ " Average winning per roll: ");
if (avg >= 0)
System.out.println(money.format(avg));
else {// when amount is
negetive
avg =
Math.abs(avg);
System.out.println("(" + money.format(avg) + ")");
}
}
}
Output