1 / 90

Building Abstractions with Data (Part 2)

Building Abstractions with Data (Part 2). CS 21a: Introduction to Computing I First Semester, 2013-2014. Last Time…. Building abstractions for data, analogous to abstractions for procedures The idea of types Algorithm- centered perspective Algorithms are active

emele
Download Presentation

Building Abstractions with Data (Part 2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Building Abstractions with Data (Part 2) CS 21a: Introduction to Computing I First Semester, 2013-2014

  2. Last Time… • Building abstractions for data, analogous to abstractions for procedures • The idea of types • Algorithm-centered perspective • Algorithms are active • Algorithms manipulate data

  3. Today… • More layers of abstraction • A deeper discussion about data types • Data-centeredperspective • Data are active • Data use algorithms

  4. Outline • Hierarchies of Abstraction • Data Types and Data Structures • From Procedural to OOP

  5. Compound Data Types Can Be Made of Compound Data Types • This should come as no surprise, given that we want to apply the same principles of abstraction to both algorithms and data.

  6. Example class Point { intx, y; } class Circle { Point center; intradius; } classLineSegment { Point firstEndPoint, secondEndPoint; }

  7. Example classStickFigure { Circle head; LineSegmentleftArm, rightArm, body, leftLeg, rightLeg; }

  8. What Allows Hierarchies of Abstraction? • The means of combination has the closure property (mathematics). • Recall: A set is closed under an operation if performing the operation on members of the set produces a member of the same set. • Example: integer + integer = integer

  9. What Allows Hierarchies of Abstraction? • If our set is a set of combinations (including primitives) and the operator is a combination operator, we have the following: • Expression op expression = expression • Procedure combine procedure = procedure • Data structure combine data structure = data structure

  10. Hierarchies for Expressions

  11. Hierarchies for Procedure Definitions

  12. Hierarchies for Data Type Definitions

  13. What Allows Hierarchies of Abstraction? • Notice the intimate relationship between hierarchies, trees, recursion, and the closure property.

  14. Practice Programming Problem: Facebook Profile • Define all the data types needed, along with the constructors and selectors, to implement a Facebook profile with the following information: • Id • Name • Birthday and Age • Phone Number • E-mail Address • Hometown • Location

  15. Outline • Hierarchies of Abstraction • Data Types and Data Structures • From Procedural to OOP

  16. What is a Data Type? • A set of possible values • Constructors are the means for building the whole (data of compound data type) out of the parts (data of simpler data types). • Selectors are the means for taking the parts (data of simpler data types) from the whole (data of compound data type).

  17. What is a Data Type? • A data type is a set of values and the expression of the need for constructors and selectors for breaking down those values in terms of simpler data types.

  18. The Data Has to Obey Certain Properties • The value of f is a Fractioniff… • The value of f is the result of makeFraction(a, b) implies num(f)is a and denom(f) is b, for some integers a and b

  19. The Data Has to Obey Certain Properties • Not just any variable can be meaningfully passed to numor denom • Not just any definition of makeFraction, num, and denomwill produce data that obeys this property

  20. The Data Structure Implements the Data Type • A data structure is a implementation of a data type. • A correct data structure for a data type has the correct definitions of the constructors and selectors to ensure that the data obeys rules imposed by the data type.

  21. The Data Structure Implements the Data Type • A data structure includes a complete description of how a data type is composed of simpler data types: • The types and (internally) the names of its parts. • Constructors: How the whole is made from the parts. • Selectors: How the parts are obtained from the whole.

  22. A Piece of Data is an Instance of a Type • The set is not the member of the set. • 1 is an int, but 1 is not int. • The data type is the blueprint, or idea, that all its instances must follow. • A piece of data is an instance of a data type. Note: it’s sometimes convenient to use the term “data type” to refer to both the abstract data type (which embodies the required properties without caring about the implementation) and any single concrete data structure that correctly implements the abstract data type.

  23. The Data is Not the Structure is Not the Type!

  24. Magic Analogies Problems Data Types A data type is a set of values and the expression of the need for procedures (constructors and selectors) for relating those values to simpler data types. • A problem is a set of input-output pairs and the expression of the need for procedures for relating those input-output pairs to simpler problems.

  25. Magic Analogies Algorithms Data Structures Data structures implement data types. There are many data structures that correctly implement one data type. Choice depends on application. • Algorithms solve problems. • There are many algorithms that correctly solve one problem. • Choice depends on application.

  26. Magic Analogies Computations Data Two independent pieces of data created from the same data structure may yield different values, even if they arise out of the same data structure. • Two independent simulations of the same algorithm may yield different behaviors, even if they arise out of the same algorithm.

  27. Magic Analogies Computations Data Two different data structures that implement the same data type can yield two pieces of data that look different, but given the same procedure applications will produce the same behavior. • Two different algorithms that solve the same problem can yield two computations that look different, but given the same question will produce the same answers.

  28. Does This Definition Apply to Primitive Types As Well? • Primitive data (and Strings) can be instantiated from a primitive data type (and the String type) using a literal. • Constructor: assigning a literal to a variable. • The value of a variable containing primitive data can be obtained by directly looking up the associated literal. • Selector: directly evaluating a variable.

  29. Literals • Variables are symbols that refer to other symbols. A variable needs to be evaluated against an environment to know its value. • A literal, as opposed to a variable, is a symbol which refers to itself. Its value is itself. • Examples: 5, 3.14, "Hello", true, false.

  30. Literals • Variables: The name is not the variable is not the value. • Literals: The name is the literal is the value.

  31. Constants • Some literals get used often enough that they can be given a name. • Can be declared outside procedures so that they can be used in all of them • Examples: • // inside a procedure finalintPI = 3.14159265359; • // outside a procedure staticintDIME_VALUE = 0.10;

  32. Quick Practice Programming Problem: PI • Create a procedure that computes the area of a circle given its radius. • Declare as a constant.

  33. Outline • Hierarchies of Abstraction • Data Types and Data Structures • From Procedural to OOP

  34. Bigger Systems Need More Modularity • I have tons of procedures in one file, now what? • Can happen in even larger systems • Ok, I’ve built barriers of abstraction with procedures to help me maintain the complexity, but… • Loading a 1MB file in my IDE just to edit a small part of the system? No, thanks.

  35. Algorithm-Centered Solution • Group all related functionality in modules: • Example in games • All physics in one module • All A.I. in one module • All constants in one module • All graphics in one module • javapjava.lang.Mathto see how it’s designed

  36. Fractions: Module Version public class Fraction { intnumerator; intdenominator; static Fraction makeFraction(intnum, intdenom) { Fraction f = newFraction(); f.numerator = num; f.denominator = denom; return f; }

  37. Fractions: Module Version static intnum(Fraction x) { returnx.numerator; } static intdenom(Fraction x) { returnx.denominator; } }

  38. Fractions: Module Version publicclass Arithmetic { staticFraction add(Fraction a, Fraction b) { intnum = Fraction.num(a) * Fraction.denom(b) + Fraction.num(b) * Fraction.denom(a); intdenom = Fraction.denom(a) * Fraction.denom(b); returnFraction.makeFraction(num, denom); } // other operations for arithmetic // static ComplexNum add(ComplexNum a, ComplexNum b) }

  39. Fractions: Module Version publicclass Printing { staticvoid print(Fraction x) { print(Fraction.num(x)); print("/"); print(Fraction.denom(x)); } // other operations for printing // static ComplexNum print(ComplexNum x) }

  40. Fractions: Module Version publicclassProgramThatUsesFraction{ publicstaticvoid main(String args[]) { Fraction x = Fraction.makeFraction(3, 2); Fraction y = Fraction.makeFraction(1, 5); Fraction sum = Arithmetic.add(x, y); Printing.print(sum); } }

  41. Data-Centered Solution • Group everything that has to do with a data type in one class. • A class is a blueprint for all objects of a given type, which includes all the procedures that can operate on the given type.

  42. An Algorithm-Centered View of Data Types • Data types are sets of possible values. • Data types are specified for operations. • Operations can only take values of certain types as operands. • Operations only return values of certain types.

  43. A Data-Centered View of Data Types • Data types are sets of possible values. • Operations are specified for data types. • Only certain operations can be performed on the values of a type. • Only certain operations can produce values of a (possibly different) data type.

  44. Procedural is Algorithm-Centered, OOP is Data-Centered • The main idea of OOP: • Give data behavior • Couple relevant operations on the data type with the data type definition • Let data be active, instead of passive arguments to procedures

  45. The Procedural View • Statements: Do actions on things. • Expressions: Combine things to create other things. • Constructors: There are special actions to create things. • Selectors: There are special actions to break things apart.

  46. The Object-Oriented View • Statements: Things perform actions. • Expressions: Things interact with other things to produce other things. • Constructors: Beginning with an initial state is one of the special actions a thing can perform. • Accessors: Things can report about its state with special actions. • Mutators: Things can change its state with special actions. The state of an object is its complete description in terms of the values of its components or fields.

  47. A Change in Vocabulary Procedural Object-Oriented Method invocation Accessor method • Procedure call • Selector

  48. Fractions: OOP Version • Suggestion: open the procedural version too to see how the similarities and differences.

  49. Fractions: OOP Version public class Fraction { intnumerator, denominator; public Fraction(intnum, intdenom) { this.numerator= num; this.denominator= denom; } public intnum() { returnthis.numerator; } public intdenom() { returnthis.denominator; } Put this in a different file from the one that contains your main method.

  50. Fractions: OOP Version (Continued) public void print() { print(this.num()); print("/"); print(this.denom()); } publicFraction add(Fraction x) { intnum = this.num() * x.denom() + x.num() * this.denom(); intdenom = this.denom() * x.denom(); return new Fraction(num, denom); } Put this in a different file from the one that contains your main method.

More Related