CS 111 - Introduction to Computer Science
Homework #3 |

**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:

- where is the wind chill index, based on the Fahrenheit scale, is the air temperature, measured in °F, and 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.)

`PigAdvisorTest.java`:

**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?79Opponent's score?42Turn total?21hold

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

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

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

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