|
CPSC/DTSC 115L: Introduction to Computer Science
|
Fall 2026
|
Laboratory 3: Gambler's ruin
Wednesday, September 23
Thursday, September 24
As done last week, you are expected to work with an assigned partner as a
pair. Both you and your partner will receive the same grade. Both of you
should always save your laboratory work on your own accounts.
Objectives
The main objectives of this laboratory are
- to learn how to design, implement and use decision and repetition
structures,
- to learn how to read and use input values through the command line.
1. Preparation
To begin, open a Terminal window and create a subdirectory named
lab3 within your cpsc115 directory (if you forgot how to
create directories, review Laboratory 1). You will do
all your work in the directory lab3 today. Next, copy the following
two .java files into the directory lab3. To do this, you
will need to be on this Laboratory 3 webpage and follow the links provided
below.
echoPrint.java
Gambler.java
You will first use the file echoPrint.java for the warm-up exercise
and then the file Gambler.java to simulate Gambler's ruin
below.
2. Warm-up exercise
Implement a simple Java program to determine whether a given year is a
leap year. Recall that a year is a leap year if it is either
- divisible by four but not by 100 or
- divisible by 400.
For example, 2024 was the last leap year. While 2000 was a leap year, 1900
was not.
For this, your program should read an input value in the command line and
output the answer in the following way:
miyazaki@lab:~/cpsc115/lab3$ java isLeapYear 2026
The year 2026 is not a leap year.
Now, open the file echoPrint.java. In it, you will see this
statement:
int year = Integer.parseInt(args[0]);
This takes the first integer from the command line and stores it in the
int variable named year. Now, the Java application
echoPrint simply echo prints the year entered in the command line.
You are to modify this program, renaming it as isLeapYear in a file
named isLeapYear.java, to implement your solution. As before, first
write down on a piece of paper your I/O specification and
algorithm in pseudocode. After implementing and compiling it
successfully, run your program with five test cases, including 2026, 2024,
2000 and 1900. When completed, show your work to the instructor or a TA.
3. Gambler's ruin
Suppose that a gambler makes a series of fair $1 bets (where, for
example, a win or loss is determined by a coin toss), starting with
some given initial stake, say $100. The gambler always goes broke at some
point, but the gambler can walk away after reaching a goal, say, $200.
Now, open the file Gamble.java. In it, you will find a
while loop that simulates such a series of $1 bets, given an initial
stake and a goal, both in the command line. In this loop, a coin toss is
simulated by generating a random number between 0 and 1 and testing if it is
< 1/2 in the following way:
Math.random() < 0.5
The loop also counts the number of
bets made in an int variable named bets until the gambler
goes broke and lose or wins upon reaching the goal. For an initial stake of
$100 and a goal of $200, the Java application Gamble runs in the
following way:
miyazaki@lab:~/cpsc115/lab3$ java Gambler 100 200
You lost everything, $100, after making 10042 bets.
Because this is a series of random experiments, the same stake and goal
can give a different result:
miyazaki@lab:~/cpsc115/lab3$ java Gambler 100 200
You reached your goal, $200, and won after making 26096 bets.
Now, modify this program to repeat this simulation for a number of trials,
where the number of trials is specified as the third input value in the
command line (in addition to an initial stake and a goal). Rather than the
result of a single series of this simulation, your program should now output
- the number of wins,
- the percentage of wins (i.e., the number of wins divided by the number
of trials), and
- the average number of bets made per series, win or lose (i.e.,
the total number of bets made divided by the number of trials).
In particular, your program should behave in the following way:
miyazaki@lab:~/cpsc115/lab3$ java Gambler 50 250 100
It resulted in 20 wins out of 100 trials.
The percent of games won: 20.0%
The average number of bets: 11218.54
After implementing and compiling it successfully, run your program with five
test cases, including the above example. When completed, show your work to
the instructor or a TA.
Documentation
As done in Laboratory 1, document both source files
isLeapYear.java and Gambler.java in the Javadoc format
using a comment block as shown above. As joint work, list both your and your
partner's names.
What to hand in
For completion, having all your work checked by the instructor or a TA,
each partner should now submit individually both source files
isLeapYear.java and Gambler.java to this Moodle site:
https://moodle.trincoll.edu/mod/assign/view.php?id=659466
If you have completed this laboratory early, stay and work on
Homework 3 for the rest of the time.