menu
bartleby
search
close search
Hit Return to see all results

Write a program called HeadsOrTails, that lets the user guess whether the flip of a coinresults in heads or tails. The program randomly generates an integer 0 or 1, whichrepresents head or tail. The program prompts the user to enter a guess and reports whetherthe guess is correct or incorrect.1) Create a Scanner2) Declare a variable called coin to be a random number of 0 and 13) Ask the user to enter a guess of 0 or 1 (0: heads, 1: tails)4) If the guess is equal to the coin, print a Message, "You guessed it!"5) Otherwise, print the result and the message, "Better luck next time"Sample Runs:Enter guess of 0 or 1 (0: heads, 1: tails)Invalid input!Enter guess of 0 or 1 (0: heads, 1: tails)0The coin is 1 (tailis) - Better luck next timeEnter guess of 0 or 1 (0: heads, 1: tails)You guessed it!

Question

For this program you need to use Eclipse 

 

Write a program called HeadsOrTails, that lets the user guess whether the flip of a coin
results in heads or tails. The program randomly generates an integer 0 or 1, which
represents head or tail. The program prompts the user to enter a guess and reports whether
the guess is correct or incorrect.
1) Create a Scanner
2) Declare a variable called coin to be a random number of 0 and 1
3) Ask the user to enter a guess of 0 or 1 (0: heads, 1: tails)
4) If the guess is equal to the coin, print a Message, "You guessed it!"
5) Otherwise, print the result and the message, "Better luck next time"
Sample Runs:
Enter guess of 0 or 1 (0: heads, 1: tails)
Invalid input!
Enter guess of 0 or 1 (0: heads, 1: tails)
0
The coin is 1 (tailis) - Better luck next time
Enter guess of 0 or 1 (0: heads, 1: tails)
You guessed it!
help_outline

Image Transcriptionclose

Write a program called HeadsOrTails, that lets the user guess whether the flip of a coin results in heads or tails. The program randomly generates an integer 0 or 1, which represents head or tail. The program prompts the user to enter a guess and reports whether the guess is correct or incorrect. 1) Create a Scanner 2) Declare a variable called coin to be a random number of 0 and 1 3) Ask the user to enter a guess of 0 or 1 (0: heads, 1: tails) 4) If the guess is equal to the coin, print a Message, "You guessed it!" 5) Otherwise, print the result and the message, "Better luck next time" Sample Runs: Enter guess of 0 or 1 (0: heads, 1: tails) Invalid input! Enter guess of 0 or 1 (0: heads, 1: tails) 0 The coin is 1 (tailis) - Better luck next time Enter guess of 0 or 1 (0: heads, 1: tails) You guessed it!

fullscreen
check_circleAnswer
Step 1

The program has the following algorithm:

  1. Declare the necessary variables within the “main()” function.
  2. Create object for “Scanner()” to get the guess from the user (0: heads, 1: tails).
  3. If the guess is equal to the coin then print the statement “You guessed it”.
  4. Otherwise, print the statement “Better luck next time”.
Step 2

Java program is as follows:

// Import the required package
import java.util.Scanner;
 
// Definition of class
public class HeadsOrTails
{
// Definition of main() method
public static void main (String [] args)
{
// Declare required variables
int coin, guess;
String num;
 
// Generate the random number 0 or 1
coin = (int) (Math.random() * 2);
 
// Create an object for scanner
Scanner sc = new Scanner(System.in);
 
// Get an input from user
System.out.println("Enter a guess 0 or 1(0: heads, 1: tails) ");
guess = sc.nextInt();
 
// Check guess is equal to 0
if (guess == 0)
{
// Condition is true, assign num to heads
num = "heads";
}
 
// Otherwise
else
{
// Assign num to tails
num = "tails";
}
 
// Check guess is not equal to 0 and 1
if (guess != 0 && guess != 1)
{
// Condition is true, print invalid input
System.out.println("Invalid input!");
}
 
// Otherwise, Check the guess is equal to coin
else if (guess == coin)
{
// Print the message
System.out.println("You guessed it!");
}
 
// Otherwise
else
{
// print the message
System.out.println("The coin is " + guess + " (" + num + ")- Better luck next time");
}
}
}
Step 3

The executed output is as follows:

Sample out...

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Our solutions are written by experts, many with advanced degrees, and available 24/7

See Solution
Tagged in

Engineering

Computer Engineering

Other

Related Computer Engineering Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: How would I solve this problem in python language Grades a) Write a program that reads in the names ...

A: Program Instructions:Use the open() function to read from the file.Save the result o spilt() functio...

question_answer

Q: What is the CPU time if the number of instructions executed by the program is 500 cycles per instruc...

A: CPU time:The formula for computing the CPU time is provided below:

question_answer

Q: how can I create comments in python?

A: In python, # character is used to start the single line comments. Place a hash symbol in front of a ...

question_answer

Q: What is the reduced form of the function F X(XY)+X(X+Y)? a) Y b) X c) 0 d) 1

A: Reduction:The reduced form of the function “F” is computed as follows:

question_answer

Q: Based on Program.Py below, define and describe a soft process (using a process model), which you wil...

A: Solution:Process model:The software process contains the four activities such as software specificat...

question_answer

Q: I need  crow ERD Model with PK, FK and Referential integrity Arrow Electronics is a consumer electro...

A: The 8 entities are indicated below with only their unqiue identifier (underlined) and one or more ot...

question_answer

Q: Consider the window size is 10, bandwidth is 1500 bps, transmission delay is 2 ms and propagation de...

A: Formula for Throughput:In Go-back-N protocol, the formula for computing the throughput is given belo...

question_answer

Q: Write a program in Eclipse called LoopMinMax that reads in 4 values from the user and prints the low...

A: Algorithm: The program has the following algorithm:The class name is “LoopMinMax” and in the main me...

question_answer

Q: Assume that a firm has already implemented a KMS system, as a manager, how do you ensure that learni...

A: Protecting knowledge of the organization is a typically an IP protection problem. From the case stud...

Sorry about that. What wasn’t helpful?