Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Theoretically, it is known that a two-sided, equally weighted coin has a 50% cha

ID: 3552529 • Letter: T

Question

Theoretically, it is known that a two-sided, equally weighted coin has a 50% chance of landing on heads and a 50% chance of landing on tails. However, is this really what happens in practice? For example, if you took a penny and flipped it 100 times, would it always produce heads 50 times and tails 50 times? Instead of actually flipping a coin, you will be simulating coin flips to find out how often heads and tails appear. Make heads to be a constant assigned to 1 and tails to be a constant assigned to 2. (For fun, you can also flip an actual coin!)



Phase I - Flip a Coin.

Your program should implement the following methods with partial signatures:
/** getNum method will prompt the user for a whole number and return a positive integer > 0. **/ /** The message for prompting is given by msg. **/ /** In case of invalid input, it will display errorMsg. **/ /** Make sure you validate that the input entered is both a number and a positive integer. **/ /** If the user enter invalid input, your program should show an error message and keep asking. **/ /** (Your program should not stop if the input is invalid!) **/ /** Sample call: int coins = getNum(Enter the number of coins, Invalid, Not a positive integer.) **/ /** Result display if user entered -4: ** Enter the number of coins: -4 ** Invalid, Not a positive integer. ** Enter the number of coins: **/ getNum(String msg, String errorMsg)
/** flipCoin method will return a random flip of the coin returning 1 or 2. **/ /** Use (int) (Math.random() * 2) + 1 in this method to randomly generate 1 or 2. **/ flipCoin()
/** getFlipType method will return the string heads if sideID is 1 and tails if sideID is 2. **/ /** Sample call: String coinType = getFlipType(HEADS) **/ /** Result: heads is returned (but not displayed) **/ getFlipType(int sideID)
/** printNumCoins method will print the number of heads or tails. **/ /** Sample call: printNumCoins(HEADS, 5) **/ /** Result display: The coin landed on heads 5 times. **/ printNumCoins(int headsOrTails, int numHeadsOrTails)
Phase II. Flip a Coin Simulation Modify your code to ask the user the number of times they would like to flip the coin. (Validate the input to make sure it is a number and it is greater than 0.) Have your code automatically flip the coin that number of times and display the number of heads and number of tails flipped. (Do not display the actual flips in your final code, although this is helpful for testing your code.)



Phase III. Coin Flip as a Scientific Process

In order for a process to be scientific, it must be repeated at least 30 times! Modify your code to automatically repeat the simulation from Phase II thirty times (not input from the user) and display the average number of times heads is shown and tails is shown. For example, if the user asks for the coin to be flipped 4 times, then the coin would be flipped 4 * 30 = 120 times! The average number of heads would be the total number of heads across all repetitions / 30. Remember to make known values constants!


Deliverable I.

Write the specifications document (Refer to Lab #1-1). Based on the above description, write a properly-ordered algorithm that includes sections for known values, inputs, calculations, and outputs. Although the algorithm is due at the same time as the final project, you may bring it to your instructor or TA for approval ahead of time.


Deliverable II.

Write the program Follow the algorithm you wrote in Deliverable I to write a Java program. Be sure to follow your algorithm exactly. Every line of code should correspond to a step in your algorithm, and the order of the steps should be the same. This may require some back-and-forth adjustments to the algorithm as well as to the code.


Deliverable III.

Display the Output For Phases 2 and 3, run your program once for each phase. For each run, use the amounts shown below as the input for the number of coin flips. Copy and paste the results into a single Microsoft Word document called yourLastName_yourFirstName_cs1160_project1_info.doc. Label the output for Phase 2 Output for Phase 2. Do likewise for output from Phase 3.


Phase 2 (Run once): Show Output when User Input is: a) 0 (should ask for a new number) b) -2.5 (should ask for a new number) c) 50 (should show results of flipping a coin 50 times)


Phase 3 (Run once): Show Output when User Input is: d) two (without quotes - should ask for a new number) e) 1.0 (should ask for a new number) f) 100 (should show the average results of flipping a coin 100 times repeatedly 30 times)


Style requirements:

Use constants within calculations for known values. All inputs and all results of calculations must be stored in variables. Do NOT do any calculating within an output statement. All calculations must be completed before any results are displayed. Divide your code into sections that correspond with your algorithm. At a minimum, this would include an input section, a calculations section, and an output section. More than one calculations section may be necessary to clarify the various items being calculated. Include Javadocs and comments in plain English throughout your program.

Explanation / Answer

import java.util.InputMismatchException;

import java.util.Scanner;


public class Test

{

/** getNum method will prompt the user for a whole number and return a positive integer > 0. **/

public static int getNum(String msg, String errorMsg)

{

Scanner s=new Scanner(System.in);

System.out.print(msg);

int no;

while(true)

{

try

{

String input=s.next();

no= Integer.parseInt(input);

}

catch(Exception ex)

{

System.out.println(errorMsg);

System.out.print(msg);

continue;

}

if(no<=0)

{

System.out.println(errorMsg);

System.out.print(msg);

}

else

{

return no;

}

}

}


/** flipCoin method will return a random flip of the coin returning 1 or 2. **/

public static int flipCoin()

{

return (int) (Math.random() * 2) + 1;

}


/** getFlipType method will return the string