3.3 Designing Data Types Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · September.

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 9 Thinking in Objects.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Ranga Rodrigo. Class is central to object oriented programming.
Object Oriented Software Development
Designing Data Types. 2 Object Oriented Programming Procedural programming. [verb-oriented] n Tell the computer to do this. n Tell the computer to do.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
CS 112 Introduction to Programming User-Defined Data Types Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.
2.2 Libraries and Clients Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Salman Marvasti Sharif University of Technology Winter 2015.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS 112 Introduction to Programming Digital Audio; User-Defined Data Types (struct) Yang (Richard) Yang Computer Science Department Yale University 208A.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
CS 112 Introduction to Programming Design Good Classes: Visualization and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Object Oriented Development, Abstraction & Inheritance
Object-Oriented Programming
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
University of Central Florida COP 3330 Object Oriented Programming
Chapter 10 Thinking in Objects
Object Oriented Programming using Java - Class Instance Variables
About the Presentations
University of Central Florida COP 3330 Object Oriented Programming
Object Based Programming
Chapter 9 Thinking in Objects
Classes and Data Abstraction
IFS410: Advanced Analysis and Design
Object-Oriented Programming
MSIS 655 Advanced Business Applications Programming
Building Java Programs
Object-Oriented Programming Using C++ Second Edition
Chapter 9 Thinking in Objects
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
Object-Oriented Programming
Building Java Programs
Review of Previous Lesson
CPS120: Introduction to Computer Science
Building Java Programs
Threads and concurrency / Safety
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

3.3 Designing Data Types Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · September 2, :56 tt

2 Object Oriented Programming Procedural programming. [verb-oriented] n Tell the computer to do this. n Tell the computer to do that. Alan Kay's philosophy. Software is a simulation of the real world. n We know (approximately) how the real world works. n Design software to model the real world. Objected oriented programming (OOP). [noun-oriented] n Programming paradigm based on data types. n Identify things that are part of the problem domain or solution. n Things in the world know things: instance variables. n Things in the world do things: methods.

3 Alan Kay Alan Kay. [Xerox PARC 1970s] n Invented Smalltalk programming language. n Conceived Dynabook portable computer. n Ideas led to: laptop, modern GUI, OOP. Alan Kay 2003 Turing Award "The computer revolution hasn't started yet." "The best way to predict the future is to invent it." "If you don't fail at least 90 percent of the time, you're not aiming high enough."

4 Encapsulation Bond. What's your escape route? Saunders. Sorry old man. Section 26 paragraph 5, that information is on a need-to-know basis only. I'm sure you'll understand.

5 Encapsulation Data type. Set of values and operations on those values. Ex. int, String, Complex, Vector, Document, GuitarString, Tour, … Encapsulated data type. Hide internal representation of data type. Separate implementation from design specification. n Class provides data representation and code for operations. n Client uses data type as black box. n API specifies contract between client and class.

6 Intuition ClientAPI - volume - change channel - adjust picture - decode NTSC signal Implementation - cathode ray tube - electron gun - Sony Wega 36XBR pounds Implementation and client need to agree on API ahead of time. client needs to know how to use API implementation needs to know what API to implement

7 Intuition API - volume - change channel - adjust picture - decode NTSC signal Implementation - gas plasma monitor - Pioneer PDP-502MX - wall mountable - 4 inches deep Can substitute better implementation without changing the client. Client client needs to know how to use API implementation needs to know what API to implement

8 Counter Data Type Counter. Data type to count electronic votes. Legal Java client. Oops. Al Gore receives -16,022 votes in Volusia County, Florida. public class Counter { public int count; public final String name; public Counter(String id) { name = id; } public void increment() { count++; } public int value() { return count; } } Counter c = new Counter("Volusia County"); c.count = ;

9 Counter. Encapsulated data type to count electronic votes. Does not compile. Benefit. Can guarantee that each data type value remains in a consistent state. public class Counter { private int count; public final String name; public Counter(String id) { name = id; } public void increment() { count++; } public int value() { return count; } } Counter Data Type Counter c = new Counter("Volusia County"); c.count = ;

10 Changing Internal Representation Encapsulation. Keep data representation hidden with private access modifier. Expose API to clients using public access modifier. Advantage. Can switch internal representation without changing client. Note. All our data types are already encapsulated! public class Complex { private final double re, im; public Complex(double re, double im) { … } public double abs() { … } public Complex plus(Complex b) { … } public Complex times(Complex b) { … } public String toString() { … } } e.g., to polar coordinates

11 Time Bombs Internal representation changes. n [Y2K] Two digit years: January 1, n [Y2038] 32-bit seconds since 1970: January 19, n [VIN numbers] We'll run out by Lesson. By exposing data representation to client, need to sift through millions of lines of code in client to update.

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

Immutability

14 Immutability Immutable data type. Object's value cannot change once constructed.

15 Immutability: Advantages and Disadvantages Immutable data type. Object's value cannot change once constructed. Advantages. n Avoid aliasing bugs. n Makes program easier to debug. n Limits scope of code that can change values. n Pass objects around without worrying about modification. Disadvantage. New object must be created for every value.

16 Final Access Modifier Final. Declaring an instance variable to be final means that you can assign it a value only once, in initializer or constructor. Advantages. n Helps enforce immutability. n Prevents accidental changes. n Makes program easier to debug. n Documents that the value cannot not change. public class Counter { private final String name; private final int maxCount; private int count;... } this value changes by invoking instance method these values don’t change once the object is constructed

Spatial Vectors

18 Vector Data Type Set of values. Sequence of real numbers. [Cartesian coordinates] API. x = (0, 3, 4, 0), y = (0, -3, 1, -4) x + y = (0, 0, 5, -4) 3x = (0, 9, 12, 0) x  y = (0  0) + (3  -3) + (4  1) + (0  -4) = -5 | x | = ( ) 1/2 = 5 x = x / | x | = (0, 0.6, 0.8, 0)

19 Vector Data Type Applications Relevance. A quintessential mathematical abstraction. Applications. n Statistics. n Linear algebra. n Clustering and similarity search. n Force, velocity, acceleration, momentum, torque. n …

20 Vector Data Type: Implementation public class Vector { private int N; private double[] coords; public Vector(double[] a) { N = a.length; coords = new double[N]; for (int i = 0; i < N; i++) coords[i] = a[i]; } public double dot(Vector b) { double sum = 0.0; for (int i = 0; i < N; i++) sum += (coords[i] * b.coords[i]); return sum; } public Vector plus(Vector b) { double[] c = new double[N]; for (int i = 0; i < N; i++) c[i] = coords[i] + b.coords[i]; return new Vector(c); } constructor instance variables methods

21 This. The keyword this is a reference to the invoking object. Ex. When you invoke a.magnitude(), this is an alias for a. public Vector times(double t) { double[] c = new double[N]; for (int i = 0; i < N; i++) c[i] = t * coords[i]; return new Vector(c); } public double magnitude() { return Math.sqrt(this.dot(this)); } public Vector direction() { return this.times(1.0 / this.magnitude()); }... Vector Data Type: Implementation