Heads or Tails java program https://github.com/seccodingguy/CMSC325/tree/master/
ID: 3678864 • 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.io.*;
import java.math.*;
class Coin_Toss {
public static void main(String args[])throws Exception {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
double d, drop, side, s_d, re=1;
int tries=0, score=0;
while(re==1) {
tries++;
System.out.println("-> Hi, please choose your side -> Press 1 for Head -> Press 2 for Tails");
side=Integer.parseInt(br.readLine());
drop=Math.random();
d = Math.round(drop);
if(d== 0) {
s_d = 1;
}
else {
s_d = 2;
}
if(s_d == side) {
System.out.println("-> You Won");
score++;
}
else {
System.out.println("-> You Lose");
}
if(s_d==1) {
System.out.println("->Its Heads");
}
else if(s_d==2) {
System.out.println("-> Its tails");
}
System.out.println("-> Press 1 to Toss again -> Press 2 to Quit");
re=Double.parseDouble(br.readLine());
if(re==2) {
System.out.println("You Scored "+score+" out of "+tries);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.