Write a method named coinFlip that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of whitespace-separated tokens. Assume that the input file data represents results of sets of coin flips. A coin flip is either the letter H or T, or the word Heads or Tails, in either upper or lower case, separated by at least one space. You should read the sequence of coin flips and output to the console the number of heads and the percentage of heads in that line, rounded to the nearest whole number. If this percentage is 50% or greater, you should print a "You win!" message; otherwise, print "You lose!". For example, consider the following input file: H T H H T Tails taIlS tAILs TailS heads HEAds hEadS For the input above, your method should produce the following output: 6 heads (50%) You win!

Answers

Answer 1

Answer:

Here is the JAVA program:

import java.io.*;

import java.util.*;

public class Main {

public static void main(String[] args) throws FileNotFoundException{ //the start of main() function body, it throws an exception that indicates a failed  attempt to open the file

Scanner input = new Scanner(new File("file.txt")); //creates a Scanner object and a File object to open and scan through the file.txt    

coinFlip(input);    } //calls coinFlip method

public static void coinFlip(Scanner input) { //coinFlip method that accepts as its parameter a string input holding a file name

while(input.hasNextLine()) { //iterates through the input file checking if there is another line in the input file

Scanner scan = new Scanner(input.nextLine()); //creates a Scanner object

int head = 0; // stores count of number of heads

int count = 0; //stores count of  total number of tokens

while(scan.hasNext()) { //iterates through the sequence checking if there is another sequence in the input file

String token= scan.next(); // checks and returns the next token

if (token.equalsIgnoreCase("H")||token.equalsIgnoreCase("Heads")) { //compares H or Heads with the tokens in file ignoring lower case and upper case differences

           head++;                } //if a token i.e. any form of heads in file matches with the H or Heads then add 1 to the number of heads

           count++;            } //increment to 1 to compute total number of counts

double result = Percentage(head, count); //calls Percentage method passing number of heads and total counts to compute the percentage of heads

System.out.println(head + " heads " + "(" + result +"%)"); // prints the number of heads

if(result >= 50.00) { //if the percentage is greater or equal to 50

System.out.println("You win!");} //displays this message if above if condition is true

else //if the percentage is less than 50

{System.out.println("You lose!");}  }    } //displays this message if above if condition is false

public static double Percentage(int h, int total) { //method to compute the percentage of heads

double p = (double)h/total* 100; // divide number of heads with the total count and multiply the result by 100 to compute percentage

return p;    } } //returns result

Explanation:

The program is well explained in the comments mentioned with each line of the above code. I will explain how the method coinFlip  works.

Method coinFlip accepts a string holding a file name as its parameter. It opens that file and reads its contents as a sequence of tokens. Then it reads and scans through each token and the if condition statement:

if (token.equalsIgnoreCase("H")||token.equalsIgnoreCase("Heads"))

checks if the each token in the sequence stored in the file is equal to the H or Heads regardless of the case of the token. For example if the first token in the sequence is H then this if condition evaluates to true. Then the head++ statement increments the count of head by 1. After scanning each token in the sequence the variable count is also increased to 1.

If the token of the sequence is HeAds then this if condition evaluates to true because the lower or upper case difference is ignored due to equalsIgnoreCase method. Each time a head is found in the sequence the variable head is incremented to 1.

However if the token in the sequence is Tails then this if condition evaluates to false. Then the value of head variable is not incremented to 1. Next the count variable is incremented to 1 because this variable value is always incremented to 1 each time a token is scanned because count returns the total number of tokens and head returns total number of heads in the tokens.

Percentage method is used to return the percentage of the number of heads in the sequence. It takes head and count as parameters (h and total). Computes the percentage by this formula h/total* 100. If the result of this is greater than or equal to 50 then the message  You win is displayed otherwise message You lose! is displayed in output.

Write A Method Named CoinFlip That Accepts As Its Parameter A String Holding A File Name, Opens That

Related Questions

Other Questions
I need helps will give you a good rating. Tim owns a clothing store where he designs pairs of shorts, s, and T-shirts, t.He sells the shorts for $12 and the T-shirts for $8 each. Tim can work 18hours a day, at most. It takes him 30 minutes to design a T-shirt and 45minutes to design a pair of shorts. He must design at least 12 items eachday, but he cannot design more than 30 items in one day. Which set ofinequalities below represents this scenario?A. s 2 12 + $ ss 30 + tiss 24-0.66t, s2; 0; t2 0B. s> 12-tss 30 - t Ss 24 -0.66t, s 2 0; t> 0O c. s? 12-ts? 30-tss 24 -0.66t, s 2 0;t20D. S 2 12-tss 30-ts 24 -0.66t, s 20; t 0 if you convinced your friend to lend you her notes to study for an exam, what would this situation be an example of? what cell structure helps the cell stay flexible and maintain homeostasis? How has your learning inside and outside of the classroom changed your perspective about experiences you have in school and life in general Please help!Much appreciated! What two factors determine how much potential energy an object has? A project has estimated annual net cash flows of $56,600. It is estimated to cost $339,600.Required:Determine the cash payback period. Evaluate. Write in standard form. Find the missing side or angle.Round to the nearest tenth. what is the location in the base interface of the link tocreate a table using wizard?pls Answer me. I'll make as BRIANLIST. Richards Parker is pis alter ego in a well constructed essay of 400 500 words explain how Richard Parker helps pi to survive his 227day ordeal On the lifeboat The sum of three consecutive multiples of 7 is 777 find these multiples If two of the ordered pairs was removed which two data points will cause the correlation to decrease the most? Select Two points1) Data point A2) Data point B3) Data point C4) Data point D Which detail from A Cub Pilot helps shape the central idea that if you believe in and trust yourself, no one should be able to shake your confidence? Twain says to Mr. Bixby the he can make the crossing with [his] eyes shut. Mr. Bixby asks, Didnt you know there was no bottom in that crossing? Twain tells Mr. Bixby that he couldnt hit the bottom of the river with a church steeple. Mr. Bixby tells Twain, I am going below awhile. You are hiking in a canyon and you notice an echo. You decide to let out a yell and notice it took 2 seconds before you heard the echo of your yell. How far away is the canyon wall that reflected your yell How does efficiency lead to conformity? The chief business officer of a construction equipment company arranges a loan of $9,300, at 12 1 /8 % interest for 37.5 months. Find the amount of interest. (Round to the nearest cent) a. $2,761.21 b. $3,583.83 c. $3,523.83 d. $3,722.47 An evergreen nursery usually sells a certain shrub after 9 years of growth and shaping. The growth rate during those 9 years is approximated by dh/dt = 1.8t + 3, where t is the time (in years) and h is the height (in centimeters). The seedlings are 10 centimeters tall when planted (t = 0). (a) Find the height after t years. h(t) = (b) How tall are the shrubs when they are sold? cm A verb that has a direct object is known as _____. 1) a predicate 2) a linking verb 3) an intransitive verb 4) a transitive verb