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.

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
When is Orientated Programming NOT? Mike Fitzpatrick.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Code Elements and Processing Coordinate System. Code elements: pages Comments: are documentation notes that are ignored by the computer but are.
Emerging Platform#5: Processing 2 B. Ramamurthy 6/13/2014B. Ramamurthy, CS6511.
Lesson Four: More of the Same
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.
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Road Map Introduction to object oriented programming. Classes
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Structures/Classes CS 308 – Data Structures. What is a structure? It is an aggregate data type built using elements of other types. Declaring a structure.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Classes and Objects in Java
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Classes / Objects An introduction to object-oriented programming.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
G RAPHICS P ROGRAMMING Lecture 3 - Simple Animation - Simple 3D Drawing.
Chapter 2 Practice.
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
OBJECTS AND CLASSES CITS1001. Concepts for this lecture class; object; instance method; parameter; signature data type multiple instances; state method.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Information System Design (IT60105) Lecture 26 Object-Oriented System Testing.
B. RAMAMURTHY Simulating Motion and Implementing Animation.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Arrays. An array is a collection “The Dinner offers an array of choices.” In computer programming, an array is a collection of variables of the same data.
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Salman Marvasti Sharif University of Technology Winter 2015.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Words. Characters and Strings Character –A single character inside of single quotes char letter = 'A' ; char digit = '0' ; – Strings Zero or more character.
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Welcome back!. Object Oriented Programming – Encapsulation Classes encapsulate state (fields) and behavior (methods) – Polymorphism Signature Polymorphism.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Unit 3 Lesson 5 Strings and the String Class Mr. Dave Clausen (modifications from the textbook)
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Review Objects – data fields – constructors – Methods Classes.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
Arrays. 2 Why do we care (about arrays)? What if you have a whole bunch of cars (or aliens or balls or ???) bouncing around the screen? How do we keep.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:
Some of Chap 17.
Chapter 8 Objects.
More programming with "Processing"
Chapter 8 Objects.
Java Programming Language
Chapter 10 Algorithms.
Trigonometry & Random March 2, 2010.
How About Some PI? Trigonometry Feb 18,2009.
Presentation transcript:

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 messages (i.e., function calls). A black box actually contains methods (functions) and data (variables) which the methods operate on. In o-o (object-oriented) programming, methods and data are merged into a single indivisible thing -- an object.

Messages Why the "black box" metaphor for an object? –A primary rule of object-oriented programming is that: as the user of an object, you should never need to peek inside the box! All communication to it is done via method calls. –So you shouldn't have to know anything about what is in the black box in order to use it. Methods are functions.

Encapsulation As long as you deal with objects as black boxes via their methods (i.e., calling their functions), the software is guaranteed to work. Providing access to an object only through its methods, while keeping the details of the code and the data (i.e., the variables) secret is called information hiding. An equivalent term is encapsulation.

Classes How are objects defined? An object is defined via its class, which determines everything about an object. Objects are individual instances of a class. –For example, you may create an object call Spot from class Dog. The Dog class defines what it is to be a Dog object. –You can make more than one object of the Dog class, and call them Spot, Fido, Rover, etc.

“Apple” example from your text methods Variables (fields)

A Representation Highlighting Encapsulation

Defining a Class: the Spot Class Defining a class is like creating your own data type. String, PImage and PFont are all data types defined by classes. The Spot class will produce a white “dot” along with any needed behaviors. What data do we need to define a “spot” –x, y position –diameter

A Spot Without a Class int x = 33; int y = 50; int diameter = 30; void setup() { size(100, 100); smooth(); noStroke(); } void draw() { background(0); ellipse(x, y, diameter, diameter); }

A Class Without Methods Spot sp; // Declare the object void setup() { size(100, 100); smooth(); noStroke(); sp = new Spot(); // Construct the object sp.x = 33; // Assign 33 to the x field sp.y = 50; // Assign 50 to the y field sp.diameter = 30; // Assign 30 to the diameter field } void draw() { background(0); ellipse(sp.x, sp.y, sp.diameter, sp.diameter); } class Spot { // the Spot class float x, y; // The x- and y-coordinate float diameter; // Diameter of the circle }

A display() Method Spot sp; // Declare the object void setup() { size(100, 100); smooth(); noStroke(); sp = new Spot(); // Construct the object sp.x = 33; sp.y = 50; sp.diameter = 30; } void draw() { background(0); sp.display(); // calling the display() method } class Spot { float x, y, diameter; void display() { // ç the display method ellipse(x, y, diameter, diameter); }

Adding a constructor Spot sp; // Declare the object void setup() { size(100, 100); smooth(); noStroke(); sp = new Spot(33, 50, 30); // Construct the object } void draw() { background(0); sp.display(); } class Spot { // Class definition float x, y, diameter; Spot(float xpos, float ypos, float dia) {// constructor method x = xpos; // Assign 33 to x y = ypos; // Assign 50 to y diameter = dia; // Assign 30 to diameter } void display() { ellipse(x, y, diameter, diameter); }

Encapsulation revisited The Spot class was not really encapsulated until we add the constructor. Without the constructor, the user of the class had to know how the data was stored to use the class

The move() Behavior or Method class Spot { float x, y; // X-coordinate, y-coordinate float diameter; // Diameter of the circle float speed; // Distance moved each frame int direction = 1; // Motion Direction(1 is down, -1 is up) Spot(float xpos, float ypos, float dia, float sp) { // Constructor x = xpos; y = ypos; diameter = dia; speed = sp; } void move() { y += (speed * direction); if ((y > (height - diameter / 2)) || (y < diameter / 2)) { direction *= -1; } void display() { ellipse(x, y, diameter, diameter); }

Many Spots Spot sp1, sp2, sp3; // Declare the objects void setup() { size(100, 100); smooth(); noStroke(); sp1 = new Spot(20, 50, 40, 0.5); // Construct sp1 sp2 = new Spot(50, 50, 10, 2.0); // Construct sp2 sp3 = new Spot(80, 50, 30, 1.5); // Construct sp3 } void draw() { fill(0, 15); rect(0, 0, width, height); fill(255); sp1.move(); sp2.move(); sp3.move(); sp1.display(); sp2.display(); sp3.display(); }

Spot Arrays “main” program Spot class Try making a Square class that is similar to the Spot class. Create 2 Square objects and cause them to display.