






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A part of the cs111 computer programming course notes at wellesley college. It discusses data abstraction, a fundamental concept in computer science. The difference between procedural and data abstraction, the importance of contracts, and the abstract and concrete representations of objects. It also covers the use of private and public variables and the concept of immutable variables. The document concludes by mentioning the data structures course (cs230) and its compelling examples of standard data abstractions.
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!
Department of Computer Science Wellesley College
Friday, December 7, 2007
Data Abstraction 24- 2
Contract
Data Abstraction 24- 3 Two Kinds of Abstraction in CS
Data Abstraction 24- 4 Procedural Abstraction
Data Abstraction 24- 7 Contract Example: java.awt.Point // An instance of the Point class has mutable (changeable) // x and y integer coordinates. // Instance variables public int x; // x coordinate of point public int y; // y coordinate of point // Constructor methods public Point (); // construct the point (0,0); public Point (int x, int y); // construct the point (x,y); public Point (Point p); // construct a new point (p.x, p.y); // Instance methods public void move (int x, int y); // move this point to (x,y) public void translate (int dx, int dy); // move this point to (x+dx, y+dy) public void toString(); // return a String representation of this point // … many other Point instance methods … Because they’re public, anyone can access and change the x and y instance variables Data Abstraction 24- 8 public final Variables Are Immutable
Can write loc.x but not loc.x = 3; Don’t want anyone to be able to redefine these colors!
Data Abstraction 24- 9 public vs. private Variables
Why?
Moral: Make your instance variables private! Data Abstraction 24- 10 Tinkering Under the Hood
Data Abstraction 24- 13 A Different Concrete Pic Representation Sometimes a concrete representation can differ dramatically from the abstract one. E.g., a Pic instance might consist of a height variable and a 1D array of pixels. 0
int[] 3
Pic pixArray A different concrete representation of a Pic instance height 2 public int getHeight() { return height; } public int getWidth() { if (height == 0) return 0; else return pixArray.length/height; } public int getPixel (int row, int col) { return pixArray(row*getWidth() + col); } How would you write getPixArray() using this representation? Data Abstraction 24- 14 Like A Duck
Data Abstraction 24- 15 Abstract Buggle Representation All semester long we’ve drawn abstract representations of Buggles: x y Location Buggle heading position color Abstract representation of a Buggle instance brushDown true Direction WEST Color Blue But the concrete representation could be rather different. Let’s explore some possibilities! (We choose to represent Direction and Color instances with names that indicate the value.) 7 4 Data Abstraction 24- 16 Buggle Positions A Buggle position need not be represented as a Location object. It could be:
x 7 4 x y Point Buggle position
7 4 Buggle position
7 4 ∅ … and many other representations (e.g. a 2-element integer array in PS10). How would the implementation change to use these representations? What are their advantages and disadvantages?
Data Abstraction 24- 19 On To CS230!