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
Luden [163]
3 years ago
10

java Write a program that simulates tossing a coin. Prompt the user for how many times to toss the coin. Code a method with no p

arameters that randomly returns either the String "heads"or the string "tails". Call this method in main as many times as requested and report the results.
Engineering
2 answers:
igomit [66]3 years ago
5 0

Answer:

The source code to this question has been attached to this response. Please download it and go through the code.

The source code contains comments explaining important segments of the code. Kindly read the comments carefully for better readability and understandability of the code.

Download java
max2010maxim [7]3 years ago
4 0

Answer:

The solution code is written in Java.

  1. public class Main {
  2.    public static void main(String[] args) {
  3.        Scanner inNum = new Scanner(System.in);
  4.        System.out.print("Enter number of toss: ");
  5.        int num = inNum.nextInt();
  6.        for(int i=0; i < num; i++){
  7.            System.out.println(toss());
  8.        }
  9.    }
  10.    public static String toss(){
  11.        String option[] = {"heads", "tails"};
  12.        Random rand = new Random();
  13.        return option[rand.nextInt(2)];
  14.    }
  15. }

Explanation:

Firstly, we create a function <em>toss()</em> with no parameter but will return a string (Line 14). Within the function body, create an option array with two elements, "heads" and "tails" (Line 15). Next create a Random object (Line 16) and use <em>nextInt()</em> method to get random value either 0 or 1. Please note we need to pass the value of 2 into <em>nextInx() </em>method to ensure the random value generated is either 0 or 1.  We use this generate random value as an index of <em>option </em>array and return either "heads" or "tails" as output (Line 17).

In the main program, we create Scanner object and use it to prompt user to input an number for how many times to toss the coin (Line 6 - 7). Next, we use the input num to control how many times a for loop should run (Line 9). In each round of the loop, call the function <em>toss() </em>and print the output to terminal (Line 10).  

You might be interested in
You work in a furniture store. You receive a
spin [16.1K]

Answer:

18 pieces of furniture

Explanation:

Since you receive $120.93 per furniture piece and a the month's commission is $2,176.74 you divide the commission by the furniture price.

  • 2176.74/120.93
3 0
3 years ago
Read 2 more answers
A tool used to put a concave edge on a plane iron is
tigry1 [53]

Answer:

C) grinder

Explanation:

7 0
2 years ago
Read 2 more answers
Technician A says vehicles with electronic throttle control do not need a separate cruise control module, stepper motor, or cabl
8_murik_8 [283]

Answer: its A

Explanation:

3 0
3 years ago
What is the minimum number of crewmembers needed when using laser plane equipment?
Goshia [24]

Answer:

two

Explanation:

..

.

.

.

.

.

.

.

.

.

.

.

.

.

..

.

.

.

.

8 0
3 years ago
What process does a professional use to find an object’s position with respect to the camera?
shtirl [24]

Answer:

"D

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • A heat pump with an ideal compressor operates between 0.2 MPa and 1 MPa. Refrigerant R134a flows through the system at a rate of
    15·1 answer
  • A 179 ‑turn circular coil of radius 3.95 cm and negligible resistance is immersed in a uniform magnetic field that is perpendicu
    11·1 answer
  • Convert 250 lb·ft to N.m. Express your answer using three significant figures.
    6·1 answer
  • Plot the absorbance, A, versus the FeSCN2 concentration of the standard solutions (the values from your Pre-lab assignment). Fro
    7·1 answer
  • Because of ____________ people must make choices, and when they choose, they incur a(n)______________.
    10·1 answer
  • Select the correct answer
    8·1 answer
  • The convection heat transfer coefficient for a clothed person standing in moving air is expressed as h 5 14.8V0.69 for 0.15 , V
    6·2 answers
  • Introduction for site visit​
    13·1 answer
  • Technician A says power steering pumps can be engine driven. Technician B says power steering
    9·1 answer
  • saan nag tungo si Aguinaldo at ilang pinuno ng kilusan pagkatapos mapairal ang kasunduan na pansamantalang nag dulot ng kapayapa
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!