Chapter 8 Objects.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
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.
Lesson Four: More of the Same
OOP - Object Oriented Programming Object Oriented Programming is an approach to programming that was developed to make large programs easier to manage.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Road Map Introduction to object oriented programming. Classes
Chapter 14: Overloading and Templates
Loops We have been using loops since week 2, our void draw(){ } is a loop A few drawbacks of draw() –It is endless –There is only one draw() –It updates.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes / Objects An introduction to object-oriented programming.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
ITP 104.  While you can do things like this:  Better to use styles instead:
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Agenda Object Oriented Programming Reading: Chapter 14.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
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.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Introduction to Object-Oriented Programming Lesson 2.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Unit 3 Lesson 5 Strings and the String Class Mr. Dave Clausen (modifications from the textbook)
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
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.
Review Objects – data fields – constructors – Methods Classes.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Objects Art &Technology, 3rd Semester Aalborg University Programming David Meredith
OOP - Object Oriented Programming
Classes C++ representation of an object
Programming with ANSI C ++
Concepts of Object Oriented Programming
Building Java Programs
Introduction to Object-oriented Program Design
Chapter 7 Functions.
Lecture 4-7 Classes and Objects
Chapter 7 Functions.
Code Elements and Processing Coordinate System
Object-oriented Design in Processing
Chapter 10 Algorithms.
Learning Objectives Classes Constructors Principles of OOP
Constructors and Other Tools
Announcements Mid-Term Exam!! Quiz 5 Again + Quiz 6 Exercise 5
Chapter 10 Algorithms.
Code Elements and Processing Coordinate System
Classes and Objects.
Object-oriented Design in Processing
Chapter 8 Objects.
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
Classes CS 21a: Introduction to Computing I
Lecture 6 Methods and Classes
Object-oriented Design in Processing
Chapter 10 Algorithms.
Review of Previous Lesson
Chapter 4, Variables Brief Notes
Object-oriented Design in Processing
Lecture 6 Methods, Classes, Inheritance
Object-Oriented Programming and class Design
Presentation transcript:

Chapter 8 Objects

In Chapter 8 you will learn: How to put data and functionality together Definitions of object and class How to write classes How to create objects How to use tabs to separate info

Object-oriented programming (OOP) … is a programming model constructed around objects. It separates data into objects and describes object contents and behavior through the declaration of classes (methods).

Everyday objects In Processing, we’ve been using all along (variables, functions, loops, etc. Now will put together more efficiently. In life, YOU are an object. - name - characteristics - actions A book is an object. A plant is an object. A ball is an object. Etc.

Moving closer to programming… An object’s template is considered a class. For instance, each car can have different characteristics and functions. A class acts as a category of objects. (defines all the common properties of the different objects that belong to it.)

Definition of Class A body of code that exists independently of the main body of code from which it is referenced. A class is similar to a function, but usually has more than a singular purpose. It can have many functions.

We are going to work in reverse order by discussing how to use and object before how to make one. Reason is that this kind of code is more like what you’ve been writing.

Using an object requires 3 steps: Declare the object variable Initialize an object Call the method (or use it)

A simple sketch with an item that displays and moves, might look like this. Car myCar;   void setup() { myCar = new Car(); } void draw() { background(255); myCar.move(); myCar.display();

Creating the class Each class must have 4 elements Name – Data – Tip: If new to OOP, start with an example without objects, then rewrite with objects. No functional changes. KISS. (i.e. keep it short scholar) Each class must have 4 elements Name – Data – Constructor – Methods –

Details of 4 Parts Name – Good to start with caps to distinguish from variables. Use camelCase naming. Then start curly bracket. Data – a collection of variables. Each instance of an object contains said variables. Constructor – a special function inside of a class that creates the instance of the object. Here you give instructions on setting up. When called, it uses the new operator , as in: Car myCar = new Car(); Methods – add all kinds of functionality such as name, return type, arguments, & code body.

A look at details of using an object Declaring an object variable. Actually a variable…of complex type of data. - holds both variables and functions. - unlike primitive variables that hold only one data type. Initialize it. initialize it by giving it a value. - Must construct object with the new operator. - This line calls the constructor; and includes all of object’s variables. - Remember primitives can be initialized without value. What happens if you don’t initialize an object? Using an object. This involves calling functions that are built into that object. AKA methods. - Also, notice the dot notation syntax. variableName.objectMethod(method arguments) ;

Example 8.1 puts all together What does author say about where to place the class? A superfluous explanation of Example 8.1 , starting with the creation of the class. Define a class Using an object A class is created (preferably in a separate tab) of a simple car. #1 The class is called Car. #2. It has 4 variables: c, xpos, y and yspeed. #3 The constructor is created where the variables are initialized or given values. #4. Two functions – display() and move() are created. On the main screen: #1 A Car object is declared as a global variable. The object is called myCar, like this: Car myCar; #2 Inside setup, the car object is initialized as MyCar = new Car(); #3 Inside draw, both the move() & display() methods are called, using the dot syntax notation, like this: myCar.display()

A Fish Instead Instead of the car we created a fish, using the exact steps Define a class Using an object //The data or variables class Fish { float fishX ; float fishY ; float fishSize ; //The constructor Fish() { fishX = 60; fishY= 170; fishSize = 80; } //The functionality or methods void display() { //in order of: tail, body, and eye. All based on body. triangle(fishX-30,fishY, fishX-50,fishY + 15, fishX-50,fishY-15); ellipse(fishX, fishY, fishSize, fishSize/2); ellipse(fishX+30, fishY, 10, 10); void swim() { fishX = fishX + 1; if(fishX > width) { fishX = 0; } Fish tastyFish; void setup() { size(500,250); tastyFish = new Fish(); } void draw() { background(200); tastyFish.display() ; tastyFish.swim();

Constructor Arguments So far, only one object was initialized Suppose you wanted several items in different locations, sizes, and/or colors?

Answer: Rewrite constructor to accept arguments. See example 8.2 Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { c = tempC; xpos = tempXpos; ypos = tempYpos; xspeed = tempXspeed; } Guidelines for temp variable name: Any name is OK, such as “temp” or “x_” Be consistent

Parameter passing example: Their sole purpose is to pass value from where object is made to where it is used.

A non-OOP fish //Better because of fishYtwist rather than entire Y moving. float fishX= 60; float fishY= 170; int fishSize= 80; float ftail = 170; float fishYtwist = ftail ; float spd = .5; //the speed void setup() { size(500,250); background(#2A9BF5); } void draw() { //the water fill(#55B8C9); rect(0, 130, width, 150); //the fish fill(255); noStroke(); //the right corner of triangle will twist triangle(fishX-30, fishYtwist, fishX - 50, ftail+15,fishX - 50, ftail-15); ellipse(fishX, fishY,fishSize, fishSize/2); fill(200,230, 0); ellipse(fishX+30, fishY, 10, 10); //fish swim with tail movement fishX = fishX +spd; if (fishX>width) { fishX = 0; ftail = fishYtwist + random(-5,5) ; Challenge, but not to submit: Using example 8-2 as a model, use OOP to make 2 fish. If you want to simplify, get rid of the tail.