Object Oriented Approach

advertisement
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 same
return this.account.equals(that.account);
}
Class Object
• Every class is a descendant of Object
• Defines fundamental methods
– equals()
– toString()
– clone()
same data as another object
string representation for display
makes an identical copy
• If you don't define your own versions of these,
automatic (usually defective) ones will be
provided
Memory Model of BankAccount object
BankAccount jane;
jane
jane =
null 100
new BankAccount(“J. Doe”, 345.67);
account
J. Doe
balance
345.67
The number
100
indicates
the memory
location
where the
object
resides
Memory allocated by the declaration
BankAccount jane;
jane = new BankAccount(“J. Doe”, 345.67);
10
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
11
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 as
jon = jane.clone(); //copy jane into jon
12
Memory Model of Two Types of Copies
jon
jane
100
200
location 100
account
balance
jon j. doe
j. doe
deep copy
187.95 345.67
345.67
location 200
account
balance
A Deep Copy of the Object jane into the Object jon
jon
100 200
shallow copy
jane
200
j. doe
location 200
345.67
A Shallow Copy of the Object jane into the Object jon
13
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 objects
protected Object theKey; // the key of the key-value pair
protected 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 application
helps familiarize operations needed
Sketch the “interface” for the class
names 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
Download