Using the display() 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:

  void display()
  {
    System.out.println(  accountNumber + "/t" + accountHolder + "/t" +  balance );
  }

(The string "/t" tells the compiler that you want the tab character.)

Using the display() 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.

Another nice thing to do is to use the tabulation character "/t" to align the output better. When the display() has been defined, the testing program can be more easily written:

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

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

    account1.display() ;
    account1.processDeposit( 2000 );
    account1.processCheck( 1500 );
    account1.display() ;

  }
}

QUESTION 19:

With this nice, new scaffolding 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:

  • Create a new object, account2
    • The account number is "007"
    • The account holder is Bond. James Bond.
    • The account amount is $500 (The end of the cold war has been hard on Mr. Bond.)
  • Display account2.
  • Deposit $700 in account2.
  • Process a $100 check.
  • Display account2.