toString() 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
{
  private String accountNumber;
  private String accountHolder;
  private int    balance;
  private int    useCount = 0;

  public CheckingAccount( String accNumber, String holder, int start ) { . . . . }
  private void incrementUse() { . . . . }
  public int getBalance() { . . . . }
  public void processDeposit( int amount ) { . . . . }
  public void processCheck( int amount ) { . . . . }

}

toString() Method

It would be nice to have a toString() method in this class that shows the use count as well as the other data. There is already a toString() method. All classes automatically have such a method. (This is done by inheritance, and subject of a upcoming chapter.) But the automatically supplied method may not do what you want.

If you write your own toString() method it will replace the automatically supplied one. The method must look like this:

public String toString()
{

}

The method must be declared to be public

class CheckingAccount
{
  private String accountNumber;
  private String accountHolder;
  private int    balance;
  private int    useCount = 0;
  . . . .

  public String toString()
  {
     return  "Account: " + accountNumber + "/tName: " + accountHolder + 
     
             "/tBalance: " +  balance + "/tUse Count: " +   ;
  }

}

QUESTION 12:

Modify the method so that it also prints out the use count.