CS 111 - Introduction to Computer Science
Homework #4

Due at the beginning of class 12.

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? 10
     diameter = 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("%f\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:

Output:

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:

 Output:

NOTE:

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.

Rubric: (20 points total)