Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming Part 3 Bank Account Embezzling

Similar presentations


Presentation on theme: "Object-Oriented Programming Part 3 Bank Account Embezzling"— Presentation transcript:

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

2 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.

3 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.

4 Typecasts (cont'd) Bank Account Checking Savings Money Market
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”, , 0.02); BankAcct y1; y1 = x1; // implicit, widening typecast. Allowed. BankAcct x2; x2 = new SavAcct(“A1234”, ,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

5 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.

6 Polymorphism removes $50 from the $1000 with the $10 fee = $940
Exercise: Suppose we have the following: MMAcct x; x = new MMAcct(“A1234”, , 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

7 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.

8 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.

9 Polymorphism (example)
Exercise: Suppose we have the following code: public class Foo { public void method() { System.out.println(“Hi!”); } public class Goo extends Foo { 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) Object (uberObject) everything else
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

11 Polymorphism (cont'd) Exercise: Does the following require or not require an explicit typecast? Object x1; x1 = new MMAcct(“A1234”, , 0.01); MMAcct y1; y1 = x1; // narrowing. MUST typecast y1 = (MMAcct) x1; // cannot say Object IS-A MMAcct MMAcct x2; x2 = new MMAcct(“A1234”, , 0.01); Object y2; y2 = x2 // widening. MMAcct IS-A Object. uberObject can point to anything

12 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.

13 ArrayLists with Polymorphism
An ArrayList with no type is an ArrayList of Objects Exercise: Given the following code: public class Foo { public void talk() { System.out.println(“Hi!”); } public class Goo extends Foo { System.out.println(“Bye!”); public class Hoo extends Goo { System.out.println(“Hola!”); Foo Goo Hoo 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.talk(); } Bye Hola Hi

14 A short aside Why not just do: for (int i=0;i<a.size();i++){
Object temp = a.get(i); temp.talk(); } (Do only computer scientists make up such absurd examples?) 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.

15 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 }

16 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?

17 End of Lecture


Download ppt "Object-Oriented Programming Part 3 Bank Account Embezzling"

Similar presentations


Ads by Google