Classes / Objects An introduction to object-oriented programming.

Slides:



Advertisements
Similar presentations
Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
Advertisements

 Functions breakdown: Functions purpose Modularity Declaring and defining a function Calling a function Parameter passing Returning a value Reusability.
Emerging Platform#5: Processing 2 B. Ramamurthy 6/13/2014B. Ramamurthy, CS6511.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Lesson Four: More of the Same
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
IAT 800 Lecture 4. Sept 18, Fall 2006IAT 8002 Outline  Programming concepts –Methods –Classes  Talk about project 1  Reading: Read Chapters 1-4 of.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
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.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/6/08.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
CS107 References and Arrays By Chris Pable Spring 2009.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Working With Objects Tonga Institute of Higher Education.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
CPS120: Introduction to Computer Science Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
CIS 3.5 Lecture 2.2 More programming with "Processing"
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
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.
Lesson Two: Everything You Need to Know
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Working With Objects Tonga Institute of Higher Education.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Introduction to Object-Oriented Programming Lesson 2.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Review Expressions and operators Iteration – while-loop – for-loop.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
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.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/20/08.
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.
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.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Chapter 1 Object Orientation: Objects and Classes.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
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
Chapter 8 Objects.
Chapter 7 Functions.
Defining Classes and Methods
Chapter 10 Algorithms.
Chapter 8 Objects.
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Object-Oriented Programming and class Design
Presentation transcript:

Classes / Objects An introduction to object-oriented programming

What is object- oriented programming?  Object-oriented programming (OOP) allows you, the programmer, to create objects  You have been using objects all along, you just weren’t aware of it …  Every time you call a rect(); or an ellipse();, you are calling an object called an ellipse. The ellipse has properties (x,y,w,h)

Why Objects?  OOP allows you to put data about something and actions it can do together into one “object”  OOP has been around since the 1960s  Most popular current languages have either adapted or were designed with OOP  C  C++  C#  JavaScript  Java … and Processing

Human example Attributes  Age  Height  Weight  Name  Eye color  Hair color  … Functions  Sleep  Wake-Up  Eat  Ride something

Classes and Objects A Class  A ‘plan’ for an object: Cookie Cutter  The general idea of a thing  Has placeholders for details, but not ‘made’ yet  It declares functions that could be done to an object An Object  An ‘instance’ of a Class: A Cookie  A ‘real’ thing that has all of the specifics  It can be ‘told’ to execute it’s functions You have to have a ‘plan’ before you can make an object: C lass before O bject

Plan a Car Class  Let’s plan a simple Car  List data and functions Let’s compare how we could convert the function we wrote to show different cars with objects We’ll use our setup() and draw() methods

Setup for a ‘Car’ before Objects  Use global variables for ‘properties’  Color: carColor  Location: carX, carY  Speed: carSpeed  In setup()  Set the carColor  Set the starting location  Set the initial speed  In draw()  Fill background  Display car at location with color (may use a function)  Increment car’s location based on speed

Setup multiple ‘Cars’ before Objects  Use global arrays of variables for ‘properties’  Allows us to use a loop to initialize a number of Cars  Declare Color array: carColor [ ]  Declare carX array: carX [ ]  Declare carY array: carY [ ]  Declare carSpeed array: carSpeed [ ]  In setup()  Set and initialize the arrays  In draw()  Fill background  Loop through all the cars  Display car at location with color (may use a function)  Increment car’s location based on speed

Setup a ‘Car’ using Objects  One big difference with objects is that you move all of the global variables inside the Car object  Color: carColor  Location: carX, carY  Speed: carSpeed  Car object instead! We end up with one variable to represent the car Instead of initializing all of those variables, we initialize the car object!

Setup a ‘Car’ using Objects  Outside of all methods (global)  Declare a ‘parking place’ for a car  In setup()  Make a new car object  based on the ‘Car’ plan)  Sets initial values for color, location and speed  In draw()  Fill background  Tell the car to ‘move’ based on speed  Tell the car to ‘display’ itself (call a function)

The ‘Car’ class  Convert the non-OOP Car Data to a Class Non-OOP OOP

Setup a ‘Car’ using Objects - Data  Let’s break it down step by step  Declare the plan for the ‘Car’  Outside draw() and setup()  Put variables for color, location and speed inside

Setup a ‘Car’ using Objects - Constructor  We need to write instructions to build a car  It is called the ‘Constructor’ method  Move the code that was in setup()  Set variables for color, location and speed inside

Setup a ‘Car’ using Objects - Functions  Move the functions that did things to the car  Move the code to inside the Car class  The will be called methods of the class  Move the code that was in display()and drive()

The whole ‘Car’ (Example 8.1) class Car { // Define a class for a car color c; // Variables. float xpos; float ypos; float xspeed; Car() { // A constructor. c = color(175); xpos = width/2; ypos = height/2; xspeed = 1; } void display() { // Function. // The car is just a square rectMode(CENTER); stroke(0); fill(c); rect(xpos,ypos,20,10); } void drive() { // Function. xpos = xpos + xspeed; if (xpos > width) { xpos = 0; }

What was that about a ‘Constructor’?  A constructor is a special method of a class  Has the same name as the class  “Builds’ the object  Sets initial values  It is ‘called’ with you use new : class Car { Car() { // Constructor c = color(175); xpos = width/2; ypos = height/2; xspeed = 1; } void setup() { size(200,200); // Initialize Car object myCar = new Car(); }

And here is the OOPized version  Is the OOP version shorter?  Is it easier to read?  Not yet maybe, but soon, and for the rest of your life.

What if we want to make more cars?  Right now, all cars are exactly the same  Their constructor sets up the color, location…  How could we make ‘custom’ cars?  Remember parameters?  What if a Car constructor took parameters? class Car { Car(color colp, int xp, int yp, int speedp) { c = colp; xpos = xp; ypos = yp; xspeed = speedp; }

Please review parameter passing…  Make a new frog with a length of 100

Now we can make two ‘custom’ cars  Use your new ‘parameterized’ constructor!

And imagine an array of Cars!  You can use the ‘Car’ class just like any other type  Declare an array of our new Cars object: Car [ ] parkingLot; setup() { parkingLot = new Car[10]; }  But wait… what just happened?  Did you create 10 cars? No, not yet.  You created 10 parking stalls for cars  So we still have to ‘build’ the cars and set all of the colors, locations and speeds… parkingLot[0] = new Car(color, x, y..);

Filling the parking lot the easy way!  Once you have the parking lot created, Car [ ] parkingLot; setup() { parkingLot = new Car[10]; }  Use a for loop to make a bunch of cars! for (int i; i < 10; i++) { parkingLot[i] = new Car(color, x, y..); }  Arrays and loops work wonderfully together!  And they are even more fun with objects!

Summary  Objects provide a different way of designing programs  Combine data and functionality  A class is a ‘plan’ for an object  An object is an ‘instance’ of a class  The ‘new’ keyword is used to instantiate (create) an object from a class  Use the ‘dot’ operator ‘.’ to access member data and functions of the class  Classes are normally written in separate files  One class per file  Put the class ‘source’ file in the same folder as the ‘main’