Object-Oriented Programming Part 3 Bank Account Embezzling
17 Slides151.88 KB
Object-Oriented Programming Part 3 Bank Account Embezzling Objectives of this presentation: 1. Statement of Problem 2. Typecasts 3. Polymorphism and Late Binding 4. ArrayLists Hood College JETT Workshop Dept. of Computer Science February 26 2005
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.
Review: 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! 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 typecast SavAcct 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 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 (example) Exercise: Suppose we have the following code: What does the following print out? public class Foo { public void method() { System.out.println(“Hi!”); } Foo x; x new Hoo(); Foo } public class Goo extends Foo { public void method() { // ok, cause a Hoo IS-A x.method(); First draw the diagram Foo System.out.println(“Bye!”); } Goo } public class Hoo extends Goo { } Bye! Hoo
Polymorphism (cont'd) Every class in Java implicitly inherits from a class called Object. (when said aloud, often called“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 10
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
ArrayList Methods public 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 An ArrayList with no type is an ArrayList of Objects Exercise: Given the following code: What prints out: Foo Foo w new Goo(); Goo public class Foo { public void talk() { System.out.println(“Hi!”); } Goo x new Hoo(); Foo y new Hoo(); Hoo } Foo z new Foo(); public class Goo extends Foo { ArrayList a new ArrayList(); public void talk() { System.out.println(“Bye!”); } a.add(0, w); a.add(1, x); a.add(2, y); } a.add(3, z); public class Hoo extends Goo { for (int i 0;i a.size();i ){ public void talk() { Foo temp (Foo) a.get(i); System.out.println(“Hola!”); } } temp.talk(); } 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.talk(); } This will fail at compile time because the class Object does not have a method called talk. If you cast to Foo, all Foo and its subclasses DO have the talk method. (Do only computer scientists make up such absurd examples?)
ArrayLists (cont'd) Using this example as a guide, we can write the Embezzlement program. public class Embezzle{ public static void main(String[] args){ ArrayList BankAcct bank new ArrayList BankAcct (); int index 0; // fill bank with bank accounts int choice 1; while (choice ! 4) { // display menu of choices: 1 check, 2 saving, 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) // In order to embezzle 1 dollar from each account // loop through all the accounts // withdraw 1 dollar from the each account } Which would you use, a for loop or a for-each loop? Why?
End of Lecture