Write a program that simulates tossing a coin. //Prompt the user for how many times to toss the coin. //Code a method with no parameters that randomly returns either the String "heads"or the string "tails". //Call this method in main as many times as requested and report the results.
// Sharissa Sullivan
// COP 2250
// Chapter 6 Method
// Method with No permeters
package sullivan4;
import java.util.*;
public class Sullivan4_2 {
// Define a method for the flip of a coin - heads or tails
class main {
public static String toss()
{
// Create Random Number object for heads/tails toss
Random randomNumber = new Random();
// Generate a random number: heads = 0, tails = 1
int flip = randomNumber.nextInt(2);
// If statement to flag flips with heads or tails
if (flip == 0)
return "heads";
else
return "tails";
}
// Define the main method
public main(final String[] args) { // public static void main(String[] args)
// Create Scanner object
Scanner userinput = new Scanner(System.in);
// Prompt the user to enter how many times they want the coin to be tossed
System.out.println("How many times should I toss the coin ?");
// Scan the value the user enters
int scan = userinput.nextInt();
// Declare variables
int heads = 0;
int tails =0;
int count;
// Create loop for coin toss
for (count = 1; count <= scan ; count++)
{
// toss the coin
String result = toss();
// If statement for toss = heads add 1 to heads count
if (result.equals("heads"))
heads++;
// Else add 1 to tails count
else
tails++;
}
// Print the totals of heads and tails
System.out.println( "Results of "+ scan + "tosses. Heads:" + heads + ",Tails:" + tails);
}
}
}
//Write a program that simulates tossing a coin.
//Prompt the user for how many times to toss the coin.
//Code a method with no parameters that randomly returns either the String "heads"or the string "tails".
//Call this method in main as many times as requested and report the results. See Example outputs below.
***I am getting an error message saying ...Error: Main method not found in class sullivan4.Sullivan4_2, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application *********
*****What is wrong with my code ?
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 4 images