© Calvin College, 2009 1 What is object-oriented programming? My guess is that object-oriented programming will be in the 1980’s what structured programming.

Slides:



Advertisements
Similar presentations
© Calvin College, Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.
CHAPTER 13 Object Oriented Programming. Objectives  Design class definitions  Implement data hiding and encapsulation  Use accessor and mutator methods.
Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Software Testing and Quality Assurance
Classes and Objects April 6, Object Oriented Programming Creating functions helps to make code that we can reuse. When programs get large it becomes.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Java Classes Using Java Classes Introduction to UML.
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
PROCESSING & JAVA An Introduction to Computing. Objectives Be able to state and apply phases of development Be able to state and describe the goals of.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
© Calvin College, For a number of years I have been familiar with the observation that the quality of programmers is a decreasing function of the.
10-Nov-15 Java Object Oriented Programming What is it?
Chapter 4 Introduction to Classes, Objects, Methods and strings
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Objects. The Heart of the Matter - The "Black Box" Object-oriented software is all about objects. An object is a "black box" which receives and sends.
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.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Chapter More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism © 2011 | PN AZRINA.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Chapter 20 Concepts for Object-Oriented Databases Copyright © 2004 Pearson Education, Inc.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Advanced Object-Oriented Programming
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Classes and Objects 2nd Lecture
Classes and Objects Encapsulation
Encapsulation & Visibility Modifiers
Object initialization: constructors
Week 6 Object-Oriented Programming (2): Polymorphism
Building Java Programs
Building Java Programs
Dr. R Z Khan Handout-3 Classes
Building Java Programs
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

© Calvin College, What is object-oriented programming? My guess is that object-oriented programming will be in the 1980’s what structured programming was in the 1970’s. Everyone will be in favor of it. Every manufacturer will promote his products as supporting it. Every manager will pay lip service to it. Every programmer will practice it (differently). And no one will know just what it is - Tim Rentsch, SIGPLAN Notices 17(12), 1982, p. 51

© Calvin College, Introduction to Classes ● Example Example ● Introduction to Classes Introduction to Classes – Using Classes Using Classes – Designing Classes Designing Classes – Implementing Classes Implementing Classes – Further Iterations Further Iterations ● Object-Oriented Programming Languages Object-Oriented Programming Languages

© Calvin College, Example: Analysis (1) ● We’d like to display a shower with hundreds of rain drops. ● A sample image of this vision is shown here.

© Calvin College, Example: Analysis (1) ● The program should: – Represent a list of rain drops; – The rain drops should fall down the output pane;

© Calvin College, Iteration 0 ● Analysis ● Design ● Implementation ● Test

© Calvin College, /* raindropsNonOO draws one falling raindrop without using classes. * kvlinden, snelesen Fall 2010 */ final int WIDTH=400, HEIGHT=600; int dropX, dropY, dropDiameter, dropVelocity; void setup() { size(WIDTH, HEIGHT); background(255); smooth(); dropX = WIDTH / 2; dropY = 0; dropDiameter = 100; dropVelocity = 5; } void draw() { background(255); if (dropY <= height) { drawRaindrop(dropX, dropY, dropDiameter); } dropY += dropVelocity; } void drawRaindrop(int x, int y, int diameter) { fill(10, 10, 200, 100); stroke(0); arc(x, y, diameter, diameter, 0, PI); noStroke(); triangle(x, y - diameter*1.5, x - diameter/2, y, x + diameter/2, y); stroke(0); line(x, y - diameter*1.5, x - diameter/2, y); line(x, y - diameter*1.5, x + diameter/2, y); }

© Calvin College, Classes ● Classes encapsulate object types. ● In object-centered design we – Reuse old classes where possible – Build new classes when necessary ● Each new class that we design and build should have a coherent set of: – Knowledge – Responsibilities

© Calvin College, images from SubmarinesCarriers Battleships Supply Los Angeles Class: SSN Los Angeles SSN Baton Rouge Virginia Class: NSSN Virginia NSSN Texas Nimitz Class: CVN 68 - Nimitz CVN 69 - Eisenhower Kitty Hawk Class: CVA 63 - Kitty Hawk CVA 64 - Constellation Iowa Class: BB 61 - Iowa BB 62 - New Jersey Montana Class: BB 68 - Ohio BB 69 - Maine Green Valley Class: TAK Green Valley TAK Jeb Stuart Naval Ships Non-Combat Combat SubmergedSurface

© Calvin College, Classes and Objects ● Classes describe sets of similar objects by specifying their: – Attributes – Behaviors ● Each object has its own copies of the attribute values.

