CS 111 - Introduction to Computer Science
Homework #4 |

**1. Circle Math:** Implement a program `CircleMath.java`.
that prompts the user with "Radius? ", reads an integer radius, uses ```
Math.PI
```

in calculation of
double-precision floating point values for diameter, circumference, and area,
and uses ```
printf
```

for
printing these values in the exact format shown below (including leading
spaces). Note: You can calculate the square of `radius`

by
using either `radius * radius`

or ```
Math.pow(radius,
2)
```

.

Example Transcript (input underlined):

Radius?10diameter = 20.000000 circumference = 62.831853 area = 314.159265

Hint: To print a floating point value `x` to six decimal places alone on a
single line, one could use "`System.out.printf("%d\n", x);`".

**2. Rock, Paper, Scissors:** Call your program `
RockPaperScissors.java`. (At least a portion of this exercise should be
done together in class during week 3.)

**Input:**

- an integer 0, 1, or 2, representing a play of "rock", "paper", or "scissors", respectively.

**Output:**

- Before the input: a prompt "Please enter 0 (rock), 1 (paper), or 2 (scissors): "
- After the input: The computer randomly chooses an integer to represent it's play.
- Print "I played
<play>" where <play> is "rock"/"paper"/"scissors" depending on the computer's randomly chosen integer. - Print "You win."/"You lose."/"Draw." based on the following rules:
- Rock wins against scissors.
- Scissors wins against paper.
- Paper wins against rock.
- Players that choose the same play draw (i.e. tie).

Hint: At no point should you have an if-else chain for 9 cases. Each of the last two print statements should require at most 3 cases each. Let the user's play be p1 and the computer's play be p2. Consider what value you get if you compute (p1 - p2 + 3) % 3. How does the result of this computation map to the win/lose/draw cases? Why does this work? Why wouldn't the expression (p1 - p2) % 3 suffice for our three cases? (Answers to these hint questions are not to be submitted, but try to understand why these expressions are helpful or not.)

Example transcripts (input underlined):

```
Please enter 0 (rock), 1 (paper), or 2 (scissors): 0
I played paper.
You lose.
```

```
Please enter 0 (rock), 1 (paper), or 2 (scissors): 1
I played rock.
You win.
```

```
Please enter 0 (rock), 1 (paper), or 2 (scissors): 2
I played scissors.
Draw.
```

**3. Flip 5:** Create a program called `Flip5.java` that
implements "Flip 5", a simple gambling game of my design. The bettor
places a bet on "more tails" or "more heads", and then flips 5 coins. If the coins show
all heads or all tails, the house wins and the bettor loses their stake.
Otherwise, if the majority of coins match the bettor's bet (e.g. 3 or 4 tails out of
5 when the bettor bet "tails"), then the banker pays 1 for 1. Otherwise,
if the majority doesn't match the bettor's bet, then the bettor loses their
stake. The specification of the program is as follows:

Input:

- The user enters a 0 or 1 to bet on a majority of tails or heads, respectively.

Output:

- Initially, the user is prompted for their bet with "Bet (0) more tails, or (1) more heads: ".
- If the user enters an integer other than 0 or 1, the program prints "Invalid bet." and does nothing more.
- Otherwise, a random number generator simulates a coin flip and either "H" or "T" is printed for head or tail, respectively. This is repeated for a total of 5 coin flips.
- The program prints " --> ".
- If the count of heads flipped is 0 or 5, print "All flips are the same. You lose."
- Otherwise, if the count of heads flipped is such that the bettor's bet was correct, print "You win!"
- Otherwise, print "You lose."

NOTE:

- A newline character should be the last character printed by your program. No other newline characters should be printed, i.e. use System.out.print for all printing except that last print statement executed.
- When generating random coin flips, you should count either the number of heads flipped or tails flipped.
*Challenge: It is possible to write this program without the use any if-statements. Instead, one may use conditional expressions (a.k.a. if-else expressions, ternary operators, selection operators). You do not need to fulfill this challenge, but this is a good opportunity to practice your use of conditional expressions.*

Example transcripts (input underlined):

```
Bet (0) more tails, or (1) more heads: 0
TTTTT --> All flips are the same. You lose.
```

```
Bet (0) more tails, or (1) more heads: 1
HTHHT --> You win!
```

```
Bet (0) more tails, or (1) more heads: 0
HTHTH --> You lose.
```