data abstraction announcements data abstraction data
play

Data Abstraction Announcements Data Abstraction Data Abstraction - PowerPoint PPT Presentation

Data Abstraction Announcements Data Abstraction Data Abstraction 4 Data Abstraction Compound values combine other values together 4 Data Abstraction Compound values combine other values together A date: a year, a month, and


  1. Rational Number Arithmetic Implementation def mul_rational(x, y): return rational(numer(x) * numer(y), nx ny nx*ny denom(x) * denom(y)) * = Constructor dx dy dx*dy Selectors Selectors def add_rational(x, y): nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) return rational(nx * dy + ny * dx, dx * dy) nx ny nx*dy + ny*dx def print_rational(x): + = print(numer(x), '/', denom(x)) dx dy dx*dy • rational(n, d) returns a rational number x These functions implement an • numer(x) returns the numerator of x abstract representation for rational numbers • denom(x) returns the denominator of x � 7

  2. Rational Number Arithmetic Implementation def mul_rational(x, y): return rational(numer(x) * numer(y), nx ny nx*ny denom(x) * denom(y)) * = Constructor dx dy dx*dy Selectors Selectors def add_rational(x, y): nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) return rational(nx * dy + ny * dx, dx * dy) nx ny nx*dy + ny*dx def print_rational(x): + = print(numer(x), '/', denom(x)) dx dy dx*dy def rationals_are_equal(x, y): return numer(x) * denom(y) == numer(y) * denom(x) • rational(n, d) returns a rational number x These functions implement an • numer(x) returns the numerator of x abstract representation for rational numbers • denom(x) returns the denominator of x � 7

  3. Pairs

  4. Representing Pairs Using Lists � 9

  5. Representing Pairs Using Lists >>> pair = [1, 2] � 9

  6. Representing Pairs Using Lists >>> pair = [1, 2] >>> pair [1, 2] � 9

  7. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] � 9

  8. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] >>> x, y = pair � 9

  9. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] >>> x, y = pair >>> x 1 � 9

  10. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] >>> x, y = pair >>> x 1 >>> y 2 � 9

  11. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 � 9

  12. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1 � 9

  13. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] 1 >>> pair[1] 2 � 9

  14. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] Element selection using the selection operator 1 >>> pair[1] 2 � 9

  15. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] Element selection using the selection operator 1 >>> pair[1] 2 >>> from operator import getitem � 9

  16. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] Element selection using the selection operator 1 >>> pair[1] 2 >>> from operator import getitem >>> getitem(pair, 0) 1 � 9

  17. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] Element selection using the selection operator 1 >>> pair[1] 2 >>> from operator import getitem >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 2 � 9

  18. Representing Pairs Using Lists >>> pair = [1, 2] A list literal: >>> pair Comma-separated expressions in brackets [1, 2] "Unpacking" a list >>> x, y = pair >>> x 1 >>> y 2 >>> pair[0] Element selection using the selection operator 1 >>> pair[1] 2 >>> from operator import getitem Element selection function >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 2 � 9

  19. Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] � 10

  20. Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] Construct a list � 10

  21. Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] Construct a list def numer(x): """Return the numerator of rational number X.""" return x[0] � 10

  22. Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] Construct a list def numer(x): """Return the numerator of rational number X.""" return x[0] def denom(x): """Return the denominator of rational number X.""" return x[1] � 10

  23. Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] Construct a list def numer(x): """Return the numerator of rational number X.""" return x[0] def denom(x): """Return the denominator of rational number X.""" return x[1] Select item from a list � 10

  24. Representing Rational Numbers def rational(n, d): """Construct a rational number that represents N/D.""" return [n, d] Construct a list def numer(x): """Return the numerator of rational number X.""" return x[0] def denom(x): """Return the denominator of rational number X.""" return x[1] Select item from a list (Demo) � 10

  25. Reducing to Lowest Terms Example: � 11

  26. Reducing to Lowest Terms Example: 3 5 * 2 3 � 11

  27. Reducing to Lowest Terms Example: 3 5 5 * = 2 3 2 � 11

  28. Reducing to Lowest Terms Example: 3 5 5 * = 2 3 2 15 1/3 5 * = 6 1/3 2 � 11

  29. Reducing to Lowest Terms Example: 2 1 3 5 5 * = + 5 10 2 3 2 15 1/3 5 * = 6 1/3 2 � 11

  30. Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 * = 6 1/3 2 � 11

  31. Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 � 11

  32. Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd � 11

  33. Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd def rational(n, d): � 11

  34. Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" � 11

  35. Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) � 11

  36. Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) return [n//g, d//g] � 11

  37. Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd Greatest common divisor def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) return [n//g, d//g] � 11

  38. Reducing to Lowest Terms Example: 2 1 1 3 5 5 * = + = 5 10 2 2 3 2 15 1/3 5 25 1/25 1 * = * = 6 1/3 2 50 1/25 2 from fractions import gcd Greatest common divisor def rational(n, d): """Construct a rational that represents n/d in lowest terms.""" g = gcd(n, d) return [n//g, d//g] (Demo) � 11

  39. Abstraction Barriers

  40. Abstraction Barriers � 13

  41. Abstraction Barriers Parts of the program that... Treat rationals as... Using... � 13

  42. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 to perform computation � 13

  43. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 whole data values to perform computation � 13

  44. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational � 13

  45. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement rational operations � 13

  46. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational operations denominators � 13

  47. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators � 13

  48. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators � 13

  49. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and constructor for rationals � 13

  50. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and two-element lists constructor for rationals � 13

  51. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and two-element lists list literals and element selection constructor for rationals � 13

  52. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and two-element lists list literals and element selection constructor for rationals � 13

  53. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and two-element lists list literals and element selection constructor for rationals Implementation of lists � 13

  54. Abstraction Barriers Parts of the program that... Treat rationals as... Using... Use rational numbers 
 add_rational, mul_rational whole data values to perform computation rationals_are_equal, print_rational Create rationals or implement numerators and rational, numer, denom rational operations denominators Implement selectors and two-element lists list literals and element selection constructor for rationals Implementation of lists � 13

  55. Violating Abstraction Barriers add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ] � 14

  56. Violating Abstraction Barriers Does not use constructors add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ] � 14

  57. Violating Abstraction Barriers Does not use Twice! constructors add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ] � 14

  58. Violating Abstraction Barriers Does not use Twice! constructors add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ] No selectors! � 14

  59. Violating Abstraction Barriers Does not use Twice! constructors add_rational( [1, 2], [1, 4] ) def divide_rational(x, y): return [ x[0] * y[1], x[1] * y[0] ] No selectors! And no constructor! � 14

  60. Violating Abstraction Barriers � 14

Recommend


More recommend