Object-Oriented Programming in Processing

Similar documents
Object-Oriented Programming Concepts

The Processing Programming Environment. By: Richard Baldwin

In this chapter: What is an array? Declaring an array. Initialization. Array operations using the for loop with an array. Arrays of objects.

If the ball goes off either the right or left edge, turn the ball around. If x is greater than width or if x is less than zero, reverse speed.

1 Getting started with Processing

Java Object Oriented Design. CSC207 Fall 2014

CS1004: Intro to CS in Java, Spring 2005

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Notes from the Boards Set # 5 Page

Fundamental Concepts and Definitions

And Even More and More C++ Fundamentals of Computer Science

Art, Randomness, and Functions

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Design Programming DECO2011

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Defining Classes and Methods

CISC 1600 Lecture 3.1 Introduction to Processing

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Binghamton University. CS-211 Fall Object Orientation

Object Visibility: Making the Necessary Connections

Interaction Design A.A. 2017/2018

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

CSSE 220 Day 13. Encapsulation Coupling and Cohesion Scoping. Please download EncapsulationExamples from your SVN

CS 231 Data Structures and Algorithms, Fall 2016

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CS121: Computer Programming I

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

1.00 Lecture 8. Using An Existing Class, cont.

Object Oriented Programming

Chapter 4: Writing Classes

Consolidation and Review

CS Problem Solving and Object-Oriented Programming

Pointers, Arrays and Parameters

COMP 102 : Test. 2017, Apr 3

Introduction to Java Unit 1. Using BlueJ to Write Programs

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer

Processing/Java Syntax. Classes, objects, arrays, and ArrayLists

Defining Classes and Methods

CPS122 Lecture: Defining a Class

CSE 100: C++ TEMPLATES AND ITERATORS

Java Classes - Using your classes. How the classes you write are being used

Lecture 5: Methods CS2301

Smaller, simpler, subcomponent of program Provides abstraction

Express Yourself. Writing Your Own Classes

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COEN244: Class & function templates

5.6.1 The Special Variable this

Intro. Scheme Basics. scm> 5 5. scm>

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

Variables One More (but not the last) Time with feeling

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Midterms Save the Dates!

AP Computer Science Unit 1. Writing Programs Using BlueJ

Assignment 3 Functions, Graphics, and Decomposition

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Recursion. Contents. Steven Zeil. November 25, Recursion 2. 2 Example: Compressing a Picture 4. 3 Example: Calculator 5

Transcriber(s): Aboelnaga, Eman Verifier(s): Yedman, Madeline Date Transcribed: Fall 2010 Page: 1 of 9

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Appendix: Common Errors

Midterms Save the Dates!

What is a variable? A named locajon in the computer s memory. A variable stores values

Syntax Errors; Static Semantics

CS 101 Functions. Lecture 15

CS112 Lecture: Working with Numbers

QUIZ Friends class Y;

Repetition is the reality and the seriousness of life. Soren Kierkegaard

BM214E Object Oriented Programming Lecture 8

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes

Interaction Design A.A. 2017/2018

We will start our journey into Processing with creating static images using commands available in Processing:

Recall that creating or declaring a variable can be done as follows:

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Lecture 02, Fall 2018 Friday September 7

CS558 Programming Languages

Introduction to Computer Science I

Languages. Solve problems using a computer, give the computer instructions. Remember our diaper-changing exercise?

2. [20] Suppose we start declaring a Rectangle class as follows:

Praktikum: Entwicklung interaktiver eingebetteter Systeme

Defining Classes and Methods

Topic Notes: Java and Objectdraw Basics

CS 376b Computer Vision

What is a Class? Short Answer. Responding to Messages. Everything in Python is an Object 11/8/2010

Chapter 1: Object-Oriented Programming Using C++

CpSc 1111 Lab 4 Formatting and Flow Control

Transcription:

Object-Oriented Programming in Processing

Object-Oriented Programming We ve (kinda) been doing this since Day 1: Python is a deeply object oriented language Most of the data types we were using (strings, list, dictionaries) were objects Now, going to shift gears to exploring more about what objects are, how/when to use them Explore examples in Processing Much of this knowledge will be transferrable when we start Java CS 6452: Prototyping Interactive Systems 2

Objects Objects are simply data+functionality, in a self-contained unit In programming terms, objects have variables associated with them (sometimes called instance variables or member data) functions associated with them (called methods) 3

