Chapter 4: Writing Classes 51
Prelab Exercises
1. Constructors are special methods included in class definitions.
a. What is a constructor used for?
b. How do constructors differ from other methods in a class?
3. Consider a class that represents a bank account.
a. Such a class might store information about the account balance, the name of the account holder, and an account
number. What instance variables would you declare to hold this information? Give a type and name for each.
b. There are a number of operations that would make sense for a bank account—withdraw money, deposit money,
check the balance, and so on. Write a method header with return type, name, and parameter list, for each such
ii. Deposit a given amount into the account. This changes the account balance, but does not return a value.
iii. Get the balance from the account. This does not change anything in the account; it simply returns the balance.
iv. Return a string containing the account information (name, account number, balance). This does not change
anything in the account.
v. Charge a $ 10 fee. This changes the account balance but does not return a value.