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
Which RAID configuration, known as byte-striped with error check, spreads the data across multiple disks at the byte level with
wel

Considering the computer system technology, the RAID configuration, known as byte-striped with an error check, and spreads the data across multiple disks at the byte level with one disk dedicated to parity bits is known as <u>RAID Level 5.</u>

<h3>What is the RAID Level 5?</h3>

RAID Level 5 is the Redundant Array of Inexpensive (or Independent) Disks Level 5. RAID Level 5 works on strips to transfer data over multiple disks in an array.

RAID Level 5 is also known to record information, with the ability to withstand numerous failures.

<h3>Other types of Redundant Array of Inexpensive (or Independent) Disks</h3><h3 />
  • RAID level 0
  • RAID level 1
  • RAID level 2
  • RAID level 3
  • RAID level 4

Hence, in this case, it is concluded that the correct answer is RAID Level 5.

Learn more about RAID configuration here: brainly.com/question/9305378

8 0
3 years ago
Who has a brainpop acct. can i pls use it !
RSB [31]

Answer:

Союз нерушимый республик свободных

Сплотила навеки Великая Русь.

Да здравствует созданный волей народов

Единый, могучий Советский Союз!

Славься, Отечество наше свободное,

Дружбы, народов надежный оплот!

Знамя советское, знамя народное

Пусть от победы, к победе ведет!

Сквозь грозы сияло нам солнце свободы,

И Ленин великий нам путь озарил.

Нас вырастил Сталин - на верность народу

На труд и на подвиги нас вдохновил.

Славься, Отечество чаше свободное,

Счастья народов надежный оплот!

Знамя советское, знамя народное

Пусть от победы к победе ведет!

Skvoz grozy siialo nam solntse svobody,

I Lenin velikij nam put ozaril.

Nas vyrastil Stalin - na vernost narodu

Na trud i na podvigi nas vdokhnovil.

Slavsia, Otechestvo chashe svobodnoe,

Schastia narodov nadezhnyj oplot!

Znamia sovetskoe, znamia narodnoe

Pust ot pobedy k pobede vedet!

Мы армию нашу растили в сраженьях,

Захватчиков подлых с дороги сметем!

Мы в битвах решаем судьбу поколений,

Мы к славе Отчизну свою поведем!

Славься, Отечество наше свободное,

Славы народов надежный оплот!

Знамя советское, знамя народное

Пусть от победы к победе ведет!Explanation:

8 0
3 years ago
Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of strings
Ghella [55]

Answer:

count = 0;

longest =0;

String myString = new String();

while (input.hasNext()){

myString = input.next();

if (myString.length() == longest) count++;

else

if (myString.length() > longest){

longest = myString.length();

count = 1;

}

}

Explanation:

5 0
3 years ago
Read 2 more answers
9. Government and corporate officials concerned about security threats do not bring their own cell phones or laptops when travel
tatuchka [14]

Answer:

True

Explanation:

It is known as Don-Not-Carry rules are implemmented to avoid

-Unauthorized full disk copies (it have been made while the laptop owner was out of the hotel room on overseas travel)

-Laptops steals

- Monitoring by third parties of wireless using.

3 0
3 years ago
To specify grouping and sorting for a report, click the ____ button on the design tab in layout view.
inessss [21]
Hello <span>Sydney2640 </span><span>

Answer: To specify grouping and sorting for a report, click the group-and-sort button on the design tab in layout view.

Hope this helps
-Chris</span>
4 0
3 years ago
Other questions:
  • When choosing a communication channel, _____refers to the degree to which communications can be planned and recorded, thus allow
    13·1 answer
  • Which of the following is not a thing needed to make a network?
    6·2 answers
  • legacy application that has hard-coded static IP addresses and is running on an EC2 instance, what is the best failover solution
    11·1 answer
  • Write the code to call a function named send_variable and that expects a single int parameter. Suppose a variable called x refer
    6·1 answer
  • Determine if the following identifiers are valid. If they are invalid, state the reason. For example, the identifier min value i
    9·1 answer
  • For the client-server application over TCP, why must the server program be executed before the client program? For the client se
    13·1 answer
  • WILL GIVE BRAINLIEST AND HALLOWEEN CANDY!!!!!!!!!<br> who wants to talk?
    9·1 answer
  • Pls help due tonight<br>Will give brainiest
    13·2 answers
  • When individuals are purchasing a computer, they sometimes might get the most expensive computer they can afford. Why might this
    7·1 answer
  • An array name is a pointer constant because the address stored in it cannot be changed during runtime.
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!