CPSC/DTSC 115L: Introduction to Computer Science Fall 2026

Laboratory 4: More on gambler's ruin (graded)

Wednesday, September 30

This laboratory assignment will be graded in numerical points. For this laboratory, unlike the P/LP/F laboratories, you are on your own, without a partner, and no instructor or TA assistance will be provided. You may access all course related materials, such as lecture slides, Lysecky and Lizarraga and programs you have written for the course. However, no personal digital device nor access to external resources will be permitted. As usual, you must submit your solutions by the end of the laboratory period.

Objectives

The main objectives of this laboratory are
  1. to evaluate your ability to design and implement your own programs by following the three basic steps of program development, and
  2. to evaluate your ability to use decision and repetition structures in your programs.

1. Preparation

To begin, open a Terminal window and create a subdirectory named lab4 within your cpsc115 directory (if you forgot how to create directories, review Laboratory 1). You will do all your work in the directory lab4 today. Next, copy the following two .java files into the directory lab4. To do this, you will need to be on this Laboratory 4 webpage and follow the links provided below.
PrintAgeToday.java
GamblersRuin.java
You will first use the file PrintAgeToday.java for the warm-up exercise and then the file GamblersRuin.java to simulate Gambler's ruin below.

2. Warm-up exercise

Implement a simple Java program to print a person's age as of today, September 30, 2026, given one's date of birth. For this, your program should read a date of birth as three integers in the order of month, day and year in the command line and output the answer in the following format:
miyazaki@lab:~/cpsc115/lab4$ java PrintAgeToday 10 15 1986
You were born on 10/15/1986. As of today, 9/30/2026, you are 39 years old.
If today is the birthday, it should also print a happy birthday message:
miyazaki@lab:~/cpsc115/lab4$ java PrintAgeToday 9 30 2007
You were born on 9/30/2007. As of today, 9/30/2026, you are 19 years old.
Happy birthday!
To begin, open the file PrintAge.java. In it, you will see the statements:
int month = Integer.parseInt(args[0]);
int day = Integer.parseInt(args[1]);
int year = Integer.parseInt(args[2]);
These statements take three integers from the command line and stores them in the int variables month, day and year in the given order. The given Java application PrintAge simply echo prints the given date of birth. For this exercise, you are to add code segment to determine and print the age and, if applicable, the happy birthday message (for simplicity, you may assume that the user is at least 2 years old). As before, first write down on a piece of paper your I/O specification and algorithm in pseudocode (which must be submitted today). After implementing and compiling it successfully, run your program with five test cases, including the above examples. When completed, show your work to the instructor.

3. More on gambler's ruin

As in Laboratory 3, we suppose again 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 GamblersRuin.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 while loop also counts the number of bets made in the int variable bets until the gambler goes broke and lose or wins upon reaching the goal. The outer for loop repeats this simulation for a number of trials, where the number of trials is specified as the third input value in the command line. The given Java application outputs In particular, it behaves in the following way for a initial stake of $50 and a goal of $250 for 100 trials:
miyazaki@lab:~/cpsc115/lab4$ java GamblersRuin 50 250 100
It resulted in 22 wins out of 100 trials.
The percentage of wins: 22.0% 
The average number of bets, win or lose: 9924.2
For this exercise, you are to add code segment to this program so that it also finds and prints the maximum and minimum numbers of bets made for a series, win or lose, during the trials. For this, use the int variables max_bets and min_bets for the maximum and minimum, respectively, numbers of bets as defined and initialized as follows:
int max_bets = 0;
int min_bets = MAXINT;
(here, MAXINT refers to a predefined constant for a very large integer value you will not encouter in this problem). Your program should behave in the following way for a initial stake of $50 and a goal of $250 for 100 trials:
miyazaki@lab:~/cpsc115/lab4$ java GamblersRuin 50 250 100 
It resulted in 14 wins out of 100 trials.
The percentage of wins: 14.0%
The maximum number of bets, win or lose: 40518
The minimum number of bets, win or lose: 278
The average number of bets, win or lose: 8592.96
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.

Documentation

As usual, document both source files PrintAgeToday.java and GamblersRuin.java in the Javadoc format using comment blocks.

What to hand in

For completion, having all your work checked by the instructor, you should now submit both source files PrintAgeToday.java and GamblersRuin.java to this Moodle site:
https://moodle.trincoll.edu/mod/assign/view.php?id=660541
Also, submit your I/O specification and algorithm for the warm-up exercise in paper.


* CPSC/DTSC 115L home page
Valid HTML 4.01!