COMPSCI 61A Lecture Notes - Lecture 9: Abstract Data Type, Rational Number
zogo39484755 and 6 others unlocked
22
COMPSCI 61A Full Course Notes
Verified Note
22 documents
Document Summary
Most values are compound objects combine objects together. Abstract data type, lets us manipulate compound objects as units. Isolate two parts of a program that uses data: Data abstraction: a methodology by which functions enforce an abstraction barrier between representation and use. As soon as division occurs, the exact representation may be lost. Assume we can compose and decompose rational numbers: Rational (n, d), returns rational number x. Constructor: builds new value, instance of abstract data type. Return numer(x) * denom(y) == numer(y) * denom(x) Ways we can manipulate rationals using three functions, these functions manipulate an abstract data type. Two values that are bundled together, that you can treat them together as a whole. >>> pair = [1,2] creates new list. Return [n, d] # constructs a rational number that represents n/d def numer(x): Context of rational numbers( useful for reducing to lowest terms)