JMU
Stack Tracing
in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Getting Started
Using the Abstraction
Where You Have Seen Them
Using Them Yourself
An Example
javaexamples/oopbasics/banking/Account.java
/**
 * A simple "bank" Account
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class Account
{
    protected double balance;
    protected int    id;

    /**
     * Default constructor
     */
    public Account() 
    {

        balance = 0;
        id = -1;
    }

    /**
     * Explicit Value Constructor
     *
     * @param idNumber        The ID for the Account
     * @param initialDeposit  The initial deposit
     */
    public Account(int idNumber, double initialDeposit)
    {
        balance = 0;

        id = idNumber;
        if (initialDeposit > 0) balance = initialDeposit;
    }

    /**
     * Determine the maximum amount that can be withdrawn
     *
     * @return The size of the maximum possible withdrawal
     */
    public double amountAvailable()
    {
        return balance;
    }

    /**
     * Deposit money in this Account
     *
   * @param amount    The size of the deposit
   *
   * @return false if unable to deposit and true otherwise
   */
    public boolean deposit(double amount)
    {
        boolean ok;

        ok = false;
        if (amount >= 0) {

            balance += amount;
            ok = true;
        }

        return ok;
    }

    /**
     * Obtain the ID of this Account
     *
     * @return The ID of this Account
     */
    public int getID()
    {
        return id;
    }
  
    /**
     * Withdraw money from this Account
     *
     * @param amount   The size of the withdrawal
     *
     * @return false if unable to withdraw and true otherwise
     */
    public boolean withdraw(double amount)
    {
        boolean     ok;

        ok = false;
        if (amount >= 0) {

            // Check if available funds are sufficient
            // 
            // Note: amountAvailble() is overriden in the 
            // derived class OverdraftAccount.
            // Which version of amountAvailable() will be 
            // called?
            //
            if (amountAvailable() >= amount) {

                balance -= amount;
                ok = true;
            }
        }

        return ok;
    }
}
        
An Example (cont.)
javaexamples/oopbasics/banking/OverdraftAccount.java
/**
 * A "bank" Account that allows the owner to withdraw more money
 * than is in the account 
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class OverdraftAccount extends Account
{
    protected double   overdraftLimit;

    /**
     * Constructor
     *
     * @param idNumber        The ID for the Account
     * @param initialDeposit  The initial deposit
     * @param limit           The overdraft limit
     */
    public OverdraftAccount(int idNumber, 
                            double initialDeposit, double limit) 
    {
        balance = 0.0;
        
        id = idNumber;
        if (initialDeposit > 0) balance = initialDeposit;
        
        overdraftLimit = 0.0;
        if (limit > 0.0) overdraftLimit = limit;
    }

    /**
     * Determine the maximum amount that can be withdrawn
     *
     * Note: This method overrides amountAvailable() in Account.
     * This method is called by the method withdraw() which
     * is defined in Account.
     *
     * @return The size of the maximum possible withdrawal
     */
    public double amountAvailable() {

        return (balance+overdraftLimit);
    }
}
        
An Example (cont.)
    // Suppose we add the following to the amountAvailable() method
    // in OverdraftAccount

    StackTraceElement[] trace;
    trace = Thread.currentThread().getStackTrace();
    for (int i=0; i<trace.length; i++) {
        System.out.println(trace[i]);
    }

    // And then do the following in the driver
    OverdraftAccount  billSmith;
    billSmith = new OverdraftAccount(1001,15000.00,5000.00);
    billSmith.withdraw(19000.00);
  

The Stack Trace:

  OverdraftAccount.amountAvailable(OverdraftAccount.java:20)
  Account.withdraw(Account.java:59)
  Driver.main(Driver.java:36)