1 / 77

Building Abstractions with Data (Part 1)

Building Abstractions with Data (Part 1). CS 21a: Introduction to Computing I First Semester, 2013-2014. Last Time…. Procedural Abstraction Creating layers of abstraction for algorithms. Today…. Data Abstraction Creating layers of abstraction for data. Outline. The Notion of Types

rupert
Download Presentation

Building Abstractions with Data (Part 1)

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 1) CS 21a: Introduction to Computing I First Semester, 2013-2014

  2. Last Time… • Procedural Abstraction • Creating layers of abstraction for algorithms

  3. Today… • Data Abstraction • Creating layers of abstraction for data

  4. Outline • The Notion of Types • Abstractions with Data Types • Same Type, Different Implementations

  5. The Notion of Types • “G” is a letter, but not a number. • “800-9000” is a telephone number, but “name@example.com” is an e-mail address. • What you’re sitting on is a chair, not a table (hopefully). • You are a person, not a dog (don’t let your significant other change that).

  6. The Notion of Types • Any value has a type. • Any piece of data has a data type. • Why is this important?

  7. Type Safety • There are procedures that can only be meaningfully carried out if the input data is of a certain type. • Examples: • Sitting should be done on chairs, (maybe occasionally on tables if you have good reasons), but not on candles. • Squaring should be done on numbers, but not on words.

  8. Type Safety • Helps catch programming errors early • No accidental squaring of words and using the result as a prescribed amount of medicine.

  9. Which Assignments Are Allowed? inta = 16; double b = 18.5; a = b; double c = 18; a = c; c = a;

  10. Type Safety in Variables inta = 16; double b = 18.5; a = b; // not allowed double c = 18; // allowed: integers are real numbers a = c; // still not allowed c = a; // allowed: integers are real numbers

  11. Type Safety in Variables inta = 16; double b = 18.5; a = b; // not allowed double c = 18; // allowed: integers are real numbers a = c; // still not allowed c = a; // allowed: integers are real numbers Type specifiersput a restriction on what values a variable can hold.

  12. Type Safety in Procedures staticintsquare(int x) { return x*x;} parameter type specifier return type specifier return value must be of correct type

  13. Will This Work? int a = square("Hello");

  14. Will This Work? int a = square("Hello"); square expects integer arguments. Design by contract, and propagate type safety down the barriers of abstraction: square needs to ensure that it’s given an integer, because it will use that argument in a * operation, which also requires its operands to be numbers. The user of square must therefore follow this restriction and only pass an integer argument.

  15. How About This? inta = square(3);

  16. How About This? inta = square(3.0);

  17. How About This? double x = 3; inta = square(x);

  18. How About This? double x = 3; inta = square(x); It’s kind of annoying that our square procedure can’t work for real numbers too. We’ll fix this later so that we can have a generic procedure definition that works for both integers and real numbers.

  19. A Quick Fix? staticdouble square(doublex) { return x*x;} … double x = 3; double y = square(x); // this works now double a = square(3.5); // and so does this intb = square(5); // but now this won’t work

  20. A Quick Fix? staticdouble square(doublex) { return x*x;} … double x = 3; double y = square(x); // this works now double a = square(3.5); // and so does this intb = square(5); // but now this won’t work The complete fix? Much later, or maybe in CS21B.

  21. How About This? booleana = square(3);

  22. How About This? booleana = square(3) > square(4);

  23. How About This? booleana = square(3) + square(4);

  24. How About This? staticintsquare(int x) { return"Hello";}

  25. The voidReturn Type • Recall: imperative programming insists that some instructions are statements, and others are expressions. • Expressions always have values. • Statements do not always do.

  26. The void Return Type • Some instructions can be statements without values. • The print statement doesn’t have an output value, because it already directly prints out to console. There’s no point keeping its “value” somewhere or using it in another expression. • Same goes with main.

  27. The void Return Type • So, we invent a new “return type” called void. • Putting void as a return type for a procedure definition means that the procedure does something, but does not return a value to the procedure that called it.

  28. Example staticvoidsayFavorite(String name, intnumber) { print(person); print("'s favorite number is "); print(number);} … sayFavorite("Grandma", 42); int x = sayFavorite("Grandma", 42);

  29. Example staticvoidsayFavorite(String name, intnumber) { print(person); print("'s favorite number is "); print(number);} … sayFavorite("Grandma", 42); int x = sayFavorite("Grandma", 42); Ok. Procedure call is a statement. Prints “Grandma’s favorite number is 42”.

  30. Example staticvoidsayFavorite(String name, intnumber) { print(person); print("'s favorite number is "); print(number);} … sayFavorite("Grandma", 42); int x = sayFavorite("Grandma", 42); Not ok. Procedure call does not produce a value. Alternatively, procedure does not have the correct return type.

  31. Example staticvoidsayFavorite(String name, intnumber) { print(person); print("'s favorite number is "); print(number);} … sayFavorite("Grandma", 42); int x = sayFavorite("Grandma", 42); By the way, the String is another built-in type over the set of words or series of characters.

  32. Example staticvoidsayFavorite(String name, intnumber) { print(person); print("'s favorite number is "); print(number);} … sayFavorite("Grandma", 42); int x = sayFavorite("Grandma", 42); Strings can take on series of characters enclosed in double quotes as values.

  33. Question • Why is it possible to print(square(4)); • But not to print(print(4)); ?

  34. Question • Why is it possible to print(square(4)); • But not to print(print(4)); ? All expressions have values, but not all procedure calls do. The print procedure has a void return type specifier.

  35. Outline • The Notion of Types • Abstractions with Data Types • Same Type, Different Implementations

  36. Three Basic Kinds of Answers to Computational Questions • Number • How far is the earth from the sun? • Yes/No • Is Venus farther from the earth from the sun? • Words • What are the planets closer to the sun than the earth is?

  37. Review: “Problem” Versions for Those Questions • Number • How far is a given planet from the sun? • Yes/No • Is this given planet farther from that given planet from the sun? • Words • What are the planets closer to the sun than this given planet is?

  38. Three Basic Kinds of Input to Computational Problems • Number • Given mass and acceleration, what’s the force? • Yes/No • Depending on whether the stock goes up or down, what’s the best next investment move? • Words • Given a search phrase, what are the most relevant web pages?

  39. Three Kinds of Primitive Data Types • Numerical Data Types • Central assumption of information theory: everything can be represented as a number • Numbers encoded in binary (0s and 1s) • Usually in many variants for efficiency’s sake • The Boolean Data Type • True or False • Assign 0 to false, 1 to true

  40. Three Kinds of Primitive Data Types • Symbolic Data Types • Characters • Can be represented as numbers, and later encoded to 0s and 1s • Strings (not really primitive, because Strings are sequences of characters) • Maybe more advanced A.I. can answer some “how” and “why” questions, but even for those, symbolic data will be enough.

  41. Primitive Data Types in Java • Read about them here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

  42. Are These Enough? • Consider: • Given the x and y coordinates of a point, find the x and y coordinates of its reflection across the line with the given A, B, and C coefficients in its equation in general form. • Overly detailed! • And what if we want to expand to three dimensions later?

  43. Are These Enough? • Limitation of Java: • Can’t have more than one output value • Only one return statement per procedure • Only one return value per return statement • Can’t output both x and y as two numbers from the same procedure, even if it makes sense to let one procedure simultaneously produce them

  44. Are These Enough? • Other kinds of input: • Sound input • Keyboard/controller input • Biometric sensors

  45. Are These Enough? • Information comes in many physical kinds. • Light, sound, matter, etc. • Information comes in many conceptual kinds. • Money, personal records, shapes, etc.

  46. Are These Enough? • Every kind of data can be broken down into numbers (even symbolic and Boolean data). • For complex systems though, we don’t want to go to the level of primitives all the time.

  47. Data Abstraction • It’s better to say: • Given a point, find its reflection across a given line. • Point and line are compound data types. • Procedure for doing this doesn’t have to worry about how points or lines are represented in terms of primitives, as long as the representation obeys certain properties. • Important point (no pun intended) we’ll get to later.

  48. Adding Fractions: Without Data Abstraction staticintnumerator-add(inta, int b, intc, int d) { return a * d + b * c; } staticintdenominator-add(inta, int b, int c, int d) { return b * d; }

  49. Adding Fractions: With Data Abstraction static Fraction add(Fraction a, Fraction b) { intnum = num(a) * denom(b) + num(b) * denom(a); intdenom = denom(a) * denom(b); returnmakeFraction(num, denom); }

  50. Interfacing with Primitives • Input to outer procedure (world) might still be given as primitive data. • To chunk the primitive data, we need to use constructors. • Output from outer procedure (world) might still need to be primitive data. • To examine (and print out) the parts of a chunked piece of data, we need to use selectors.

More Related