go to previous page   go to home page   go to next page

Answer:

  public String toString()
  {
     return "Account: " + accountNumber + ";\tOwner: " + accountHolder + ";\tBalance: " + balance ;
  }

Using the toString() Method

Since this method is a member of a CheckingAccount object, the object's data is accessed by using the variable name, like accountNumber. The dot operator is needed outside of the object, like account1.accountNumber in the main method.

Use the tabulation character "\t" to align the output better. When the toString() has been defined, the testing program can be more easily written:


class CheckingAccount
{
  . . . . (Now including the toString() method.)
}

class CheckingAccountTester
{
  public static void main( String[] args )
  {
    CheckingAccount account1 = new CheckingAccount( "123", "Bob", 100 );

    System.out.println( account1.toString() );
    account1.processDeposit( 2000 );
    account1.processCheck( 1500 );
    System.out.println( account1.toString() );

    
  }
}

The output of the program is now:

Account: 123;   Owner: Bob;     Balance: 100
Account: 123;   Owner: Bob;     Balance: 585

QUESTION 19:

With this nice new method in place, you must surely be eager to do further testing of the program. Add statements after the old statements in the test program that: