Java Programming. Please make sure program compiles, the code is copyable, and s
ID: 3848970 • 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
make sure both programs(java files)(DieCounts.java and PieChart.java) are in same folder.
make sure that your program can access package "jfreechart-1.0.19.jar"
you can download package from link:
https://sourceforge.net/projects/jfreechart/files/latest/download?source=files
DieCounts.java
//package files;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class DieCounts {
private static int sideCounts[] = new int[7];
private int rolls;
public int rollDie(){
Random random = new Random();
return random.nextInt(7)+1;
}
public int[] getSideCounts(){
return sideCounts;
}
public void storeCountsForaSimulation(int no_of_rolls){
for(int i=1;i<=no_of_rolls;i++)
sideCounts[rollDie()-1]++;
}
public void printResult(){
System.out.println("Result:");
System.out.println("(1-7) Count Percentage --------------------------");
for(int i=0;i<sideCounts.length;i++){
System.out.println(printFormat(i));
}
System.out.println("Sum of Total Counts: "+rolls);
}
public String printFormat(int index){
return String.format("%d %d %1.2f", index+1,sideCounts[index],(float)sideCounts[index]/rolls);
}
public void startRollingDie(){
Scanner input = new Scanner(System.in);
int no_of_rolls;
do{
System.out.println("Enter no. of rolls:(10 to 1000000)");
no_of_rolls = input.nextInt();
}
while(no_of_rolls<10 || no_of_rolls>1000000);
input.close();
//now no_of_rolls store a number between 10 to 1000000
rolls = no_of_rolls;
storeCountsForaSimulation(no_of_rolls);
//System.out.println(Arrays.toString(sideCounts));
printResult();
}
}
PieChart.java
//package files;
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;
public class PieChart extends ApplicationFrame {
public PieChart( String title ) {
super( title );
setContentPane(createDemoPanel( ));
}
private static PieDataset createDataset( ) {
DefaultPieDataset dataset = new DefaultPieDataset( );
DieCounts dc = new DieCounts();
dc.startRollingDie();
for(int i=1;i<=7;i++)
dataset.setValue(""+i, dc.getSideCounts()[i-1]);
return dataset;
}
private static JFreeChart createChart( PieDataset dataset ) {
JFreeChart chart = ChartFactory.createPieChart(
"Die Counts", // chart title
dataset, // data
true, // include legend
true,
false);
return chart;
}
public static JPanel createDemoPanel( ) {
JFreeChart chart = createChart(createDataset( ) );
return new ChartPanel( chart );
}
public static void main( String[ ] args ) {
PieChart demo = new PieChart( "Die Counts" );
demo.setSize( 560 , 367 );
RefineryUtilities.centerFrameOnScreen( demo );
demo.setVisible( true );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.