

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
In this java programming exercise, you will learn about creating objects of classes and calling their methods by using the examples of a coin class and a bank account class. The coin class represents the properties and behavior of a coin with methods for flipping and getting the face. The bank account class represents a bank account with methods for printing a summary, charging a fee, and changing the name. The goal is to simulate flipping a coin 100 times and determine the longest run of heads and complete the bank account class as described.
Typology: Lab Reports
1 / 3
This page cannot be seen from the preview
Don't miss anything!


In this exercise you will:
As its name suggests, the Coin class (Coin.java) represents properties and behavior of a coin. This class contains a method (flip) that will simulate the flipping of the coin. It also contains a method (getFace) that will return the “face” of the coin, either heads or tails.
In general, methods in a class will start with a header. The form of a method header is
optionalAccessModifiers returnType methodName ( optionalParameterList )
The flip method has the header
public void flip()
The methodName is flip. The returnType is void, meaning that this method does not return any value after it is executed. This method has no parameterList , meaning that it does not need any additional information. The term public is an accessModifier that tells us that this method is available for the public to use.
The getFace method has the header
public void getFace()
The only differences in the headers of flip and getFace are the values for methodName and returnType. While the method names flip and getFace are obviously different, the returnType of the methods is more interesting. While the flip method does not return any value, the getFace method returns an int representing the current face of the coin. Another method in the Coin class called toString returns the face of the coin as a String, either the string “Heads” or “Tails.”
To be able to use this class in a program, we need to create an object of type Coin. Once this object is created, it will always have a state, in this case its face, either heads or tails. The flip method can then be called on this object and the state of the object will be updated.
In the following experiment, we want to simulate flipping the coin 100 times and then determine the longest run of heads. For example, if the result of flipping the coin ten times was
Heads, Tails, Heads , Heads , Heads , Tails, Heads, Tails, Heads, Heads
then the longest run of heads is 3 (this run is in bold).