Java Reference
In-Depth Information
public class BankAccount
{
private double balance;
private int number;
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
public int getNumber()
{
return number;
}
public double getBalance()
{
return balance;
}
public void deposit(double amount)
{
double prevBalance = balance;
balance = prevBalance + amount;
}
public void withdraw(double amount)
{
double prevBalance = balance;
balance = prevBalance - amount;
}
}
The BankAccount class seems simple enough, but I am obviously setting
you up here. Check out the following BankTeller class that makes a $100
deposit on a BankAccount object:
public class BankTeller extends Thread
{
private BankAccount account;
public BankTeller(BankAccount a)
{
account = a;
}
public void run()
{
System.out.println(this.getName() + “ depositing $100...”);
account.deposit(100.00);
}
}
Search WWH ::




Custom Search