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: 3552532 • 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(

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