

SavingsAccount collegeFund = new SavingsAccount(10); BankAccount anAccount = collegeFund; Object anObject = collegeFund;
BankAccount anAccount = (BankAccount) anObject;
if (anObject instanceof BankAccount)
{
BankAccount anAccount = (BankAccount) anObject;
. . .
}
Why was the second parameter of the transfer method of type BankAccount?
public void transfer(double amount, BankAccount other)
{
withdraw(amount);
other.deposit(amount);
}
That's an error—it should have been of type
SavingsAccountObjectSavingsAccount and CheckingAccount
objectsBankAccount
Rectangle box = new Rectangle(5, 10, 20, 30); String s = box.toString(); // Sets s to "java.awt.Rectangle[x=5,y=10,width=20,height=30]"
"box=" + box; // Result: "box=java.awt.Rectangle[x=5,y=10,width=20,height=30]"
BankAccount momsSavings = new BankAccount(5000); String s = momsSavings.toString(); // Sets s to something like "BankAccount@d24606bf"
public String toString()
{
return "BankAccount[balance=" + balance + "]";
}
public class BankAccount
{
public String toString()
{
return getClass().getName() + "[balance=" + balance + "]";
}
}
public class SavingsAccount
{
public String toString()
{
return super.toString() + "[interestRate=" + interestRate + "]";
}
}
BankAccount acct = new SavingsAccount(0.5); System.out.println(acct);
What does it print?
BankAccount[balance=0.0]SavingsAccount[balance=0.0]SavingsAccount[balance=0.0, interestRate=0.5]SavingsAccount[balance=0.0][interestRate=0.5]if (coin1.equals(coin2)) . . . // Contents are the same
if (coin1 == coin2) . . . // Objects are the same
public class Coin
{
. . .
public boolean equals(Object otherObject)
{
Coin other = (Coin) otherObject;
return name.equals(other.name) && value == other.value;
}
}
public class CollectibleCoin extends Coin
{
. . .
public boolean equals(Object otherObject)
{
CollectibleCoin other = (CollectibleCoin) otherObject;
return getName().equals(other.getName()) && getValue == other.getValue()
&& year == other.getYear;
}
}
Coin c1 = new Coin("quarter", 0.25);
Coin c2 = new CollectibleCoin("quarter", 0.25, 1993);
What do c1.equals(c2) and c2.equals(c1) return?
truefalsetrue, the second one returns
falsetrue, the second one throws an
exceptionpublic class Coin
{
public boolean equals(Object otherObject)
{
if (getClass() != otherObject.getClass()) return false;
...
}
}
super.equals
public class CollectibleCoin extends Coin
{
public boolean equals(Object otherObject)
{
if (!super.equals(otherObject)) return false;
...
}
}