© Calvin College, Using Classes ● When you use a class object: – The calling program doesn’t know: How to initialize the object’s data How to implement the object’s methods – The object itself is responsible for these things. ● The calling program accesses them by calling predefined methods.

© Calvin College, Iteration 1 ● Analysis ● Design ● Implementation ● Test

© Calvin College, /** *... appropriate documentation... */ final int WIDTH = 400, HEIGHT = 600; Drop drop; void setup() { size(WIDTH, HEIGHT); background(255); smooth(); drop = new Drop(); } void draw() { background(255); drop.move(); drop.display(HEIGHT); }

© Calvin College, Designing Classes ● When we want to work with objects not supported by existing types, we must design a new class. ● The key design issues here are: – What classes do we need? – What goes in them (and what doesn’t)?

© Calvin College, Information Hiding ● When we design a class we distinguish: – the external interface to a class; – the internal implementation of the class. ● The principle of information hiding dictates that a class designer: – provide public views of those things that a class user really needs to know; – hide all other details by making them private.

© Calvin College, Design using Perspectives ● Use an external perspective to specify the public interface to a class. ● Use an internal perspective to specify the internals of the class design.

© Calvin College, Implementing Classes ● Implementing the class attributes Implementing the class attributes ● Implementing the class methods: – Constructors Default-value constructor Explicit-value constructor – Accessors Accessors – Mutators Mutators – Other Methods Other Methods – Object Interaction Object Interaction – Copy Constructors Copy Constructors

© Calvin College, Class Attributes ● Drop objects will certainly have to encapsulate their own: – x-y coordinates; – Diameter; ● These will be stored as instance variables, which means that each Drop object will have its own versions of these values.

