Need help to fix my code below this question. Im having problems getting the pro
ID: 3528161 • Letter: N
Question
Need help to fix my code below this question. Im having problems getting the proper output that is at the bottom of question 1. Need help
Proper output is above, this is what application should produce
My code is below
------------------------------
die.java
------------------------------
package question2;
import java.util.Scanner;
import java.util.Scanner;
import java.util.Random;
public class die
{
private int faceValue;
Scanner input = new Scanner(System.in);
private final int sides=input.nextInt();
private int face;
public die()
{
face = 1;
}
public void roll()
{
face = (int)(Math.random() * sides) +1;
}
public void setFaceValue(int value)
{
face=value;
}
public int getFace()
{ return face;
}
public String s()
{
String result = "You rolled: " + face;
return result;
}
}
-------------------------------------------
dietester.java
-----------------------------------------------
package question2;
public class dietester
//create runner
{
public static void main (String[] args)
{
System.out.print("Enter 4, 6, 8, 10, 20, 100 sides:");
die d1, d2 ;
int sum ;
d1 = new die() ;
d2 = new die() ;
d1.roll() ;
d2.roll() ;
System.out.println ( " Die One: " + d1 + " , Die Two: " + d2 ) ;
sum = d1.getFace() + d2.getFace();
System.out.println ( " Sum: " + sum ) ;
}
}
Explanation / Answer
please rate - thanks
import java.util.Scanner;
public class dietester
//create runner
{
public static void main (String[] args)
{
Scanner in=new Scanner(System.in);
System.out.print("Creating two dice - one with 6 sides, enter number of sides for the second one:");
int sides=in.nextInt();
die d1, d2 ;
int sum ;
d1 = new die() ;
d2 = new die(sides) ;
System.out.println(d1.toString());
System.out.println(d2.toString());
System.out.println("Rolling each separately and printing the values:");
d1.roll() ;
d2.roll() ;
System.out.println ( " Die 1 showing: " + d1.getValue());
System.out.println ( " Die 2 showing: " + d2.getValue());
sum = d1.getValue() + d2.getValue();
System.out.println ( " Sum of the dice: " + sum ) ;
System.out.println("for loop to roll the two dice 10 times and show the values and the sum:");
System.out.println("Die 1 Die 2 Sum");
for(int i=0;i<10;i++)
{d1.roll();
d2.roll();
sum=d1.getValue() + d2.getValue();
System.out.println(d1.getValue()+" "+d2.getValue() + " "+sum);
}
die y1,y2,y3,y4,y5;
int max=-1 ,min=999999 ,total=0,count=0;
double avg;
y1 = new die() ;
y2 = new die() ;
y3 = new die() ;
y4 = new die() ;
y5 = new die() ;
System.out.println(y1.toString());
System.out.println(y2.toString());
System.out.println(y3.toString());
System.out.println(y4.toString());
System.out.println(y5.toString());
System.out.println("Rolling until i find Yahtzees. Counting number of rolls it takes to get each one");
for(int i=0;i<1000;i++)
{count=0;
do
{y1.roll();
y2.roll();
y3.roll();
y4.roll();
y4.roll();
count++;
}while(y1.getValue()!=y2.getValue()||y1.getValue()!=y3.getValue()||y1.getValue()!=y4.getValue()||y1.getValue()!=y5.getValue());
if(count>max)
max=count;
if(count<min)
min=count;
total+=count;
}
avg=total/1000.;
System.out.println("Found 1000 Yahtzes. Max rolls to find one: "+max+" min rolls: "+min+" avg rolls: "+avg);
}
}
-----------------------------------
import java.util.Scanner;
import java.util.Random;
public class die
{
private int faceValue;
private int sides;
public die()
{
sides=6;
roll();
}
public die(int s)
{if(s==2||s==6||s==8||s==10||s==12||s==20||s==100)
sides=s;
else
sides=6;
roll();
}
public void roll()
{
faceValue = (int)(Math.random() * sides) +1;
}
public int getValue()
{ return faceValue;
}
public String toString()
{
String result = "Die["+sides+" sides, value="+faceValue+"]";
return result;
}
}