Upload
abel-bradford
View
228
Download
0
Embed Size (px)
Citation preview
Object-Oriented ProgrammingPart 3
Bank Account Embezzling
Hood College JETT WorkshopDept. of Computer Science
February 26 2005
Objectives of this presentation:
1. Statement of Problem
2. Typecasts
3. Polymorphism and Late Binding
4. ArrayLists
5. equals() and Comparable interface
Statement of Problem
Problem: Write a program that will embezzle 1 dollar from each bank account at Hood National Bank.
The key ideas we will need to solve this problem are polymorphism and ArrayLists.
Typecasts
• Typecast: the conversion of a value from one data type to another.
– Widening typecast: a conversion that is “safe” because there is no loss in information: e.g. casting from int to double. Java allows implicit widening typecasts.
int x = 4; double y;
y = x;
– Narrowing typecast: a conversion that is “unsafe” because information is potentially lost: e.g. casting from double to int. Java does not allow implicit narrowing typecasts. Rather, such typecasts must be explicit.
double y = 4.5; int x;
x = y; // won't compile!
x = (int) y; // ok, will compile.
prevents you from shooting yourself in the foot.
Typecasts (cont'd)
• More generally, implicit typecasts are allowed when the data type being converted from “is-a” subset of the type being converted into.
• Otherwise, an explicit typecast is required; e.g.
SavAcct x1; x1 = new SavAcct(“A1234”, 500.00, 0.02);BankAcct y1;y1 = x1;
// implicit, widening typecast. Allowed.
BankAcct x2;x2 = new SavAcct(“A1234”, 500.00,0.02); // widening
typecastSavAcct y2;y2 = x2;
// implicit, narrowing typecast. Not allowed
y2 = (SavAcct) x2; // explicit, narrowing typecast. Allowed.
Bank Account
Checking Savings
Money Market
In order to embezzle
• It is inefficient to withdraw money asking:– “if it’s a checking account withdraw money this
way, if it’s a Money Market account … etc.”
• It doesn’t make sense to have one class with many withdraw methods based on the type of account because:– What if new kinds of accounts are created by
the bank? code will need to be rewritten.– It would be nice if your code will work for any
kind of bank account automatically.
Polymorphism
Exercise: Suppose we have the following:
MMAcct x;x = new MMAcct(“A1234”, 1000.00, 0.01);BankAcct y;y = x;y.withdraw(50.00);System.out.println(y.getBalance());
What gets printed?removes $50 from the $1000 with the $10 fee = $940
You always choose which method to use based on type of actual object not on type of reference
Polymorphism (cont'd)
Late binding: When invoking a method, the version of the method that is called depends on the type of the object, not on the type of the reference to the object.
Called late binding because the type is not determined until the program is actually run.
Polymorphism: There can be many forms of the same method.
poly = many
morph = shape
There are many versions of the same method, and which one is called depends on what type of object you have when program runs.
Most important OOP concepts = A PIE
• Abstraction = only know what you need to know. Ignore unnecessary details.
• Polymorphism = same method works different ways for many classes
• Inheritance = subclasses inherit methods from superclasses
• Encapsulation = each class takes care of its own data.
Polymorphism (cont'd)
Exercise: Suppose we have the following code:
public class Foo {
public void method() {
System.out.println(“Hi!”);
}
}
public class Goo extends Foo {
public void method() {
System.out.println(“Bye!”);
}
}
public class Hoo extends Goo {
}
What does the following print out?
Foo x;
x = new Hoo();
// ok, cause a Hoo IS-A Foo
x.method();
First draw the diagram
Bye!
Foo
Goo
Hoo
10
Polymorphism (cont'd)
• Every class in Java implicitly inherits from a class called Object. (when said aloud, I'll refer to the class as “uberObject” so as to distinguish it from the computer science term “object”.)
Consequently, an Object reference can point to any Java object at all.
Object (uberObject)
everything else
Polymorphism (cont'd)
• Exercise: Does the following require or not require an explicit typecast?Object x1;
x1 = new MMAcct(“A1234”, 1000.00, 0.01);
MMAcct y1;
y1 = x1;
// narrowing. MUST typecast y1 = (MMAcct) x1;
// cannot say Object IS-A MMAcct
• Exercise: Does the following require or not require an explicit typecast?MMAcct x2;
x2 = new MMAcct(“A1234”, 1000.00, 0.01);
Object y2;
y2 = x2
// widening. MMAcct IS-A Object. uberObject can point to anything
ArrayLists
• An ArrayList is similar to an array in that elements can be easily referenced by an index.
• The major differences are:– the size of an array, once set, can never be
modified during the execution of the program. The size of an ArrayList, however, can be modified at any time, expanding its size whenever necessary.
– The data type of the elements of an array can be specified in the program. The elements of an ArrayList, however, are always Object references.
Simplified: an ArrayList is like an Array that can automatically resize and hold different kinds of objects.
ArrayList Methodspublic class ArrayList implements List {
public int size(){
// returns size of ArrayList
}
Object get(int index) {
// returns elem at position index
}
Object set(int index, Object x) {
// replaces the elem at index with x, and returns elem formerly at position
}
void add(int index, Object x) {
// insert x at position index, sliding over elem if necessary
}
void add(Object x) {
// insert x at end of lsit
}
Object remove(int index) {
// remove elem at position index, sliding over elem if necessary.
// Returns elem formerly at specified position.
}
}
ArrayLists with Polymorphism
Exercise: Given the following code:
public class Foo {
public void method() {
System.out.println(“Hi!”);
}
}
public class Goo extends Foo {
public void method() {
System.out.println(“Bye!”);
}
}
public class Hoo extends Goo {
public void method() {
System.out.println(“Hola!”);
}
}
What prints out:
Foo w = new Goo();
Goo x = new Hoo();
Foo y = new Hoo();
Foo z = new Foo();
ArrayList a = new ArrayList();
a.add(0, w);
a.add(1, x);
a.add(2, y);
a.add(3, z);
for (int i=0;i<a.size();i++){
Foo temp = (Foo) a.get(i);
temp.method();
}
Foo
Goo
Hoo
Bye
Hola
Hola
Hi
A short aside
• Why not just do:for (int i=0;i<a.size();i++){ Object temp = a.get(i); temp.method();}This will fail at compile time because the class Object does not have a method called method. If you cast to Foo, all Foo and its subclasses have the method method.
(Do only computer scientists make up such absurd examples?)
ArrayLists (cont'd)
Using this last example as a guide, we can write the Embezzlement program. You'll complete the program in a hands-on exercise.
ArrayList bank = new ArrayList();
int index = 0;
// fill bank with bank accounts
int choice = 1;
while (choice != 4) {
// display menu of choices: 1=check, 2=sav, 3=money market, 4=quit
// prompt user for menu choice
if (choice == 1) {
// prompt user for a checking account and add to bank
else if (choice == 2) {
// prompt user for savings account and add to bank
else if (choice == 3) {
// prompt user for money market account and add to bank
}
}
ArrayLists (cont'd)
// embezzle 1 dollar from each account
for (int I = 0; i < bank.size(); i++) {
// withdraw 1 dollar from the ith account
}
End of Lecture