CS 111 - Introduction to Computer Science Homework #3

Due: Friday 2/10 at the beginning of class

NOTE: Homework that does not compile or is not submitted according to instructions may receive no credit.

NOTE 2: Use printf when printing lines with floating-point output.

1. Division:  Create a program Division.java that has the user enter two integers using the Scanner class, which are then used to compute both the floating point and integer division results as follows (user input underlined):

Please enter integer a: 9
Please enter integer b: 4
Floating point division: a/b = 2.25
Integer division: a/b = 2 remainder 1

2. Cast, Round, Floor, Ceiling:  Create a program DoubleToInt.java that uses the Scanner method nextDouble to read a double value x from the user, and then report each of the following values.

• (int) x
• (int) Math.round(x)
• (int) Math.floor(x)
• (int) Math.ceil(x)

Example transcript (user input underlined):

Please enter a double value: -.9
(int) -0.9 == 0
(int) Math.round(-0.9) == -1
(int) Math.floor(-0.9) == -1
(int) Math.ceil(-0.9) == 0

Optional questions

Under what circumstances is output different for:

• integer truncation and rounding
• integer truncation and flooring
• integer truncation and ceiling
• rounding and flooring
• rounding and ceiling
• flooring and ceiling

How many different program inputs (i.e. runs) are necessary to demonstrate difference in output for all of these pairs?

3. Wind Chill: Create a program WindChill.java that uses the Scanner method nextDouble to read a double values t (Fahrenheit temperature) and v (wind speed in miles per hour) from the user, and then reports the wind chill temperature to 2 decimal places using the North American formula with U.S. customary units and using the output format shown in the transcript below:

$T_{\rm wc}=35.74+0.6215 T_{\rm a}-35.75 V^{+0.16}+0.4275 T_{\rm a} V^{+0.16}\,\!$
where $T_{\rm wc}\,\!$ is the wind chill index, based on the Fahrenheit scale, $T_{\rm a}\,\!$ is the air temperature, measured in °F, and $V\,\!$ is the wind speed, in mph.

Example transcript (user input underlined):

Degrees Fahrenheit? 5
Wind miles per hour? 15
The wind chill temperature is -12.99 degrees Fahrenheit.


Note: You can raise a number x to power p with the expression Math.pow(x, p).

4. Pig "Keep Pace and End Race" Advisor: Create a program PigAdvisor.java according to the following specification.

Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 ("pig") is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn, the player is faced with two decisions:

• roll - If the player rolls a
• 1: the player scores nothing and it becomes the opponent's turn.
• 2 - 6: the number is added to the player's turn total and the player's turn continues.
• hold - The turn total is added to the player's score and it becomes the opponent's turn.

Problem:  Given a player's score i, the opponent's score j, and the current turn total k, advise a player to "roll" or "hold" according to this "keep pace and end race" policy:

• If the player's score i plus the turn total k is greater than or equal to the goal score of 100, hold.
• Otherwise, if either player has a score greater than or equal to 71, roll.
• Otherwise, roll if and only if the turn total is less than 21 + round((j - i) / 8). (Use floating-point division before rounding.)

Input Format:

• integer player's score
• integer opponent's score
• integer turn total

Output Format:

• Prompt the user for each of the inputs above with "Player's score? ", "Opponent's score? ", and "Turn total? ", respectively.
• Given these inputs, output "roll" or "hold" according to the "keep pace and end race" policy above.

Sample Transcripts (input underlined):

Player's score? 79
Opponent's score? 42
Turn total? 21
hold


Player's score? 37
Opponent's score? 71
Turn total? 48
roll


Player's score? 71
Opponent's score? 0
Turn total? 20
roll


Player's score? 42
Opponent's score? 57
Turn total? 22
roll


Player's score? 42
Opponent's score? 57
Turn total? 23
hold