Java Programming. Please make sure program compiles, the code is copyable, and s
ID: 3848969 • Letter: J
Question
Java Programming. Please make sure program compiles, the code is copyable, and screenshots of the output are provided for 5 stars :-)
3) 19 points] You've been hired by Chart Checkers to write a Java console application that repeatedly rolls a seven-sided die and tabulates the roll counts. Use a validation loop to prompt for and get from the user the number of rolls to simulate in the range 10-1,000,000. Store the counts for the simulation in an array Declare the array as a private static field so that it doesn't need to be passed around. After each simulation, print the counts. Format the output in three columns with the first column containing a number (1-7), the second column containing its count, and the third column containing the percentage of that count over the total rolls. Format the counts with commas. Format the percentages with two decimal places. Calculate and print the sum of the counts. Also, chart theExplanation / Answer
Note:
The mentioned example black board code does not given so not able to include those parts to fulfil all requirements,solution is prepared as two parts, one is witout pie chart another one is with piechart..
Solutions:
Code without pie chart :
import java.util.Random;
import java.util.Scanner;
import java.text.NumberFormat;
public class SevenSideDie
{
private static int SevenSarray[]=new int[7];
public static void main(String[] args)
{
Random rnddice = new Random();
int dcenum, dceout;
Scanner scninpt = new Scanner(System.in);
// code to read input continuously until 0
while(true)
{
System.out.println("Seven sided die ");
System.out.println("How many simulations (10-1000000)?");
dcenum = scninpt.nextInt();
// code to validate input
if(dcenum!=0)
{
// code to reset array
for(int lp=0;lp<7;lp++)
{
SevenSarray[lp]=0;
}
// code to run the dice and store the value
for(int lp=0;lp<dcenum;lp++)
{
dceout = rnddice.nextInt(7) + 1;
SevenSarray[dceout-1] =SevenSarray[dceout-1]+1;
}
System.out.println(" The rolled statistics is.");
// code to display the statistics
for(int lp=0;lp<7;lp++)
{
System.out.println((lp+1)+" " + NumberFormat.getNumberInstance().format(SevenSarray[lp]) + " "+String.format( "%.2f",(SevenSarray[lp]/(double)dcenum)));
}
int total=0;
//code to count the total
for(int lp=0;lp<7;lp++)
{
total=total+SevenSarray[lp];
}
// code display the total count
System.out.println(" The total count is " +total);
}
else
break;
}
}
}
Result:
Seven sided die
How many simulations (10-1000000)?
10
The rolled statistics is.
1 2 0.20
2 2 0.20
3 0 0.00
4 2 0.20
5 0 0.00
6 3 0.30
7 1 0.10
The total count is 10
Seven sided die
How many simulations (10-1000000)?
100000
The rolled statistics is.
1 14,323 0.14
2 14,418 0.14
3 14,158 0.14
4 14,325 0.14
5 14,263 0.14
6 14,143 0.14
7 14,370 0.14
The total count is 100000
Seven sided die
How many simulations (10-1000000)?
1000000
The rolled statistics is.
1 142,637 0.14
2 142,508 0.14
3 143,295 0.14
4 143,281 0.14
5 142,973 0.14
6 142,532 0.14
7 142,774 0.14
The total count is 1000000
Seven sided die
How many simulations (10-1000000)?
Code with pie chart:
import java.util.Random;
import java.util.Scanner;
import java.text.NumberFormat;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
// Class for dice roll with pie chart
public class DicePieChart extends ApplicationFrame
{
private static int SevenSarray[]=new int[7];
// constructor to create a panel
public DicePieChart( String chrttle ) {
super( chrttle );
setContentPane(createChartPanel( ));
}
// method to make dataset
private static PieDataset makeDataset( ) {
DefaultPieDataset dicedataset = new DefaultPieDataset( );
for(int itr=0;itr<7;itr++)
{
dicedataset.setValue(itr, SevenSarray[itr] );
}
return dicedataset;
}
// method to create a chart
private static JFreeChart createChart( PieDataset
dicedataset ) {
// set chart title, data, and in legend
JFreeChart dicechart = ChartFactory.createPieChart( "Dice Roll with Pie Chart", dicedataset, true,true, false);
return dicechart;
}
// method to create a panel
public static JPanel createChartPanel( ) {
JFreeChart dicechart = createChart(makeDataset( ) );
return new ChartPanel( dicechart );
}
public static void main( String[ ] args )
{
// Java code to roll the seven side dice and print the result in console
Random rnddice = new Random();
int dcenum, dceout;
Scanner scninpt = new Scanner(System.in);
// code to read input continuously until 0
while(true)
{
System.out.println("Seven sided die ");
System.out.println("How many simulations (10-1000000)?");
dcenum = scninpt.nextInt();
// code to validate input
if(dcenum!=0)
{
// code to reset array
for(int lp=0;lp<7;lp++)
{
SevenSarray[lp]=0;
}
// code to run the dice and store the value
for(int lp=0;lp<dcenum;lp++)
{
dceout = rnddice.nextInt(7) + 1;
SevenSarray[dceout-1]= SevenSarray[dceout-1]+1;
}
System.out.println(" The rolled statistics is.");
// code to display the statistics
for(int lp=0;lp<7;lp++)
{
System.out.println((lp+1)+" " + NumberFormat.getNumberInstance().format(SevenSarray[lp]) + " "+String.format( "%.2f",(SevenSarray[lp]/(double)dcenum)));
}
int cnttot=0;
//code to count the total
for(int lp=0;lp<7;lp++)
{
cnttot=cnttot+SevenSarray[lp];
}
// code display the total count
System.out.println(" The total count is " + cnttot);
}
else
break;
}
// code to create a pie chart
DicePieChart piechartdemo = new DicePieChart( "Dice Roll
with Pie Chart" );
piechartdemo.setSize( 560 , 367 );
RefineryUtilities.centerFrameOnScreen( piechartdemo );
piechartdemo.setVisible( true );
}
}
Rolls Percentage of 6s roled
10 0.30%
100,000 0.14%
1,000,000 0.14%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.