Question: need help getting this java program to work. coin flip program The code is import java.util.Scanner; import java.util.Random; class Simulate { int Simulate() { System.out.println(Enter

need help getting this java program to work. coin flip program

The code is

import java.util.Scanner;

import java.util.Random;

class Simulate

{

int Simulate()

{

System.out.println("Enter the numbers of coin toss");

Scanner in =new Scanner(System.in);

int n=in.nextInt();

return n;

}

}

class Coin

{

private int headsCount=0;

private int tailsCount=0;

public void toss(int n) {

//num=num+1;

String[] str1={"heads","tails"}; //create a string of array with heads and tails

Random r = new Random(); //create a random object

String random = (str1[new Random().nextInt(str1.length)]); //generate a random of string between heads and tails

System.out.println(n+1+". "+random); //prints the random choosed heads or tails

//this if statement for calculation of percentage of heads and tails

if(random=="heads") //checks if random has selected heads

headsCount+=1; //this will counts the heads

if(random=="tails") //checks if random has selected tails

tailsCount+=1; //this will counts the tails

}

public int getHeadsCount() {

return headsCount;

}

public int getTailsCount() {

return tailsCount;

}

}

public class Driver

{

public static int NoofHeads,NoofTails,num=0; //static variable for use in both of the coinToss class and the toss method

public static void main(String[] args) {

Simulate toss=new Simulate();

int number=toss.Simulate();

Coin c=new Coin();

for(int i=0;i

c.toss(i); //toss method to generate heads and tails

NoofHeads=c.getHeadsCount(); //assign the No of heads

NoofTails=c.getTailsCount(); //assign the No of tails

System.out.println("heads= "+NoofHeads +" Tails= " +NoofTails); //prints the no of heads and tails

System.out.println(NoofHeads*100/20 +"% heads " +NoofTails*100/20 +"% tails"); //prints the heads and tails

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!