chapter 1: object oriented paradigm. 1.1 data abstraction and encapsulation oop allows programmer to...

Post on 14-Dec-2015

238 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 1: Object Oriented Paradigm

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 !

Strings represented on Macs vs Linux

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

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

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)

1.4 A Special-Purpose Class: A Bank Account

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

• Performs: – getAccount– getBalance– deposit– withdraw

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);

}

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

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

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

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

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

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

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

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

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

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

5 Step Data

Structure Design Process

Sketch an example test applicationhelps familiarize operations needed

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

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

top related