Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to...

22
Chapter 1: Object Oriented Paradigm

Transcript of Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to...

Page 1: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

Chapter 1: Object Oriented Paradigm

Page 2: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

1.1 Data Abstraction and Encapsulation

• OOP allows programmer to– separate the details that are important to the user• myCoin.flip(); myCoin.getValue();

– from the details that make the abstraction work• if (Math.random() > 0.5) side = "heads"; else side="tails";

• Accomplished by organizing code into – Interface: details important to user– Implementation: “unimportant” details hidden from user

• Who is "user?" – person who writes the application…and/or…YOU !

Page 3: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

Strings represented on Macs vs Linux

Page 4: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

1.2 The Object Model

• A convenient design method– program data managed by objects– objects manage internal data, determines state• point object manages its x,y coordinates• coin object manages its side indicator• student record object manages it's name, ID, GPA

– this helps manage chaos as complexity grows• reduces unexpected linkages between parts of program

Page 5: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

Primary focus of class

• how we implement and evaluate objects with methods that are logically complex

• how we can use the objects we create• objects mainly will be data structures, – our primary interest!

• occasionally we will develop control structures that manipulate other objects

Page 6: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

1.3 Object-Oriented Terminology

• Applied to a Ratio class – see Demo1 in mod1 download– encapsulation– object– instance– class– fields– methods– constructor– utility methods (reduce, gcd)– static methods (gcd)

Page 7: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

1.4 A Special-Purpose Class: A Bank Account

• Manages data: – balance and account number (or account name)

• Performs: – getAccount– getBalance– deposit– withdraw

Page 8: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

BankAccount equals() method

public boolean equals(Object other)// pre: other is a valid bank account// post: returns true if this bank account is the same

as other{

BankAccount that = (BankAccount)other;// two accounts are the same if account numbers are the

samereturn this.account.equals(that.account);

}

Page 9: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

Class Object

• Every class is a descendant of Object• Defines fundamental methods– equals() same data as another object– toString() string representation for display– clone() makes an identical copy

• If you don't define your own versions of these, automatic (usually defective) ones will be provided

Page 10: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

10

Memory Model of BankAccount object

Memory allocated by the declarationBankAccount jane;jane = new BankAccount(“J. Doe”, 345.67);

BankAccount jane;

jane = new BankAccount(“J. Doe”, 345.67);

null 100

J. Doe

345.67

account

balance

jane The number 100 indicates the memory location where the object resides

Page 11: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

11

Shallow vs Deep Copy

• There are two ways to “copy” an object– Deep copy creates a clone of the object

• The objects data values are copied into the clone

– Shallow copy creates reference to the object• The location of the object is copied into a reference variable

• After a– Deep copy, two distinct objects exist

• Can change one object’s data without changing the other’s

– Shallow copy two references to one object exists

Page 12: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

12

Code of Shallow and Deep Copies

• Shallow copy is similar to copying primitives jon = jane;

• Deep copy– requires a class method (e.g., in the BankAccount class) public BankAccount clone()

{ BankAccount copy = new BankAccount();

copy.account = account; copy.balance = balance; return copy;}

– Invoked asjon = jane.clone(); //copy jane into

jon

Page 13: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

13

Memory Model of Two Types of Copies100 200

janejon

location 200location 100

jon j. doe

187.95 345.67 345.67

j. doedeep copy

A Deep Copy of the Object jane into the Object jon

account

balanceaccount

balance

100 200 200janejon

location 200

345.67

shallow copy

A Shallow Copy of the Object jane into the Object jon

j. doe

Page 14: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

Using Scanner and File

• Scanner – a class representing objects that read input streams or files and extracts primitive data types from them

• File – a class representing a path to a location on disk or other media

• We will now apply these to our BankAccount app

Page 15: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

1.5 A General-Purpose Class: An Association

• very general class represents a connection between two kinds of objectsprotected Object theKey; // the key of the key-value pairprotected Object theValue; // the value of the key-value pair

• Association can be used to link– ID (key) with an employee record– Two words from different languages (pig latin)– Methods: setValue, setKey, getValue, getKey

Page 16: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

Protected vs Private

• Usually, it is best to restrict methods as much as possible. – private: member is only used within class itself– protected: member is available to other classes in

a package or to subclasses through inheritance– public: member is available to any user

Page 17: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

An app that uses Association

• atinLay – a Pig Latin translator• This program uses the argument list set up in

main, as would be obtained from a “command line” execution

• We (or you!) could convert it to read from the System.in console with a Scanner object

Page 18: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

1.6 Sketching an Example: A Word List

• Suppose we want to develop a Hangman app– The program should :

1. Select a random word from a list of words (TODO!)2. Receive user guesses and draw stick figure (demo)

• We propose to develop a WordList class to help us with item number 1)– BTW this is a “Data Structure”

• How do we design such a thing????

Page 19: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

5 Step Data

Structure Design Process

Page 20: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

Sketch an example test applicationhelps familiarize operations needed

Page 21: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

Sketch the “interface” for the classnames of methods, parameters, return types

Page 22: Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

Develop the implementation

• Choose an internal representation– An array of words?

• Write the methods• Test and debug using tester class• Then write the complete implementation