© Calvin College, Implementing Class Attributes class Drop { private final color COLOR = color(10, 10, 200, 100); private float myX, MyY; // x-y coordinates private float myDiameter; // raindrop diameter private float myVelocity; // falling speed private color myColor; // other class stuff here… }

© Calvin College, Default-Value Constructor External View (in the driver): Drop drop1 = new Drop(); Internal View (in the class): /** * Construct a new Drop * with default values */ public Drop() { myX = 0; myY = 0; myDiameter = 5; myVelocity = 5; myColor = COLOR; } 0 5 drop1 0 myX myY myVelocity 5 myDiameter 10,10,200,100 myColor

© Calvin College, Constructors as Methods ● Constructors are like methods except that: – They have no return type – They are given the same name as the class – They are invoked with new: Drop drop = new Drop(); ● Constructors initialize instance variables within the limits set by the invariants. ● Constructor methods are often overloaded.

© Calvin College, Class Invariants ● Objects must maintain the integrity of their internal data. /** * Print a message & stop if the diameter is negative. diameter the value to check verified diameter value */ private float checkDiameter(float diameter) { if (diameter < 0) { println("invalid diameter: " + diameter); stop(); } return diameter; }

© Calvin College, Explicit-Value Constructor External View: Drop drop1 = new Drop(25, 50, 10, 3); Internal View: /** * Construct a new Drop x the x-coordinate y the y-coordinate diameter the non-negative diameter velocity the falling speed */ public Drop(float x, float y, float diameter, float velocity) { myX = x; myY = y; myDiameter = checkDiameter(diameter); myVelocity = velocity; myColor = COLOR; } 25 3 drop1 50 myX myY myVelocity 10 myDiameter 10,10,200,100 myColor

© Calvin College, Accessor Methods /** my x coordinate */ public float getX() { return myX; } /** my y coordinate */ public float getY() { return myY; } /** my diameter */ public float getDiameter() { return myDiameter; }

© Calvin College, /** * Change my y position based on my drop velocity */ public void move() { myY += myVelocity; } /** * Set a new diameter value diameter non-negative diameter value */ public void setDiameter(float diameter) { myDiameter = checkDiameter(diameter); } Mutator Methods

© Calvin College, /** * Draw myself on the output panel provided that I'm still visible. height the height of the output panel */ public void display(int height) { if (myY <= height) { fill(myColor); stroke(0); arc(myX, myY, myDiameter, myDiameter, 0, PI); noStroke(); triangle(myX, myY - myDiameter*1.5, myX - myDiameter/2, myY, myX + myDiameter/2, myY); stroke(0); line(myX, myY - myDiameter*1.5, myX - myDiameter/2, myY); line(myX, myY - myDiameter*1.5, myX + myDiameter/2, myY); } Other Methods

© Calvin College, /** * Suck up the volume of neighboring drops if they are close enough. * Don’t do this if I am already sucked dry. other reference to one other drop object */ public void combine(Drop other) { float otherRadius = other.getDiameter() / 2; if ((myDiameter != 0) && (dist(myX, myY, other.getX(), other.getY()) < (myDiameter/2 + otherRadius + COMBINE_FACTOR))) { myDiameter += sqrt(2) * otherRadius; other.setDiameter(0); } Object Interaction ● Sometimes objects must interact with each other.

© Calvin College, Copy Constructors ● We can’t “copy” objects as follows: Drop drop1 = new Drop(); Drop drop2 = drop1; This only copies the reference. ● Thus, we must write copy constructors: Drop drop1 = new Drop(); Drop drop2 = drop1.copy();

© Calvin College, Copying Objects /** a copy of myself */ public Drop copy() { return new Drop(myX, myY, myDiameter); }

© Calvin College, Iteration 2 ● Analysis ● Design ● Implementation ● Test

© Calvin College, /** * Raindrops models a number of falling raindrops. * cf. Dan Shiffman, chapter 10 * kvlinden august2009 */ final int DROP_COUNT = 1000, DROP_DIAMETER = 10, DROP_VELOCITY = 5; Drop[] drops; float phaseIn; // Allows program to add drop objects one at a time void setup() { size(400, 600); background(255); smooth(); drops = new Drop[DROP_COUNT]; for (int i = 0; i < drops.length; i++) { drops[i] = new Drop(random(DROP_DIAMETER, width - DROP_DIAMETER), DROP_DIAMETER, DROP_DIAMETER, DROP_VELOCITY); } phaseIn = 0.0; } void draw() { background(255); for (int i = 0; i < drops.length && i < phaseIn; i++) { drops[i].move(); drops[i].display(height); // Check for collosions with other drops. for (int j = 0; j < drops.length && j < phaseIn; j++) { if (i != j) { drops[i].combine(drops[j]); } phaseIn += 0.5; }

© Calvin College, Continued /** * Drop models a falling raindrop that can combine with other drops. * cf. Dan Shiffman, chapter 10 * kvlinden august, 2009 */ class Drop { private int COMBINE_FACTOR = 1; private float myX, myY, myDiameter, myVelocity; private color myColor; /** * Construct a Drop object with default values */ public Drop() { myX = 0; myY = 0; myDiameter = 5; myVelocity = 5; myColor = color(10, 10, 200, 100); } /** * Construct a Drop object x x-coordinate value y y-coordinate value diameter desired diameter (non-negative) velocity desired velocity */ public Drop(float x, float y, float diameter, float velocity) { myX = x; myY = y; myDiameter = checkDiameter(diameter); myVelocity = velocity; myColor = color(10, 10, 200, 100); } /** * Print a message and stop the program if diameter is negative. diameter the value to check verified diameter value */ private float checkDiameter(float diameter) { if (diameter < 0) { println("invalid diameter: " + diameter); stop(); } return diameter; }

© Calvin College, /** my x-coordinate value */ public float getX() { return myX; } /** my y-coordinate value */ public float getY() { return myY; } /** my diameter */ public float getDiameter() { return myDiameter; } /** * Set a new diameter value diameter non-negative diameter value */ public void setDiameter(float diameter) { myDiameter = checkDiameter(diameter); } /** my velocity value */ public float getVelocity() { return myVelocity; } /** c a new color value */ public void setColor(color c) { myColor = c; } /** * Change my y position based on my drop velocity */ public void move() { myY += myVelocity; } Continued

© Calvin College, /** * Draw myself on the output panel provided that I'm still visible. height the height of the output panel */ public void display(int height) { if (myY <= height) { fill(myColor); stroke(0); arc(myX, myY, myDiameter, myDiameter, 0, PI); noStroke(); triangle(myX, myY - myDiameter*1.5, myX - myDiameter/2, myY, myX + myDiameter/2, myY); stroke(0); line(myX, myY - myDiameter*1.5, myX - myDiameter/2, myY); line(myX, myY - myDiameter*1.5, myX + myDiameter/2, myY); } /** * Suck up the volume of neighboring drops if they are close enough. * Don’t do this if I am already sucked dry. other reference to one other drop object */ public void combine(Drop other) { float otherRadius = other.getDiameter() / 2; if ((myDiameter != 0) && (dist(myX, myY, other.getX(), other.getY()) < (myDiameter/2 + otherRadius + COMBINE_FACTOR))) { myDiameter += sqrt(2) * otherRadius; other.setDiameter(0); } /** a copy of myself */ public Drop copy() { return new Drop(myX, myY, myDiameter, myVelocity); }

© Calvin College, OO Programming Languages ● Simula – Ole-Johan Dahl and Kristen Nygaard – Developed in the mid-1960’s – Used as a simulation language ● Smalltalk – Xerox PARC project lead by Alan Kay – Developed in the early 1970’s – Used to support interactive computing What’s the Big Idea images from