Classes Classes are the blueprints for an object Think of them like a cookie cutter or a template Can create multiple copies from the pattern Each object that is created based on a class s blueprint is called an instance of that class (Classes are like the abstract description of the thing that becomes an object) Class = cookie cutter Object/instance = cookie 4

Example Let s say we want to write a program that moves a car horizontally across the screen (We ll just use a simple rectangle for the car to make our job easier) What information will we need to keep track of for our car? color, location (x & y), speed What functionality pertains to our car? Draw the car on the screen, change its location based on its speed, initialize color/location/etc. 5

Doing it without OO (pseudocode) Global variables Car color Car X & Y location Car speed Setup code: Initialize car color Initialize car location to starting point Initialize car speed Draw code: Fill background Display car at current location, with current color Increment car s location by speed 6

Doing it without OO color c = color(0); float x = 0; float y = 100; float speed = 1; void setup() { size(200,200); void draw() { background(255); move(); display(); void move() { x = x + speed; if (x > width) { x = 0; void display() { fill(c); rect(x,y,30,10); 7

That s a lot of stuff spread all over our program! In an OO development style: Take all of the variables and functions out of the main program Put them inside a car object Car object keeps track of its data Color, location, speed Also handles the stuff it can do via methods Display it, drive it, etc. 8

Pseudocode for the new version Global variables in our program: The car object Setup code: Initialize the car object Draw code: Fill the background Display the car object Drive the car object 9

Code for the new version Car mycar; void setup() { mycar = new Car(); void draw() { background(255); mycar.drive(); mycar.display(); Simpler, and less to keep track of 10

Defining the class Before we can create a Car object, we have to define its class! Write the cookie cutter All classes have four parts to them: The class s name The data associated with the class (variables) A constructor function (run at initialization time) The functions associated with the class (its methods) 11

12

Declaring and Using Objects Declaring a normal variable: int var; Declaring an object: Car car; Initializing a normal variable: var = 10; Initializing an object: car = new Car(); // use the new operator // this automatically runs the constructor 13

Putting it all together // Class definition class Car { color c; float xpos; float ypos; float xspeed; Car() { c = color(0); xpos = width/2; ypos = height/2; xspeed = 1; void display() { rectmode(center); fill(c); rect(xpos, ypos, 20, 10); void drive() { xpos = xpos + xspeed; if (xpos > width) { xpos = 0; // Main code Car mycar; void setup() { size(200,200); mycar = new Car(); void draw() { background(255); mycar.drive(); mycar.display(); 14

Haven t we just moved the code around? Well yeah. But this version is still better: Once we get Car working, we can forget about it CS concept: encapsulation - treat the code as a black box without having to worry about how it works Each instance is isolated from every other instance We can add new cars with only minor changes to the code! Think about what a mess this would be with our original, non-oo version 15

Multiple cars Car mycar1; Car mycar2; // Two objects! void setup() { size(200,200); // Parameters go inside the parentheses when the object is constructed. mycar1 = new Car(color(255,0,0),0,100); mycar2 = new Car(color(0,0,255),0,10); void draw() { background(255); mycar1.drive(); mycar1.display(); mycar2.drive(); mycar2.display(); 16

Parameters in constructors To make the above code work we need our cars to look different Can add parameters to our constructors, used to initialize different cars differently Car(color tempc, float tempxpos, float tempypos) { c = tempc; xpos = tempxpos; ypos = tempypos; 17

Constructor parameters can be confusing 18

Scoping rules Remember scoping just means what variables are visible from where, in a program. Parameter variables names are only visible within the function that uses them Variables defined within a function are only visible within that function Object instance variables are visible anywhere in that instance (i.e., from any function in that object) Each object s instance variables are separate from each others Even if you use the cookie cutter to stamp out multiple cookies, each one s data is separate Even though the variable names are the same for all objects of the class. 19

Objects are data Once you create a class, you re defining a new data type (just like integers, strings, etc.) Objects can contain other objects! Just include it in the Class definition class Garage { Car car1, car2; Objects can be passed as arguments to functions, just like other data. One difference: When simple data types are passed in to a function, a copy is made of them. Changes to the data inside the function don t affect the copy outside. With objects, a reference is passed in. Changes to the object inside the function do affect the original. 20

Programming Challenge 1. Update the Car class Change the way the car is drawn (add some graphics for a passenger, for instance) Add a speed variable, and use that to control the speed of the car when it drives 2. Use an array of cars to allow more cars in your program, without needing to keep separate variables for each 21