Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Heads or Tails java program https://github.com/seccodingguy/CMSC325/tree/master/

ID: 3678682 • Letter: H

Question

Heads or Tails java program

https://github.com/seccodingguy/CMSC325/tree/master/src/cmsc325gameconcepts

Using the Strategy and StrategyRandom class, create a game that prompts the user to choose heads or tails, flips a virtual coin, and then displays the results to the user. The game must continue until 100 flips have been completed. Save the results of the flip results in a format of your choosing.

Modify the code to include the StrategyProbability class, using the results saved from the StrategyRandom as the starting values for the setMoves() function. The game must continue until 100 flips have been completed. Save the results of the flip results in a format of your choosing and separate from the StrategyRandom results.

For both 2 and 3, generate statistics on the odds of getting the right guess as well as the number of heads and tails flipped, displayed, respectively, to the user as well as saved with the results.

The program does not have to use graphics of actual coins - this is optional.

The 100 flips can be generated manually, i.e. user pressing a button, or automatically.

Thanks,

Explanation / Answer

import java.util.*; import javax.swing.*; public class CoinFlipTest { public static void main(String[] args) { int select, rand, result; String selectvalues[] = {"Heads", "Tails"}; String coinSides=null, userGuess=null; /** Instantiate an array list for storing outcomes. **/ ArrayList outcomes = new ArrayList(); for (int i=0; i 0 } else { userGuess = "wrong"; outcomes.add(1); // if wrong => 1 } /** Display a result. **/ JOptionPane.showMessageDialog(null, "The result is " + coinSides + "! " + "Your guess is " + userGuess +"!", "Result", JOptionPane.INFORMATION_MESSAGE); } /** Display the odds **/ JOptionPane.showMessageDialog(null, "Your odds of getting the right guess are " + calculateOdds(outcomes), "Your Odds", JOptionPane.INFORMATION_MESSAGE); } /** Generate a random value for flip of a coin. **/ private static int generateRandomValue() { int r = (int) (Math.random()*2); return r; } /** Calculate user odds. **/ private static float calculateOdds(ArrayList list) { int numOfRightAnswers=0, numOfWrongAnswers=0; Iterator it = list.iterator(); while(it.hasNext()) { Integer e = (Integer) (it.next()); if (e == 0) { numOfRightAnswers++; } else { numOfWrongAnswers++; } } // odds = the number of desirable outcomes/the number of undesirable outcomes return (float) numOfRightAnswers/numOfWrongAnswers; } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote