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

Laboratory 7: Working with simulation data as objects

Wednesday, October 21
Thursday, October 22

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 objectives of this laboratory are
  1. to give practice working with simulation data as objects, and
  2. to give further practice using decision and repetition structures.

1. Preparation

As usual, open a Terminal window and create a subdirectory named lab7 within your cpsc115 directory. You should do all your work in the directory lab7 today. Next, copy the following three .java files into the directory lab7. To do this, you will need to be on this Laboratory 7 webpage and follow the links provided below.
BetResult.java
SimulateBets.java
SimulateBetsApplication.java
You will first work on the file BetResult.java for the warm-up exercise and then the file SimulateBetsApplication.java to simulate Gambler's ruin. While it is required to run the Java application SimulateBetsApplication, the file SimulateBets.java has been completed (and should not be edited).

2. Warm-up exercise

As in Laboratories 3, 4 and 5, 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. The class SimulateBets represents simulation of gambler's ruin in which all results of simulation will now be stored in a list, where each entry is a pair of boolean and int values defined by

The class BetResult. This simple class represents a result of a single series of simulation consisting of two fields:
  1. won—whether the series resulted in a win or not.
  2. bets—the number of bets made for the series.
It also has three methods:
  1. BetResult()—a constructor to instantiate a BetResult object of a given boolean value, indicating whether won or not, and a number of bets.
  2. getWon()—an accessor for won.
  3. getBets()—an accessor for bets.
Now, open the file BetResult.java. In it, all but the two accessors are completed. You are to implement the two accessors according to the given specifications.

The class SimulateBets. As noted above, this class has already been completed. It consists of four fields:
  1. stake—gambler's initial stake.
  2. goal—gambler's goal.
  3. trials—the number of trials to perform.
  4. results—a list of BetResult objects to store all results.
Its methods are:
  1. SimulateBets()—a constructor to instantiate a SimulateBets object of a given stake, goal and a number of trials.
  2. getResults()—an accessor for results.
  3. simulate()—a method, given an integer i, that performs the ith trial of simulation and stores its result, whether won or not and the number of bets made, in the list results at index i; if no integer is given, then it invokes simulate(i) for i = 0, ..., trials − 1.

3. Analyzing the results

Now, open the file SimulateBetsApplication. In it, you will only see the main() method. As before, the Java application SimulateBetsApplication first reads three integers, for an initial stake, a goal and a number of trials, in the command line. It then performs simulation using the class SimulateBets but only in three lines:
SimulateBets simulation = new SimulateBets(stake, goal, trials);
simulation.simulate();
ArrayList<BetResult> results = simulation.getResults();
All results are now stored in the list named results, an ArrayList object. The first for loop then counts the total number of bets made and the total number of wins from the list. The second for loop finds the most number of bets made with its trial number (i.e., index in the list) and result, whether it resulted in a win. You are now to add code segments to determine and print, from the list results, Hints. To find the second most number of bets, perform the same search as that for the most number of bets while disregarding the entry for the most number of bets. For the third most number of bets, disregard the entries for the most and second most numbers of bets. To find the length of the longest winning streak, use an int variable, say, start, first initialized to −1, to mark the starting index of a winning streak and reset it to −1 whenever the streak ends. If a winning streak ends at an index i with a loss, then the length of that streak is simply i − start.

Your program should behave in the following way:
miyazaki@lab:~/cpsc115/lab7$ java SimulateBetsApplication 100 200 1000
It resulted in 501 wins out of 1000 trials.
The percent of games won:             50.10%
The average number of bets made:   10089.42
The most number of bets made:      49474 for trial #552 for a win.
The 2nd most number of bets made:  49310 for trial #168 for a loss.
The 3rd most number of bets made:  47088 for trial #285 for a win.
The longest winning streak:           11 wins
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 usual, document the source files BetResult.java and SimulateBetsApplication.java in the Javadoc format using comment blocks as shown in given examples: Each class should begin with a comment block. Each method should also begin with a comment block; if there are any, itemize parameters using @param for each parameter and list the return value using @return. 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 BetResult.java and SimulateBetsApplication.java to this Moodle site:
https://moodle.trincoll.edu/mod/assign/view.php?id=664499
If you have completed this laboratory early, stay and work on Homework 6 for the rest of the time.


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