Name | Type | |
---|---|---|
Account number | accountNumber | String |
Name of account holder | accountHolder | String |
Current balance | balance | int |
You might have thought of equally valid names.
The account number should be a String
reference
because it is not
expected to take part in arithmetic operations.
Sometimes account numbers contain dashes or other non-digit characters.
The balance is kept in terms of cents, so should be an int
.
To enforce good modularity, instance variables should be private
.
So far, the CheckingAccount class looks like this:
public class CheckingAccount { // instance variables private String accountNumber; private String accountHolder; private int balance; constructors methods }
Next, consider the constructor. The constructor has the same name as the class, and looks like this:
CheckingAccount( parameter-list ) { initialize the data }
The constructor is used with the
new
operator to
create a new
checking account object.
It then
initializes the account number, the
account holder's name, and starting balance.
Actual parameters are supplied to the constructor
when it is used, such as in the following:
CheckingAccount billsAccount = new CheckingAccount( "123", "Bob", 100 ) ;
This statement creates a new CheckingAccount
object by calling the
constructor.
The constructor will initialize the object's accountNumber
to "123",
its accountHolder
to "Bob", and its balance
to
100.
Fill in the parameter list for the constructor. You will have to think of names for the parameters, and will have to include the type of each parameter.