1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
kap26 [50]
3 years ago
8

Write a program that simulates flipping a coin to make decisions. The input is how many decisions are needed, and the output is

either heads or tails. Assume the input is a value greater than 0.
Ex: If the input is 3, the output is:
tails
heads
heads
For reproducibility needed for auto-grading, seed the program with a value of 2. In a real program, you would seed with the current time. In that case, every program's output would be different, which is what is desired but can't be auto-graded.
Note: A common student mistake is to create an instance of Random before each call to rand.nextInt(). But seeding should only be done once, at the start of the program, after which rand.nextInt() can be called any number of times.
Your program must define and call the following method that returns "heads" or "tails".
public static String HeadsOrTails(Random rand)

Computers and Technology
1 answer:
Travka [436]3 years ago
5 0

Answer:

Here is the JAVA program:

import java.util.Scanner;  // to get input from user

import java.util.Random;   // to generate random numbers

public class CoinFlip {  

// function for flipping a coin to make decisions

  public static String HeadsOrTails(Random rand) {

//function to return head or tail

     String flip;   // string type variable flip to return one of the heads or tails

     if ((rand.nextInt() % 2) == 0)  

// if next random generated value's mod with 2 is equal to 0

        {flip = "heads";}  // sets the value of flip to heads

     else   // when the IF condition is false

       { flip = "tails";}   // sets value of flip to tails

  return flip;    }    //return the value of flip which is one of either heads or tails

  public static void main(String[] args) {  //start of main function body

     Scanner rand= new Scanner(System.in);  // creates Scanner instance

// creates random object and seeds the program with value of 2

     Random no_gen = new Random(2);  

     int decision = rand.nextInt();   //calls nextInt() to take number of decisions

     for (int i = 0; i < decision; i++) {

// produces heads or tails output for the number of decisions needed

        System.out.println(HeadsOrTails(no_gen));       }    }    }

//prints heads or tails

Explanation:

The above program has method HeadsOrTails(Random rand) to return the heads or tails based on the condition if ((rand.nextInt() % 2) == 0) . Here nextInt() is obtains next random number from the sequence of random number generator and take the modulus of that integer value with 2. If the result of modulus is 0 then the value of flip is set to heads otherwise tails.

In the main() function, a random object is created and unique seed 2 is set. The decision variable gets the number of decisions. In the for loop the loop variable i is initialized to 0. The loop body continues to execute until the value of i exceeds the number of decisions needed. The loop body calls the HeadsOrTails(Random rand) method in order to output either heads or tails.

The program and output is attached in a screenshot.

You might be interested in
A user asks for a checkbox to be automatically ticked if the annual revenue field is greater than a million. Which formula to tr
Otrada [13]

Answer:

AnnualRevenue > 1000000

Explanation:

First, represent the field for annual revenue with AnnualRevenue

From the question, we understand that this field must be greater than 1000000 before a certain action can be performed.

Base on the given condition, the formula that triggers this action is: AnnualRevenue > 1000000

<em>Further explanation is in the attachment</em>

<em></em>

<em>From the attachment, we have that:</em>

If the condition is true, the checkbox be ticked

However, if the condition is false; the checkbox remain unchanged because no specific action is stated for this, in the question.

<em></em>

6 0
3 years ago
A professional development plan allows an employee to
drek231 [11]
Answer: (B) evaluate current skill level and determine what skills are necessary to advance

Explanation: A professional development plan is used to assess your current skills and see what’s needed to further advance within the field.
7 0
3 years ago
List the steps to identify address or link window on a phone​
Law Incorporation [45]

Answer: this is how

Explanation: if you are on your phone and a link pops up if it is blue then it is able to be clicked but if it is not blue you can simply copy and paste the link into your web browser.

7 0
3 years ago
The part of the computer that contains the brain, or central processing unit, is also known as the A. monitor. B. keyboard. C. m
Paul [167]

The part of the computer that contains the brain, or central processing unit, is also known as the system unit. Correct answer: D

The system unit contains the main components of a computer. The keyboard, mouse and monitor on the other hands are the peripheral devices.  The system unit contains the CPU, motherboard, hard drive, and RAM.

6 0
3 years ago
Read 2 more answers
Create a recursive method, a method that calls itself, that returns true or false depending on whether or not a given string is
ICE Princess25 [194]

Answer:

public class CheckPalindrome{

public static boolean IsPalindrome(String str){

if(str.Length<1)

{

return true;

}

else

{

if(str[0]!=str[str.length-1])

return false;

else

return IsPalindrome(str.substring(1,str.Length-2));

}

}

public static void main(string args[])

{

//console.write("checking palindromes");

string input;

boolean flag;

input=Console.ReadLine();

flag= IsPalindrome(input);

if(flag)

{

Console.WriteLine("Received"+input+"is indeed palindrome");

}

else

{

Console.WriteLine("received"+input+"is not a palindrome");

}

}

Explanation:

6 0
3 years ago
Other questions:
  • If you are asked to bring in agile way of working into the way a meeting runs, which one among the listed options will you imple
    11·1 answer
  • Need answers for 11&amp;12. Due today. Thanks.
    14·1 answer
  • Gary says, "Ports are where data enter a network before reaching a gateway. The data travel via transmission media." Explain why
    12·2 answers
  • Computers store temporary Internet files in the Recycle Bin. These files take up space and slow down a computer. Which tool can
    14·1 answer
  • Describe each of the six steps of the selection process as illustrated in chapter 12
    13·1 answer
  • Create a stored procedure sp_Q1 that takes two country names like 'Japan' or 'USA'as two inputs and returns two independent sets
    10·1 answer
  • What icon is usually used to indicate an attachment feature?
    14·2 answers
  • 23+ Composition refers to
    11·1 answer
  • Which option represents the location of the Goal Seek function?
    8·2 answers
  • Many universities form tight-knit alumni networks, causing the alumni to continue to use each other as a resource for career pla
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!