Java Programming Exercise: Creating Objects and Methods with Coin and Bank Account Classes, Lab Reports of Computer Science

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

Pre 2010

Uploaded on 04/12/2010

koofers-user-pl4
koofers-user-pl4 🇺🇸

5

(2)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Part 1: Using a Class to Create Objects
In this exercise you will:
1. Learn about creating objects of classes and calling their methods.
2. Learn about creating methods in classes and utilizing them.
pf3

Partial preview of the text

Download Java Programming Exercise: Creating Objects and Methods with Coin and Bank Account Classes and more Lab Reports Computer Science in PDF only on Docsity!

Part 1: Using a Class to Create Objects

In this exercise you will:

  1. Learn about creating objects of classes and calling their methods.
  2. Learn about creating methods in classes and utilizing them.

The Coin Class

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.

Experiment 4.

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).