ObjectOriented Programming Part 3 Bank Account Embezzling Objectives

Object-Oriented Programming Part 3 Bank Account Embezzling Objectives of this presentation: 1. Statement of Problem 2. Typecasts 3. Polymorphism and Late Binding 4. Array. Lists Hood College JETT Workshop Dept. of Computer Science February 26 2005

Statement of 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 Array. Lists.

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! 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. Sav. Acct x 1; x 1 = new Sav. Acct(“A 1234”, 500. 00, 0. 02); Bank. Acct y 1; y 1 = x 1; // implicit, widening typecast. Bank Account Checking Allowed. Bank. Acct x 2; x 2 = new Sav. Acct(“A 1234”, 500. 00, 0. 02); // widening typecast Sav. Acct y 2; y 2 = x 2; // implicit, narrowing typecast. Not allowed y 2 = (Sav. Acct) x 2; // explicit, narrowing typecast. Allowed. 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(“A 1234”, 1000. 00, 0. 01); Bank. Acct y; y = x; y. withdraw(50. 00); System. out. println(y. get. Balance()); 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: public class Foo { public void method() { System. out. println(“Hi!”); } What does the following print out? Foo x; x = new Hoo(); // ok, cause a Hoo IS-A Foo x. method(); } public class Goo extends Foo { public void method() { First draw the diagram Foo System. out. println(“Bye!”); } Goo } public class Hoo extends Goo { Hoo } Bye!

Polymorphism (cont'd) • Every class in Java implicitly inherits from a class called Object. (when said aloud, often called“uber. Object” so as to distinguish it from the computer science term “object”. ) Consequently, an Object reference can point to any Java object at all. Object (uber. Object) everything else 10

Polymorphism (cont'd) • Exercise: Does the following require or not require an explicit typecast? Object x 1; x 1 = new MMAcct(“A 1234”, 1000. 00, 0. 01); MMAcct y 1; y 1 = x 1; // narrowing. MUST typecast y 1 = (MMAcct) x 1; // cannot say Object IS-A MMAcct • Exercise: Does the following require or not require an explicit typecast? MMAcct x 2; x 2 = new MMAcct(“A 1234”, 1000. 00, 0. 01); Object y 2; y 2 = x 2 // widening. MMAcct IS-A Object. uber. Object can point to anything

Array. List Methods public class Array. List implements List { public int size(){ // returns size of Array. List } 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. } }

Array. Lists with Polymorphism An Array. List with no type is an Array. List of Objects Exercise: Given the following code: public class Foo { public void talk() { System. out. println(“Hi!”); } What prints out: Foo w = new Goo(); Goo x = new Hoo(); Foo y = new Hoo(); Hoo } Foo z = new Foo(); public class Goo extends Foo { Array. List a = new Array. List(); 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 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? )

Array. Lists (cont'd) Using this example as a guide, we can write the Embezzlement program. public class Embezzle{ public static void main(String[] args){ Array. List<Bank. Acct> bank = new Array. List<Bank. Acct>(); 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 } }

Array. Lists (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
- Slides: 17