Chapter 8 Objects.

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

Code Elements and Processing Coordinate System. Code elements: pages Comments: are documentation notes that are ignored by the computer but are.
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.
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.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
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:
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Classes CS 21a: Introduction to Computing I First Semester,
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
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.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Review Objects – data fields – constructors – Methods Classes.
Objects Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Andrew(amwallis) Classes!
Lecture contents Looping Arrays For each loop while do while for
OOP - Object Oriented Programming
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Classes C++ representation of an object
Some of Chap 17.
Concepts of Object Oriented Programming
Building Java Programs
Computer Graphics: Rocket, Java: Class
Chapter 8 Objects.
Introduction to Object-oriented Program Design
Chapter 7 Functions.
Lecture 4-7 Classes and Objects
Recursion 12-Nov-18.
Chapter 7 Functions.
Code Elements and Processing Coordinate System
Functions BIS1523 – Lecture 17.
Object-oriented Design in Processing
Organizing Memory in Java
Chapter 10 Algorithms.
CS 100: Roadmap to Computing
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Chapter 9 Objects and Classes
Learning Objectives Classes Constructors Principles of OOP
Announcements Mid-Term Exam!! Quiz 5 Again + Quiz 6 Exercise 5
Chapter 10 Algorithms.
Recursion 29-Dec-18.
Code Elements and Processing Coordinate System
Classes and Objects.
Object-oriented Design in Processing
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
Recursion 23-Apr-19.
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
Classes C++ representation of an object
Object-oriented Design in Processing
Lecture 6 Methods, Classes, Inheritance
Object-Oriented Programming and class Design
Introduction to Computer Science and Object-Oriented Programming
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 (methods). through the declaration of classes

Everyday objects In Processing, we’ve been using all along (variables, functions, loops, etc. Now more efficiently organized. ) 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.) What are the properties of a car such as this? What can it do?

Definition of Class A body of code that exists independently of the main body of code from which it is referenced. It defines a set of properties & methods that are common to all objects of one type. 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: New to OOP? Start with former regular sketch. Then rewrite with objects. No functional changes. KISS. (i.e. keep it simple 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 these 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. (a variable of complex data type) - 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 Let’s look at another example Using an object Define a class Create without looking at code Start with only display() function //The data or variables class Fish { float fishY ; float fishSize ; float fishX ; fishY= 170; fishX = 60; Fish() { //The constructor } 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, fishSize/8, fishSize/8); if(fishX > width) { fishX = fishX + 1; void swim() { fishX = 0; } size(500,250); void setup() { Fish tastyFish; void draw() { } tastyFish = new Fish(); tastyFish.display() ; background(200); 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. Or try something from scratch. E.g., design balloons that move up. Create parameters for at least 2 things – such as x and color.