CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Slides:



Advertisements
Similar presentations
This is Java Jeopardy Writing Methods…chapter 4…
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
CS 112 Introduction to Programming
Fields, Constructors, Methods
Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
3.5 Modular Programming Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · April.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Guide To UNIX Using Linux Third Edition
ASP.NET Programming with C# and SQL Server First Edition
Distribution of Marks Internal Sessional Evaluation Assignments – 10 Quizzes – 10 Class Participation Attendence – 5 Mid – Term Test – 25 External Evaluation.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Designing Data Types. 2 Object Oriented Programming Procedural programming. [verb-oriented] n Tell the computer to do this. n Tell the computer to do.
3.3 Designing Data Types Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · September.
Introduction to UML Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CS 112 Introduction to Programming Inheritance Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Our objectives: We will consider four thematic map types choropleth proportional symbol dot density cartograms understand decisions involved in classifying.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CS 112 Introduction to Programming User-Defined Data Types: Using static to hide OOP; Encapsulation Yang (Richard) Yang Computer Science Department Yale.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Puzzle 1  what does the following program print? public class Puzzle01 { public static void main(String[] args) { System.out.print("C" + "S" + "E"); System.out.println('1'
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter 9 - Classes with Class Members Class Variables Class Methods How to Access Class Members When to Use Class Members Class Constants Example Program.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
1 Red States, Blue States McCain Obama 2008 Presidential election.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
CS 112 Introduction to Programming Digital Audio; User-Defined Data Types (struct) Yang (Richard) Yang Computer Science Department Yale University 208A.
CS 112 Introduction to Programming Design Good Classes: Visualization and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.
CS 112 Introduction to Programming Object Relationship Analysis: Composition, Association, Inheritance Yang (Richard) Yang Computer Science Department.
CS 112 Introduction to Programming Class Inheritance Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CS 265 Jianbo Zhao. What is interface? Interface is a detailed boundary between code that provides a service and code that uses it. An interface defines.
Classes A class is a blueprint of an object
Chapter 4: Writing Classes
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Object Based Programming
Chapter 5 – Writing Classes
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
© A+ Computer Science - OOP © A+ Computer Science -
Professor John Canny Spring 2004 March 5
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
CPS120: Introduction to Computer Science
Presentation transcript:

CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:

2 Admin  Updated class schedule

3 Recap: Encapsulating Data Client Methods Data/state Client should not see the internal state or behaviors Client can see only the external API

Recap: Encapsulating Data  Benefit m Consistency: so that it is impossible for others to "reach in" and directly alter another object's state m Flexibility: so that you can change the state representation later without worrying about breaking others’ code

 Encapsulated data types. m Don't touch data and do whatever you want. m Instead, ask object to manipulate its data.  Lesson. Limiting scope makes programs easier to maintain and understand. Adele Goldberg Former president of ACM Co-developed Smalltalk Ask, Don't Touch "principle of least privilege" "Ask, don't touch."

6 Recap: Encapsulation/How r 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 m Members without an access modifier: default private accessibility, i.e., accessible in the same package; otherwise, not accessible.

7 Examples: Set the Access Modifiers  Coin  Ball  BankAccount  Point

8 Class Diagram Coin - face : int + flip() : void + isHeads() : boolean + toString() : String Above is a class diagram representing the Coin class. “-” indicates private data or method “+” indicates public data or method class name attributes methods

9 Outline  Admin and recap  Defining classes o Motivation and basic syntax o Examples o The encapsulation principle  An OOP example: geomap visualization

Data Visualization “ If I can't picture it, I can't understand it. ” — Albert Einstein 10 Edward Tufte. Create charts with high data density that tell the truth.

Visualization based on Geography Classes?

12 Major Classes and Relationship GeoMapRegion Polygon Color Point 1mm2 111m A composition relationship An association relationship

13 Major Classes and Relationship GeoMap Region String … Polygon Color Point …

14 Major Classes and Relationship GeoMapRegion Polygon Color Point 1mm2 111m A composition relationship An association relationship Design question: - Immutable objects?

15 Major Classes and Relationship GeoMapRegion Polygon Color Point 1mm2 111m A composition relationship An association relationship Design question: - What is the basic controller (of GeoMap) structure?

 Retrieve region m Batch: retrieve list of all regions m Specific: retrieve one specific region  Coloring m Map properties of a region to a color 16 Coloring Controller Structure

17 Major Classes and Relationship GeoMapRegion Polygon Color Point 1mm2 111m A composition relationship An association relationship Discussion: -Public methods (API) of Point

18 Major Classes and Relationship GeoMapRegion Polygon Color Point 1mm2 111m A composition relationship An association relationship Discussion: -Public methods (API) of 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(Scanner input) { 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() { … } … }

20 Major Classes and Relationship GeoMapRegion Polygon Color Point 1mm2 111m A composition relationship An association relationship Discussion: -Public methods (API) of Region

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() } … } Q: Should Region have a method that returns its internal Polygon? Even though most complexity is in Polygon, Polygon is not exposed. Region delegates tasks internally to Polygon.

Example Controllers  GeoMap.java  RandomColorMap.java  ClickColorMap.java  RedBlueMap.java

Purple America (PurpleMap.java)  Idea. [Robert J. Vanderbei] Assign color based on number of votes. m a 1 = Rep. votes. m a 2 = Other votes. m a 3 = Dem. votes. 100% Dem 100% Rep 100% Other 55% Dem, 45% Rep 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  Cartogram. Area of state proportional to number of electoral votes. Michael Gastner, Cosma Shalizi, and Mark Newman www-personal.umich.edu/~mejn/election

Cartograms  Cartogram. Area of country proportional to population.

 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 Summary