DOC PREVIEW
WUSTL CSE 131 - sp14_8

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Object Oriented ProgrammingObject Oriented ProgrammingAlan KaySlide 5IntuitionEncapsulationIntuitionIntuitionCounter Data TypeCounter Data TypeChanging Internal RepresentationTime BombsAsk, Don't TouchSlide 15Immutability: Advantages and DisadvantagesFinal Access ModifierSlide 18Vector Data TypeVector Data Type ApplicationsVector Data Type: ImplementationVector Data Type: Implementation3.3 Designing Data TypesIntroduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 1/14/19 06:11:57 AM2Object Oriented ProgrammingProcedural programming. [verb-oriented]Tell the computer to do this.Tell the computer to do that.OOP philosophy. Software is a simulation of the real world.We know (approximately) how the real world works.Design software to model the real world.Objected oriented programming (OOP). [noun-oriented]Programming paradigm based on data types.Identify objects that are part of the problem domain or solution.Identity: objects are distinguished from other objects (references).State: objects know things (instance variables).Behavior: objects do things (methods).3Object Oriented Programming4Alan KayAlan Kay. [Xerox PARC 1970s]Invented Smalltalk programming language.Conceived Dynabook portable computer.Ideas led to: laptop, modern GUI, OOP.Alan Kay2003 Turing Award “ The computer revolution hasn't started yet. ” “ The best way to predict the future is to invent it. ” “ If you don't fail at least 90 per cent of the time, you're not aiming high enough. ” — Alan Kay5EncapsulationBond. What's your escape route?Saunders. Sorry old man. Section 26 paragraph 5, that information is on a need-to-know basis only. I'm sure you'll understand.6IntuitionClient API - volume - change channel - adjust picture - decode NTSC signalImplementation - cathode ray tube - electron gun - Sony Wega 36XBR250 - 241 poundsImplementation and client need to agree on API ahead of time.client needs to know how to use APIimplementation needs to knowwhat API to implement7EncapsulationData type. Set of values and operations on those values.Ex. int, String, Complex, Vector, Document, GuitarString, …Encapsulated data type. Hide internal representation of data type.Separate implementation from design specification.Class provides data representation and code for operations.Client uses data type as black box.API specifies contract between client and class.Bottom line. You don't need to know how a data type is implementedin order to use it.8IntuitionClient API - volume - change channel - adjust picture - decode NTSC signalImplementation - cathode ray tube - electron gun - Sony Wega 36XBR250 - 241 poundsImplementation and client need to agree on API ahead of time.client needs to know how to use APIimplementation needs to knowwhat API to implement9IntuitionAPI - volume - change channel - adjust picture - decode NTSC signalImplementation - gas plasma monitor - Samsung FPT-6374 - wall mountable - 4 inches deepCan substitute better implementation without changing the client.Clientclient needs to know how to use APIimplementation needs to knowwhat API to implement10Counter Data TypeCounter. Data type to count electronic votes.Legal Java client.Oops. Al Gore receives -16,022 votes in Volusia County, Florida.public class Counter { public int count; public final String name; public Counter(String id) { name = id; } public void increment() { count++; } public int value() { return count; } }Counter c = new Counter("Volusia County");c.count = -16022;11Counter. Encapsulated data type to count electronic votes.Does not compile.Benefit. Can guarantee that each data type value remainsin a consistent state.public class Counter { private int count; private final String name; public Counter(String id) { name = id; } public void increment() { count++; } public int value() { return count; } }Counter Data TypeCounter c = new Counter("Volusia County");c.count = -16022;12Changing Internal RepresentationEncapsulation.Keep data representation hidden with private access modifier.Expose API to clients using public access modifier.Advantage. Can switch internal representation without changing client.Note. All our data types are already encapsulated!public class Complex { private final double re, im; public Complex(double re, double im) { … } public double abs() { … } public Complex plus(Complex b) { … } public Complex times(Complex b) { … } public String toString() { … }}e.g., to polar coordinates13Time BombsInternal representation changes.[Y2K] Two digit years: January 1, 2000.[Y2038] 32-bit seconds since 1970: January 19, 2038.[VIN numbers] We'll run out by 2010.Lesson. By exposing data representation to client, might needto sift through millions of lines of code in client to update.www.cartoonstock.com/directory/m/millenium_time-bomb.asp14Encapsulated data types.Don't touch data and do whatever you want.Instead, ask object to manipulate its data.Lesson. Limiting scope makes programs easier to maintain and understand."Ask, don't touch."Adele GoldbergFormer president of ACMCo-developed SmalltalkAsk, Don't Touch"principle of least privilege"Immutability16Immutability: Advantages and DisadvantagesImmutable data type. Object's value cannot change once constructed.Advantages.Avoid aliasing bugs.Makes program easier to debug.Limits scope of code that can change values.Pass objects around without worrying about modification.Disadvantage. New object must be created for every value.17Final Access ModifierFinal. Declaring an instance variable to be final means that you can assign it a value only once, in initializer or constructor.Advantages.Helps enforce immutability.Prevents accidental changes.Makes program easier to debug.Documents that the value cannot not change.public class Counter { private final String name; private int count;...}this value changes by invokinginstance methodthis value doesn't changeonce the object is constructedSpatial Vectors19Vector Data TypeSet of values. Sequence of real numbers. [Cartesian coordinates]API.x = (0, 3, 4, 0), y = (0, -3, 1, -4)x + y = (0, 0, 5, -4)3x = (0, 9, 12, 0)x  y = (0  0) + (3  -3) + (4  1) + (0  -4) = -5| x | = (02 + 32 + 42 + 02)1/2 = 5x = x / | x


View Full Document

WUSTL CSE 131 - sp14_8

Documents in this Course
sp14_4

sp14_4

28 pages

sp14_3

sp14_3

29 pages

sp14_2

sp14_2

43 pages

sp14_10

sp14_10

19 pages

sp14_9

sp14_9

16 pages

sp14_7

sp14_7

33 pages

sp14_6

sp14_6

27 pages

sp14_5

sp14_5

55 pages

lecture1

lecture1

33 pages

lab0

lab0

6 pages

Load more
Download sp14_8
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view sp14_8 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view sp14_8 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?