In: Computer Science
Write an application that simulates coin tossing. Let the program toss a coin each time the user chooses the “Toss Coin” menu option. Count the number times each side of the coin appears. Display the results. The program should call a method flip( ) that takes no arguments and returns a zero to represent a tail or a one to represent a head. There is a Random class that will allow you to generate a random integer. Import it from java.util.Random.?
Answer :
here is the fiip method as per your requirement
Raw code:
//importing Random
import java.util.Random;
//main
class Main {
//method static flip
public static int flip(){
//creating random object
Random rand= new Random();
//getting random number between 0 and 1
int randomNumber=rand.nextInt(2);
//return the generated number
return randomNumber;
}
//main
public static void main(String[] args) {
//calling the static method
if(flip()==1){
System.out.println("tail");
}
else{
System.out.println("head");
}
}
}
Editor:
output:
Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.
"Please refer to the screenshot of the code to understand the indentation of the code".
Thank you! Do upvote.