whats wrong with my code import.java.util.Random; import.java.text.DecimalFormat; public…
- User
whats wrong with my code import.java.util.Random; import.java.text.DecimalFormat; public class MyProgram { public static void main(String[] args) { int[] rollCounts36 = simulateDiceRolls(36); int[] rollCounts500 = simulateDiceRolls(500); int[] rollCounts10000 = simulateDiceRolls(10000);
System.out.println("2 Die Simulator\n"); System.out.println("Theoretical Probability of rolling two die:"); printPaddedNumbers(2, 12); printPaddedProbabilities(2, 12); System.out.println("\n"); displayResults("Results Rolling 36 die:", rollCounts36, 36); displayResults("\nResults Rolling 500 die:", rollCounts500, 500); displayResults("\nResults Rolling 10000 die:", rollCounts10000, 10000); } public static int[] simulateDiceRolls(int numberOfRolls){ Random random = new Random(); int[] counts = new int[11]; for(int i = 0; i < numberOfRolls; i++) { int die1 = random.nextInt(6) + 1; int die2 = random.nextInt(6) + 1; int sum = die1 + die2; counts[sum - 2]++; } return counts; } public static double getTheoreticalProbability(int sum){ int[] occurences = {1,2,3,4,5,4,3,2,1}; return occurences[sum-2]/36.0; } public static voud printRightPaddedString(String str, int width){ System.out.print(str); for(int i = str.length(); i < width; i++){ System.out.print(" "); } } private static printPaddedNumbers(int start, int end){ System.out.println("X "); for(int i = start; i <= end; i++){ printRightPaddedString(String.valueOf(i), 10); } System.out.println(); private static void printPaddedProbabilities(int start, int end){ System.out.print("P(X) "); for(int i = start; i <= end, i++){ printRightPaddedString(new DecimalFormat("#.####").format(getTheoreticalProbability(i), 10); } System.out.println(); } public static void displayResults(String title, int[] rollcounts, int numberOfRolls){ DecimalFormat df = new DecimalFormat("#.####"); System.out.println(title); printPaddedNumber(2,12); System.out.print("\nCount"); for(int count : rollCounts){ printRightPaddedString(String.valueOf(count), 10); } System.out.print("\nCount "); for (int count : rollCounts) { printRightPaddedString(String.valueOf(count), 10); } System.out.print("\nEm Prob"); for (int count : rollCounts) { printRightPaddedString(df.format(count / (double) numberOfRolls), 10); } System.out.println("\n"); }}
- Assistant
There are several issues with your code. Let's address them one by one:
- Import syntax errors: The correct syntax for importing classes in Java does not include a period between the import keyword and the package name. Replace the incorrect lines:
import.java.util.Random; import.java.text.DecimalFormat;with the correct import statements:
import java.util.Random; import java.text.DecimalFormat;-
Method visibility issue:
printRightPaddedStringis defined aspublic static voud, which is incorrect. It's a typo and should bevoidto specify the return type. Replacevoudwithvoid. -
Missing method closing braces:
printPaddedNumbersandprintPaddedProbabilitiesmethods are missing their closing braces}. -
Syntax errors in methods: In the method
printPaddedProbabilities, there is a missing closing parenthesis and the second parameter10should be outside of theformatmethod call. Replace this line:
printRightPaddedString(new DecimalFormat("#.####").format(getTheoreticalProbability(i), 10);with:
printRightPaddedString(new DecimalFormat("#.####").format(getTheoreticalProbability(i)), 10);- Incorrect for loop condition syntax and a missing semicolon:
In the
printPaddedProbabilitiesmethod, the for loop uses a comma instead of a semicolon in its condition, and there's a missing semicolon at the end. Replace this line:
for(int i = start; i <= end, i++){with:
for(int i = start; i <= end; i++){- Consistency in method calls:
You have a method call
printPaddedNumber, which should beprintPaddedNumbers. Change this:
printPaddedNumber(2,12);to this:
printPaddedNumbers(2,12);- Method signature inconsistency:
In
displayResults, you are trying to passrollCountsto a method call, but you have the variablerollcountsas your parameter. Change this:
public static void displayResults(String title, int[] rollcounts, int numberOfRolls)to this:
public static void displayResults(String title, int[] rollCounts, int numberOfRolls)- Redundant code in
displayResults: You have two blocks of code that print the count, which would result in the count being printed twice. You can remove one of them.
After making these corrections, your code should have the chance of working correctly. Double-check for any additional typos or mistakes and ensure that all methods are implemented as intended. Remember to also handle any other methods (
printRightPaddedString) you may have included but have not provided the implementation in your original question.