public class BankAccount {
public BankAccount () { this(0.0) ; }
public BankAccount( double initBal )
{ balance_=initBal; id_=newID(); }
public double balance() { return balance_; }
public int id() { return id_; }
public void withdraw(double amt) { balance_ -=amt; }
public void deposit(double amt) { balance_ +=amt; }
public String toString()
{ return super.toString() + "(id:" + id_ + ", bal:" + balance_ + ")" ; }
// Instance variables
private double balance_;
private int id_;
// Class variable and class method
public static int nextID_=100;
private static int newID() { return nextID_++; }
// Another "special" class method
public static void main(String args[]) {
BankAccount a=new BankAccount(15.25);
BankAccount b=new BankAccount();
System.out.println("a=" + a.toString() );
System.out.println("b=" + b.toString() );
a.withdraw(5.50);
b.deposit(125.99);
System.out.println("a=" + a);
System.out.println("b=" + b);
System.exit(0);
} // no semi-colon
}
Detailed Description:
"Once we've got our two objects created, we want to print them out. And so,
there is a construct in the system, to go to the System class, get the out
variable, which is a PrintStream and print information into the stream. So notice
the construct. We have System.out.println(...). System is a class, out is a
class variable that's a PrintStream. And println is a method on that class that
writes out a String to the console. The string is the arguments passed to println,
"a=" followed by a plus sign (+) that does concatenation to a.toString(). The
result is it's going to say "a=" and whatever information BankAccount's toString
does. Its job is to print out what the super.toString() creates and then print out
the id and the balance. The next one does the same except it works with
BankAccount 'b'."