Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Abstraction: Understanding Abstract and Concrete Representations in CS111, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-p7e
koofers-user-p7e 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Data Abstraction
Captain Abstraction
Meets Private Data
Friday, December 7, 2007
A
D
Data Abstraction 24-2
Revisiting A Big Idea: Abstraction
Class
Black Box
Implementer /
Designer
User / Client
Contract
ABSTRACTION BARRIER
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Data Abstraction: Understanding Abstract and Concrete Representations in CS111 and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science Wellesley College

Data Abstraction

Captain Abstraction

Meets Private Data

Friday, December 7, 2007

A

D

Data Abstraction 24- 2

Revisiting A Big Idea: Abstraction

Class

Black Box

Implementer /

Designer

User / Client

Contract

ABSTRACTION BARRIER

Data Abstraction 24- 3 Two Kinds of Abstraction in CS

1. Procedural Abstraction:

Methods abstract over computational behavior

2. Data Abstraction:

Classes abstract over state and behavior of objects

Today, we’ll briefly review procedural abstraction and

then focus on data abstraction.

Data Abstraction 24- 4 Procedural Abstraction

Methods capture procedural patterns, abstracting over behaviors.

The contract of a method specifies its behavior.

public void drawColoredLine (int n, Color c);

Assume this buggle’s brush is up.

Use color c to paint n cells in the direction that this

buggle is facing, starting with the cell it is currently over.

The final state of the buggle should be the same

as its initial state.

Parameters abstract over values used by the method.

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

We often want instance and class variables that can be publicly

accessed but are immutable – i.e., they cannot be changed.

The final keyword is used for this purpose. Once a final variable

has been initialized, it cannot be changed.

// Instance variables of the Location class, whose

// instances are immutable 2D integer points

public final int x;

public final int y;

// Class variables of the Color class

public static final Color red;

public static final Color blue;

public static final Color magenta;

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

Most classes that we have used this semester do not have public

instance variables; they have private instance variables instead.

E.g.: Buggle, Color, Direction, Graphics, IntList,

Picture, StringList, Sprite, Turtle

Why?

  • A contract shouldn’t be cluttered with details clients don’t

need to know.

  • Private variables can be accessed/changed only via methods,

giving the implementer fine-grained control over their use.

E.g., using setPosition() to change a Buggle’s position allows

redrawing the Buggle in the grid.

  • Keeping variables hidden gives implementers the freedom

to use clever implementations and change implementations.

Moral: Make your instance variables private! Data Abstraction 24- 10 Tinkering Under the Hood

To get some hands-on experience with private data, we’ll spend

the rest of today tinkering with the implementations of several

classes that we know and love:

  • Pic
  • Buggle
  • IntList
  • Direction

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

  • 1

2

int[] 3

4

5

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

The Pic examples illustrate a key

aspect of data abstraction:

clients care about the abstract

representations and behavior

of objects, not their concrete

structure.

If an object walks like a duck,

quacks like a duck, and behaves

like a duck every other respect,

data abstraction says that it’s a

duck --- even if it’s a mechanical

duck or a wolf in a duck suit!

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:

  1. x and y coordinates: Buggle y

x 7 4 x y Point Buggle position

2. A (mutable) Point object …

7 4 Buggle position

3. A list of 2 integers …

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!

Although the examples we’ve shown illustrate the key ideas

of data abstraction, they’re not particularly compelling.

The Data Structures course (CS230) is chock full of

compelling examples of standard data abstractions that

every practicing programmer needs to know:

  • vectors (extensible arrays)
  • stacks
  • queues
  • priority queues
  • sets
  • bags
  • tables
  • trees
  • graphs