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

Slides:



Advertisements
Similar presentations
1 Inheritance Classes and Subclasses Or Extending a Class.
Advertisements

Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Chapter 13 – Introduction to Classes
 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
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
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.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
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.
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.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Classes / Objects An introduction to object-oriented programming.
Processing Dr Andy Evans. Processing MIT Media Lab Libraries for visualisation. Wraps a simple visualisation scripting language.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
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.
Graphics Concepts CS 2302, Fall /3/20142 Drawing Paths.
Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman.
Copyright © Curt Hill Turtles The beginning of media computation.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
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.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
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"
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.
Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays Evan.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Review Expressions and operators Iteration – while-loop – for-loop.
Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays (Oh My) Micah.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
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.
Chapter 1 Object Orientation: Objects and Classes.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Objects Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Concepts of Object Oriented Programming
Computer Graphics: Rocket, Java: Class
Emerging Platform#1: Processing 3
Chapter 8 Objects.
LCC 6310 Computation as an Expressive Medium
Defining Classes and Methods
Programming for Artists
Chapter 10 Algorithms.
Introduction to Object Oriented Programming
Chapter 10 Algorithms.
More programming with "Processing"
Chapter 8 Objects.
Lecture 11: Classes AP Computer Science Principles
Introducing JavaScript
Chapter 10 Algorithms.
Object-Oriented Programming and class Design
[Robert W. Sebesta, “Programming the World Wide Web
Object-Oriented Programming and class Design
Presentation transcript:

Objects

2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming Functions break a program into modules like an outline What is an Object? Usually based on a noun It has properties It has actions (methods) that it can do (verbs) An examples of an object that you know: String The dot operator is used to make the String do work!

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

4 Object Example: Human Attributes Age Height Weight Name Eye color Hair color … Functions Sleep Wake-Up Eat Ride something

5 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 its functions You have to have a plan before you can make an object: C lass before O bject

Exercise 8.1: Plan a Car Class Lets plan a simple Car List data and functions Lets compare how we could convert the function we wrote to show different card with objects Well use our setup() and draw() methods

Setup for a Car before Objects 7 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 cars location based on speed

Setup a Car using Objects 8 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 9 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-OOPOOP

Setup a Car using Objects - Functions 11 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 move() { // 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. 14

Car Class should really be in its own file… Click Right Arrow in tabs row Use New Tab in menu to create a new file Name the new tab exactly like the class = Car Move the code for the Car class to it Saves to a Car.pde file in your folder Keep both files in the same folder If you want to use your Car class in another project, simply copy the Car.pde file there

Your first multi-file Project!

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; }

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!