CS 111 - Introduction to Computer Science
Homework #6

Due: Wednesday 10/6 at the beginning of class

1. Name That Method:  Written Exercise R2.18.  Additionally, what would be a good name for the method?  Hint: If this one has you stumped, add it to your BankAccount class definition and experiment with it using at least two BankAccount objects.

2. Find the Errors:  Written Exercise R5.1.  Correction: The second to last and third to last bullets should be combined.

3. Omitted Else: Written Exercise R5.9.  Create a table with each of the 4 cases possible with different values of x and y, and indicate the value of s at the end of the execution of these blocks of code.

x > 0 y > 0 s after code block 1 s after code block 2
false false    
false true    
true false    
true true    

4. Car:  Programming Exercise P2.10.  In addition, check for invalid parameters.  For instance, a negative or zero mileage makes no sense, so print an error message if the user attempts to create a car with a non-positive mileage.  Also, cars shouldn't be able to regain gas by driving backwards.  Print error messages for all invalid parameters.  Add a method getGas that returns the gallons of gas remaining.  Add a method getMiles that returns the total miles traveled.  If the user requests a car to drive so as to run out of gas, print the message "Out of gas" and have the car only travel as far as the gas would permit, leaving an empty gas tank.  Write a test program called CarTest.java that creates a car and demonstrates that each line of code functions properly.  That is, be sure to test each case such that all lines of code (including error messages) are exercised. Use double values throughout.

5. Optimal Piglet Advisor: Here is a description of a simple coin game called Piglet.  (Piglet is based on a simple dice game called Pig.):

The object of Piglet is to be the first player to reach n points. (In this case, we'll use n = 6.)  Each turn, a player repeatedly flips a coin until either a "tail" is flipped or the player holds.  At any time during the player's turn, the player is faced with two choices: flip or hold. If the coin turns up tails, the player scores nothing for the turn and it becomes the opponent's turn. Otherwise, the player's turn continues. If the player chooses to hold, the number of consecutively flipped heads is added to the player's score and it becomes the opponent's turn.

Below is a table that describe the optimal way to play Piglet when the goal score is 6.  The way you read the table is as follows:  The rows are numbered 0 to 5 downward.  The columns are numbered 0 to 5 rightward.  If your score is i and your opponent's score is j, then the number at row i, column j is the number of consecutive heads at which you should hold.  Otherwise, keep flipping.

Your task is to create an optimal Piglet advisor.  Call your program PigletAdvisor.java.  Use a Scanner to ask the user for (1) their score, and (2) their opponent's score.  If the score(s) are invalid, the output "Invalid scores".  Otherwise, the output should be "You should hold after # heads.", where # is given in the above table and "heads" is properly singular or plural depending on #.

Hint: There is no need to store a table (e.g. 2D array introduced later) if you notice that the table can be divided into cases: (1) either player has 5 points or the sum of the scores is 7 or more and you need to try to win this turn, (2) players each have exactly three points and you should hold at 1 head, (3) you're even or behind and should hold at 2, and (4) otherwise, you should hold at 1.  These cases should be handled in this order using an if-else chain to determine the hold value.