Testing the Method

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

class CheckingAccount
{

  // methods
  int getBalance()
  {
     return balance;
  }

}

Testing the Method

When the getBalance() 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. You start with a good design, then lay the foundation. Then structures are built and tested until the house is complete.

Here is a compilable and runable 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 getBalance()
  {
     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.getBalance() );
    System.out.println( account2.accountNumber + " " +
        account2.accountHolder + " " + account2.getBalance() );
  }
}

This test program creates two objects, account1 and account2 of type CheckingAccount.

QUESTION 13:

What will the program output when run? (Sketch out the answer, don't worry about spaces.)