![]() |
CS 111 - Introduction to Computer Science
Homework #5 |
1. Gas Mileage: Exercise 4.17. Call your class Mileage.java. Place your pseudocode in a block comment. Let a user's input of 0 miles driven serve as the sentinel value marking the end of input. Your test cases should be in plain text files named mileage1.dat, mileage2.dat, and mileage3.dat, such that entering a command of the form "java Mileage < testFileName" will execute your mileage computation with the given testFileName as input data. Note: the mileage computation over all tanks entered is the total miles divided by the total gallons. Use floating point division for all calculations.
2. Largest: Exercise 4.21. Call your class Largest.java. Place your pseudocode in a block comment. Don't forget that the largest value can be negative. Clarification: "Largest" means "greatest" in this context (i.e. >), rather than "largest magnitude".
3. Pig "Keep Pace and End Race" Advisor: Call your class PigAdvisor.java.
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