|
CPSC/DTSC 115L: Introduction to Computer Science
|
Fall 2026
|
Laboratory 5: User-defined methods
Wednesday, October 7
Thursday, October 8
As done two weeks ago, 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 user-defined methods in your
programs, and
- to learn how to use the System.out.printf() method to create and
print formatted strings.
1. Preparation
As usual, open a Terminal window and create a subdirectory named
lab5 within your cpsc115 directory. You will do all your
work in the directory lab5 today. Next, copy the following
two .java files into the directory lab5. To do this, you
will need to be on this Laboratory 5 webpage and follow the links provided
below.
CtoFTable.java
SimulateGambling.java
You will first use the file CtoFTable.java for the first exercise
and then the file SimulateGambling.java to simulate
Gambler's ruin below.
2. Temperature conversion tables
Katherine was asked to design and implement a simple Java application to
generate a table that shows all temperatures between 0 °C and 100 °C,
with an increment of one degree in Celsius, in one column and the
equivalent temperatures in Fahrenheit, with an increment of 9/5 degrees in
Fahrenheit, in another column. For this, she wishes to define a user-defined
method named toFahrenheit() that takes a temperature in Celsius in
an int parameter and returns the equivalent temperature in
Fahrentheit as a double value. The following is her formal
specifation for such a method:
Given: a temperature in Celsius in an int parameter
Return: the equivalent temperature in Fahrentheit as a
double value
How should she perform this conversion? She recalls again that the freezing
temperature 0 °C is equal to 32 °F, and the boiling temperature
100 °C is equal to 212 °F. From this, she computes the ratio of
Fahrenheit to Celsius as follows: (212-32)/100 = 180/100 = 9/5. Then, using
f for Fahrenheit and c for Celsius, the conversion formula must
have the form f = 9/5 × c + k for some constant
k. Since she knows that f = 32 when c = 0, this means
that k must be equal to 32. In summary, she comes up with the
following algorithm in pseudocode:
- Take a temperature in Celsius in an int parameter c.
- Compute and return f = 9/5 × c + 32, the equivalent
temperature in Fahrentheit as a double value.
She translates her algorithm into Java code as follows:
public static double toFahrenheit(int c) {
return (9.0/5.0)*c + 32;
}
In the main() method, she then generates a temperature conversion
table by calling the method toFahrenheit() for every temperature
between 0 °C and 100 °C in the following way:
public static void main(String[] args) {
System.out.printf("Celsius\tFahrenheit%n");
for (int c = 0; c <=100; c++) {
System.out.printf("%4d\t", c);
System.out.printf("%7.2f%n", toFahrenheit(c));
}
}
Here, to align numbers neatly, we use the method printf() (cf. §9.2 in Lysecky and Lizarraga).
For example, in
System.out.printf("%4d\t", c);
we allocate 4-digit space for an int value for c with a
tab character indicated by \t. In the next statement
System.out.printf("%7.2f%n", toFahrenheit(c));
we allocate 7-digit space with 2 digits after the decimal point for a
double value for toFahrenheit(c) with a new line character
indicated by %n. See the source file
CtoFTable.java for a complete
implementation. Now, modify her solution to design and implement a Java
application to do the opposite: generate a table that shows all temperatures
between 0 °F and 100 °F, with an increment of one degree in
Fahrenheit, in one column and the equivalent temperatures in Celsius, with an
increment of 5/9 degrees in Celsius, in another column. For this, first write
down on a piece of paper your formal specification and
algorithm in pseudocode for a user-defined method named
toCelsius() to convert a temperature in Fahrenheit to the
equivalent temperature in Celsius. Then implement the toCelsius()
method as well as the main() method to generate a table in a new
Java class named FtoCTable in a file named FtoCTable.java.
After compiling it successfully, run your application to generate a table.
Is it correct? When completed, show your work to the instructor or a TA.
3. Yet another gambler’s ruin
As in Laboratories 3 and 4,
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
SimulateGambling.java. In it,
for each series of simulation, you will find a call to a user-defined method
named simulate() that takes a stake and a goal, both in
int parameters, simulates a series of such bets until the gambler
either wins or loses, and returns the number of bets made if it results in a
win, or zero if it results in a loss. As before, the Java applications reads
an initial stake, a goal and a number of trials in the command line, and the
for loop repeats the simulation for the given number of trials.
This particular application is slightly different from those given in the
previous laboratories in that it is now designed to 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 for a win (i.e., the total number of
bets made for all wins divided by the number of wins).
You are to comlete the method simulate(). For this, as before, you
should simulate a coin toss by generating a random number between 0 and 1
and testing if it is < 1/2 in the following way:
Math.random() < 0.5
Using a while loop, repeat such a coin toss until the gambler
either wins or loses. The loop should also count the number of bets made.
However, the method should return the number of bets only if the gamler
wins, and it should return zero otherwise. In other words, a positive
number of bets indicates a win, and zero indicates a loss. With the
method correctly implemented, the Java application should behave in the
following way:
miyazaki@lab:~/cpsc115/lab3$ java SimulateGambling 50 250 100
It resulted in 18 wins out of 100 trials.
The percent of games won: 18.00%
The average number of bets to win: 19249.22
As before, you should first begin with writing down on a piece of paper your
formal specification and algorithm in pseudocode for the method
simulate(). 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 both source files FtoCTable.java and
SimulateGambling.java in the Javadoc format using comment blocks as
shown in given examples. 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
FtoCTable.java and SimulateGambling.java to this Moodle
site:
https://moodle.trincoll.edu/mod/assign/view.php?id=661967