Objects
- maiguzman28
- Jun 6, 2020
- 4 min read
Updated: Jul 16, 2020
The next lesson is about object oriented processing. As usual I started with the tutorial videos before diving into the book. This are my notes on the video:

I liked how Daniel S. explains everything, he took a human as an example. Data ist for example height, weight, eye color, personality, etc... Functionality are things like sleep, eat, run, etc... These are components that are necessary for an object.
Class is like a template, the cookie cutter, if you will. The object is the thing itself, the cookie, if you will.
Data Type:
Primitive Data Type: Data Type with just a single number, lower case. Like float or int.
Object Data Type: Collection of data and functions, capital letter. Like PImage, String.
This is a lot like working with functions. It is new information, but seems logical to me.
Inside the class, we need to have functions in order to tell the program what this object has to do. Oh and we can also work with tabs! :O
Constructor: The place where the object is "born". Return type - is actually the object, it is returning the object. This is where you can initialize things, for example the position of the object.
This is what you need to have inside your object:
Data
Constructor
Functions
Something that is a bit confusing but very important is, you need a temp value in order to use this value in the whole object not only in a local function. This is going to be used along the codes of this tutorial serie.
The last video about pass by value and pass by reference video was very complicated, but it looks very cool, so I am kind of looking forward to try that.
Now I am done with the videos, and ready to read the chapter about objects in order to reforce the information I have learned and apply this new knowledge.
Exercise 8-1: Consider a car as an object. What data would a car have? What functions
would it have?
Car Data:
Color
Size
Doors (2, 4?)
Brand
Horse Power
Car Functions:
speed
brake
drive back
turn on windshields
etc..
This is important:
Functions that are inside of an object are technically referred to as “ methods ” in Java so we can begin to use this nomenclature (see Section 7.1). Calling a method inside of an object is accomplished via dot syntax:
variableName.objectMethod(Method Arguments);
Exercise 8-3: Assume the existence of a Human class. You want to write the code to declare a
Human object as well as call the function sleep( ) on that human object. Write out the code below:
Declare and initialize the Human object:
human h; human h = newHuman ();
Call the sleep( ) function:
h.sleeping ();
Exercise 8-4: Create a sketch with multiple tabs. Try to get the Car example to run without
any errors.
This is also very impotant. This explains the temporary argument that I did not understand in the tutorial video:
Arguments are local variables used inside the body of a function that get filled with values when the function is called. In the examples, they have one purpose only, to initialize the variables inside of an object. These are the variables that count, the car’s actual car, the car’s actual x location, and so on. The constructor’s arguments are just temporary, and exist solely to pass a value from where the object is made into the object itself. This allows us to make a variety of objects using the same constructor.
Exercise 8-5: Rewrite the gravity example from Chapter 5 using objects with a Ball class.
Include two instances of a Ball object. The original example is included here for your reference with a framework to help you get started.
This was actually very understandable. It is complicated, but it was ok to do. And it worked!
//Define variable
Ball ball1;
Ball ball2;
float gravity = 0.1;
void setup() {
size(200, 200);
//creating ball objects
ball1 = new Ball(50,0,16);
ball2 = new Ball(100,50,32);
}
void draw() {
background(255);
//calling the functions of the object, it has to be double because of the two balls
ball1.display();
ball2.display();
ball1.move();
ball2.move();
}
class Ball {
//declaring the variables
float x;
float y;
float w;
float speed;
//this is the ball constructor
Ball(float tempX, float tempY, float tempW) {
//location of the ball
x = tempX;
y = tempY;
w = tempW;
speed = 0;
}
//this is what we have as ball.display
void display () {
// Display the circle
fill(random(255),random(255),random(255));
noStroke();
ellipse(x, y, w, w);
}
void move () {
// Add speed to y location
y = y + speed;
// Add gravity to speed
speed = speed + gravity;
// Reverse speed
if (y > height) {
speed = speed * -0.90;
y = height;
}
}
}
On to the next. Here are a couple of fun facts about objects:
A class can include other objects among its variables.
A function can have an object as its argument.
Objects allow you to organize the concepts inside of a software application into modular, reusable packages.
refactoring: reorganization process
Exercise 8-6: Rewrite Example 8-3 to include two Zoogs. Can you vary their appearance?
Behavior? Consider adding color as a Zoog variable.
This one is also fun to do.
The second coding session focused also a lot on objects. These are the excercises we did this week:
void setup() {
size(800, 800);
myCoronas = new Corona[500];
for (int i=0; i<myCoronas.length; i++) {
myCoronas[i]= new Corona();
myCoronas[i].x=(int)random(0, width);
myCoronas[i].y=(int)random(0, height);
}
}
Corona[] myCoronas;
void draw() {
background(111);
for (int i=0; i<myCoronas.length; i++) {
myCoronas[i].drawMe();
}
}
class Corona {
int speedX=3, speedY=2, x=0, y=0;
void drawMe() {
x=x+speedX;
ellipse(x, y, 30, 30);
if (x>width) {
speedX = speedX*-1;
}
if (x<0) {
speedX=speedX*-1;
}
y=y+speedY;
if (y>height || y<0) {
speedY=speedY*-1;
}
}
}
In order to add some randomness to these excercise, we added randomness to thexspeed and to the yspeed. So it looked like this:
void setup() {
size(800, 800);
myCoronas = new Corona[500];
for (int i=0; i<myCoronas.length; i++) {
myCoronas[i]= new Corona();
myCoronas[i].x=(int)random(0, width);
myCoronas[i].y=(int)random(0, height);
myCoronas[i].speedX=(int)random(0, 3);
myCoronas[i].speedY=(int)random(0, 3);
}
}
Corona[] myCoronas;
void draw() {
background(111);
for (int i=0; i<myCoronas.length; i++) {
myCoronas[i].drawMe();
}
}
class Corona {
int speedX=3, speedY=2;
int x=0, y=0;
void drawMe() {
x=x+speedX;
ellipse(x, y, 30, 30);
if (x>width) {
speedX = speedX*-1;
}
if (x<0) {
speedX=speedX*-1;
}
y=y+speedY;
if (y>height || y<0) {
speedY=speedY*-1;
}
}
}
This we could have done with tabs in order to structure the code more.
I really liked this session, and everything was very understandable.
We also did a little bit of arrays at the end of the session, but even thought it was not something I had already learned, it was very understandable.
Commentaires