CS 140 - Lab 13 Instructions

Objectives

  1. More practice using Java's if-statement.
  2. More practice using Java's if-statement inside a for-loop.

Method

Part I - Gamblers Ruin by Doubling Down

In this lab we're going to modify the gambler's ruin program we developed in class.

A gambler might try to recoup losses quickly by doubling down on a loss. That is, when they lose, they double their previous bet and they keep doubling until they finally win. When they finally win they revert back to betting one dollar.

  1. Rather than always adding or subtracting one dollar when a gambler wins or loses create an apropriately typed variable named bet that is initialized to 1 dollar. Modify the program to use bet rather than the integer constant 1.
  2. Modify the gambler's ruin program so that when a gambler loses, their next bet is doubled. When they finally win their bet should revert back to one dollar.
  3. Run the program several times for a large number of plays (such as one million). What eventually happens with the gambler's final cash amount?
  4. Fix the problem of a gambler betting money they don't have. A gambler should only double their bet if they have the money to cover it. If they do not have the money to cover it they should only bet whatever cash they have left.
  5. What happens now when you run the simulation for a large number of plays? Does the gambler every get into a negative cash situation?

Extra Credit

Modify the gambler's ruin program so that when a gambler has either lost all their money or have reached the maximum number of plays they can gamble all over again with more cash. Have the user enter a number for the number of times they can play again.
© Ed Harcourt