class CheckingAccount
{
// methods
int currentBalance()
{
return balance;
}
}
When the currentBalance()
method is added,
the program can be run and tested.
It is a good idea to alternate between writing and testing like this.
By testing each part as it is added you catch problems early on,
before they can cause further problems.
Of course, it helps to have a good initial design.
This is how houses are built, after all:
starting with a good design, the foundation is laid,
and structures are built and tested until the house is finished.
Here is a compilable and runnable test program:
class CheckingAccount { // instance variables String accountNumber; String accountHolder; int balance; //constructors CheckingAccount( String accNumber, String holder, int start ) { accountNumber = accNumber ; accountHolder = holder ; balance = start ; } // methods int currentBalance() { return balance; } } class CheckingAccountTester { public static void main( String[] args ) { CheckingAccount account1 = new CheckingAccount( "123", "Bob", 100 ); CheckingAccount account2 = new CheckingAccount( "92a-44-33", "Kathy Emerson", 0 ); System.out.println( account1.accountNumber + " " + account1.accountHolder + " " + account1.currentBalance() ); System.out.println( account2.accountNumber + " " + account2.accountHolder + " " + account2.currentBalance() ); } }
This test program creates two objects,
account1
and account2
of type CheckingAccount
.