Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the."— Presentation transcript:

1 Chapter 1: Object Oriented Paradigm

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

3 Strings represented on Macs vs Linux

4 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

5 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

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

7 1.4 A Special-Purpose Class: A Bank Account Manages data: – balance and account number (or account name) Performs: – getAccount – getBalance – deposit – withdraw

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

9 Class ObjectObject 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 10 Memory Model of BankAccount object Memory allocated by the declaration BankAccount 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 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 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 as jon = jane.clone(); //copy jane into jon

13 13 Memory Model of Two Types of Copies 100 200 jane jon location 200 location 100 jon j. doe 187.95 345.67345.67 j. doe deep copy A Deep Copy of the Object jane into the Object jon account balance account balance 100 200 200 jane jon location 200 345.67 shallow copy A Shallow Copy of the Object jane into the Object jon j. doe

14 Using Scanner and File Scanner – a class representing objects that read input streams or files and extracts primitive data types from them Scanner File – a class representing a path to a location on disk or other media File We will now apply these to our BankAccount app

15 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

16 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

17 An app that uses Association atinLay – a Pig Latin translatorPig Latintranslator 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

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

19 5 Step Data Structure Design Process

20 Sketch an example test application helps familiarize operations needed

21 Sketch the “interface” for the class names of methods, parameters, return types

22 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 ppt "Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the."

Similar presentations


Ads by Google