Object-Oriented Programming Part 3 Bank Account Embezzling Hood College JETT Workshop Dept. of Computer Science February 26 2005 Objectives of this presentation:

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
Polymorphism What is Polymorphism? Taking Advantage of Polymorphism
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
C++ fundamentals.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Arrays And ArrayLists - S. Kelly-Bootle
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Programming Languages and Paradigms Object-Oriented Programming.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Polymorphism & Interfaces
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Topic 4 Inheritance.
Programming in Java CSCI-2220 Object Oriented Programming.
ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
M1G Introduction to Programming 2 5. Completing the program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Building Java Programs Interfaces reading: , 16.4.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Extending Classes.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Review of Previous Lesson
Review: libraries and packages
Object-Oriented Programming Part 3 Bank Account Embezzling
Web Design & Development Lecture 6
Presentation transcript:

Object-Oriented Programming Part 3 Bank Account Embezzling Hood College JETT Workshop Dept. of Computer Science February 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”, , 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 CheckingSavings 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”, , 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”, , 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”, , 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 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 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 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