Lecture 15 – March 13,
2007
First example of extending a class
GradedActivity2.java
FinalExam2.java
Let’s look at the copy constructor example in the text
(found on page 569)
- What
we are interested in, in this example,
is just the mechanism for creating a copy constructor.
Stock.java
CopyConDemo.java
Now, let me point out that the copy constructor example in
the book is not a good one because two companies can’t have the same trading
symbol.
Note that, copy constructors are very useful when you want
to have different (separate objects) with the same attributes.
- An
example might be a poker hand where
each player
has an initially empty array of 5 cards
each player
has an initially empty string representing the type of hand
- Another
example might be a hash table where you want to create a copy of the table
to play with not the real table.
___________________________________________________
We have said that we want all declarations of variables at
the top of methods.
- When
you try to do this in a constructor you get an error message if the
constructor calls its super class constructor.
- This
is because you’re trying to muck with the object before it has been
created.
- This is the one place where it is not
only OK but required that you declare the variable AFTER other code in a
method.
___________________________________________________
What about the over ridden method getAccountNumber?
___________________________________________________
Professor Harris is helping students with major applications. Please be sure to see her as soon as
possible.
There is an ACM sponsored event tomorrow evening Consulting 101 with food. You do not have to be an ACM member to
attend. You should have gotten an e-mail
message about it and if you deleted it, the time and place are posted on the
bulletin board outside my office.
Below are the two methods you were asked to write for
Lab 15 which we discussed in class
/**
@author Elizabeth Adams
March 13, 2007 - for Lab 15 from
Gaddis
Note that the commented out statements
were discussed in class/
Some of them show alternate ways of
coding a method.
Others show things that didn't quite
match specifications
*/
public class
CheckingAccount extends BankAccount
{
//
public final double FEE = .15;
private static final double FEE = 0.15 ;
public CheckingAccount(String name , double initAmount )
{
// String temp;
super(name, initAmount);
String temp;
temp = getAccountNumber();
temp = temp + "-10";
setAccountNumber(temp);
//
setAccountNumber ( getAccountNumber() + "-10");
}// end constructor
// super class withdraw header shown - method was to be over
ridden
// public boolean withdraw(double amount)
public boolean withdraw (double amount)
{
boolean result;
amount = amount + FEE;
result = super.withdraw(amount);
return result;
// return (super.withdraw(amount));
}// end withdraw
}// end class
|
/**
@author Elizabeth Adams
March 13, 2007
Written for Lab15 based on
instructions in Gaddis
Commented statements were discussed
in class
This version doesn't use the required
over ridden method
getAccountNumber()
*/
public class
SavingsAccount extends BankAccount
{
private double rate; //= 0.025;
private int
savingsNumber; // = 0;
private String
accountNumber;
public SavingsAccount (String name, double initBalance)
{
super (name, initBalance);
String temp; // has to be declared AFTER call to
super
rate = 0.025;
savingsNumber = 0;
temp = super.getAccountNumber();
accountNumber = temp + "-" +
savingsNumber;
}// end constructor
// copy constructor described on
page 569 of text_
public SavingsAccount (SavingsAccount
oldSavingsAccountObject,
double initBalance)
{
super(oldSavingsAccountObject,
initBalance);
String temp;
savingsNumber =
oldSavingsAccountObject.savingsNumber + 1;
temp = super.getAccountNumber();
accountNumber = temp + "-" +
savingsNumber;
}// end copyconstructor
public void postInterest
()
{
// calculates one month's
worth of interest on the balance
_// and deposits it into the
account
double tempBalance;
tempBalance = getBalance();
tempBalance = tempBalance +
tempBalance * (1.0/12.0) * rate;
setBalance(tempBalance);
}// end postInterest
/**accessor method to account number
@return the account number*/
public String getAccountNumber()
{
return
accountNumber;
} // end overRidden
getAccountNumber;
}// end class
|
Here are links to the other code discussed in class. They
point to Word files.
GradedActivity2.java
FinalExam2.java
Stock.java
CopyConDemo.java