CS 111 - Introduction to Computer Science
Homework #3

Due at the beginning of class 9.

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

NOTE 2: Use printf only when printing lines with floating-point output where the number of decimal places is specified (i.e. exercise 3).

NOTE 3: Unless otherwise specified, we will use type int for integers and type double for floating-point numbers.

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 double-precision 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.

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

See text section 4.2.3.

Optional questions

Under what circumstances is output different for:

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).

Note 2: Use printf when printing lines with floating-point output that specified the number of decimal places (e.g. 2 decimal places means use format "%.2f").

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:

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:

Input Format:

Output Format:

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

Rubric: (20 points total)