Cs 112 introduction to programming
This presentation is the property of its rightful owner.
Sponsored Links
1 / 26

CS 112 Introduction to Programming PowerPoint PPT Presentation


  • 54 Views
  • Uploaded on
  • Presentation posted in: General

CS 112 Introduction to Programming. Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: [email protected] Admin. Updated class schedule. Recap: Encapsulating Data.

Download Presentation

CS 112 Introduction to Programming

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cs 112 introduction to programming

CS 112 Introduction to Programming

Design Good Classes: Encapsulation and OOP Analysis

Yang (Richard) Yang

Computer Science Department

Yale University

308A Watson, Phone: 432-6400

Email: [email protected]


Admin

Admin

  • Updated class schedule


Recap encapsulating data

Recap: Encapsulating Data

Client can see only the external API

Methods

Client

Data/state

Client should not see the internal state or behaviors


Recap encapsulating data1

Recap: Encapsulating Data

  • Benefit

    • Consistency: so that it is impossible for others to "reach in" and directly alter another object's state

    • Flexibility: so that you can change the state representation later without worrying about breaking others’ code


Ask don t touch

Ask, Don't Touch

  • Encapsulated data types.

    • Don't touch data and do whatever you want.

    • Instead, ask object to manipulate its data.

  • Lesson. Limiting scope makes programs easier to maintain and understand.

"Ask, don't touch."

Adele GoldbergFormer president of ACMCo-developed Smalltalk

"principle of least privilege"


Recap encapsulation how

Recap: Encapsulation/How

  • access modifiers enforce encapsulation

    • public members (data and methods: can be accessed from anywhere

    • private members: can be accessed from a method defined in the same class

    • Members without an access modifier: default private accessibility, i.e., accessible in the same package; otherwise, not accessible.


Examples set the access modifiers

Examples: Set the Access Modifiers

  • Coin

  • Ball

  • BankAccount

  • Point


Class diagram

Coin

- face : int

+ flip() : void

+ isHeads() : boolean

+ toString() : String

Class Diagram

class name

attributes

methods

Above is a class diagram representing the Coin class.

“-” indicates private data or method

“+” indicates public data or method


Cs 112 introduction to programming

Outline

  • Admin and recap

  • Defining classes

    • Motivation and basic syntax

    • Examples

    • The encapsulation principle

    • An OOP example: geomap visualization


Data visualization

Data Visualization

“ If I can't picture it, I can't understand it. ”

— Albert Einstein

Edward Tufte. Create charts with high data density that tell the truth.


Visualization based on geography

Visualization based on Geography

Classes?


Cs 112 introduction to programming

Major Classes and Relationship

m

1

m

2

1

1

1

m

GeoMap

Region

Color

A composition relationship

An associationrelationship

Polygon

Point


Cs 112 introduction to programming

Major Classes and Relationship

GeoMap

Region

Region

String

Polygon

Color

Color

Point

Point


Cs 112 introduction to programming

Major Classes and Relationship

m

1

m

2

1

1

m

1

GeoMap

Region

Color

A composition relationship

An associationrelationship

Polygon

Design question:

- Immutable objects?

Point


Cs 112 introduction to programming

Major Classes and Relationship

m

1

m

2

1

1

m

1

GeoMap

Region

Color

A composition relationship

An associationrelationship

Polygon

Design question:

- What is the basic controller (of GeoMap) structure?

Point


Cs 112 introduction to programming

Coloring Controller Structure

  • Retrieve region

    • Batch: retrieve list of all regions

    • Specific: retrieve one specific region

  • Coloring

    • Map properties of a region to a color


Cs 112 introduction to programming

Major Classes and Relationship

m

1

m

2

1

1

m

1

GeoMap

Region

Color

A composition relationship

An associationrelationship

Polygon

  • Discussion:

  • Public methods (API) of Point

Point


Cs 112 introduction to programming

Major Classes and Relationship

m

1

m

2

1

1

m

1

GeoMap

Region

Color

A composition relationship

An associationrelationship

Polygon

  • Discussion:

  • Public methods (API) of Polygon

Point


Polygon

Polygon

public class Polygon {

private final int N; // number of boundary points

private final Point[] points; // the points

// read from input stream

public Polygon(Scannerinput) {

N = input.nextInt();

points = new Point[N+1];

for (int i = 0; i < N; i++) {

points[i] = new Point ( input );

}

points[N] = points[0];

}

public void draw() { … }

public void fill() { … }

public boolean contains(Point p) { … }

public Point centroid() { … }

}


Cs 112 introduction to programming

Major Classes and Relationship

m

1

m

2

1

1

m

1

GeoMap

Region

Color

A composition relationship

An associationrelationship

Polygon

  • Discussion:

  • Public methods (API) of Region

Point


Region

Q: Should Region have a method that returns its internal Polygon?

Region

public class Region {

private final String regionName; // name of region

private final String mapName;

private final Polygon poly; // polygonal boundary

private Color fillColor, drawColor;

public Region(String mName, String rName, Polygon poly) {

regionName = rName;

mapName = mName;

this.poly = poly;

setDefaultColor();

}

public void setDrawColor (Color c) {drawColor = c; }

public void draw() { setDrawColor(); poly.draw (); }

public void fill() { … }

public boolean contains(Point p) {

return poly.contains(p);

}

public Point centroid() { return poly.centroid() }

}

Even though most complexity is in Polygon, Polygon is not exposed. Region delegates tasks internally to Polygon.


Example controllers

Example Controllers

  • GeoMap.java

  • RandomColorMap.java

  • ClickColorMap.java

  • RedBlueMap.java


Purple america purplemap java

Purple America (PurpleMap.java)

  • Idea. [Robert J. Vanderbei] Assign color based on number of votes.

    • a1 = Rep. votes.

    • a2 = Other votes.

    • a3 = Dem. votes.

100% Rep

55% Dem, 45% Rep

100% Dem

100% Other

public Color getColor() {

int dem = tally.dem(), rep = tally.rep(), ind = tally.ind();

int tot = tally.dem + tally.rep + tally.ind;

return new Color((float) rep/tot, (float) ind/tot, (float) dem/tot);

}


Cartograms

Cartograms

  • Cartogram. Area of state proportional to number of electoral votes.

Michael Gastner, Cosma Shalizi, and Mark Newman

www-personal.umich.edu/~mejn/election


Cartograms1

Cartograms

  • Cartogram. Area of country proportional to population.


Summary

Summary

  • Encapsulation is a key problem solving technique for large, complex problems

  • A good way to learn more is to read about designs of large-scale systems


  • Login