Example 1. Create a BankAccount class that has the usual bank account attributes.
//File: BankAccount.java
public class BankAccount
{
//Attributes
private double
balance;
//Overloaded Constructors
//NOTE: Constructors have the same name as the class and return no
values!!
public BankAccount()
{
balance = 0;
}
public BankAccount (double amount)
{
balance = amount;
}
//Behavior
//Accessors
public double getBalance ()
{
return balance;
}
//Mutators
public void deposit (double amount)
{
balance = balance + amount;
}
}
Again, this class must be compiled (>javac BankAccount.java) just like any other Java
source code. However, it cannot be run (>java BankAccount) since it has no main()
method. In order to use the BankAccount class (or type), we must write a program
(with a main()).
//File: BankAccountClient.java
import CSLib.*;
public class BankAccountClient
{
public static void main (String [] args)
{
OutputBox out = new
OutputBox("Testing BankAccount Class");
out.setSize (250, 100);
BankAccount myAccount = new
BankAccount(2000);
out.println ("My balance = " +
myAccount.getBalance());
myAccount.deposit(1000);
out.println ("My balance = " +
myAccount.getBalance());
BankAccount yourAccount = new
BankAccount();
out.println ("Your balance = " +
yourAccount.getBalance());
}
}

By adding a toString() method to our BankAccount class, we can avoid the necessity to make elaborate strings for output.
//File: BankAccount2.java
public class BankAccount2
{
.....................
public String toString()
{
return "This account has a balance of $ " + balance;
}......................
}
The driver program is now considerably simplified.
//File: BankAccount2Client.java
import CSLib.*;
public class BankAccount2Client
{
public static void main (String [] args)
{
OutputBox out = new
OutputBox("Testing BankAccount2 Class");
out.setSize (350, 100);
BankAccount2 myAccount = new
BankAccount2 (2000);
out.println ("" + myAccount);
myAccount.deposit(1000);
out.println ("" + myAccount);
BankAccount2 yourAccount = new
BankAccount2();
out.println ("" + yourAccount);
}
}
Note that the line
out.println ("" + myAccount);
is equivalent to
out.println (myAccount.toString());
When a String object is expected (as in out.println ("" + ...)), the compiler will look for a toString() method associated with the class of the object to be output. This proves to be such a convenience that I always try to add a toString() method to all of my classes.
By creating a DecimalFormat object, we're able to specify that the output should be limited to two digits to the right of the decimal point. If we add this feature to the BankAccount class, we can guarantee that the display of our account status will exhibit two digit precision.
//File: BankAccount3.java
import java.text.DecimalFormat;
public class BankAccount3
{
................
public String toString()
{
DecimalFormat twoDigits = new DecimalFormat("0.00");
return "This account has a balance of $ "
+ twoDigits.format(balance) ;
}.................
}
The output from the corresponding driver program (BankAccount3Client.java) is:
