You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before we begin examining the details of how object-oriented programming (OOP) works in Processing, let's embark on a short conceptual discussion of "objects" themselves. Imagine you were not programming in Processing, but were instead writing out a program for your day, a list of instructions, if you will. It might start out something like:
13
+
Before we begin examining the details of how object-oriented programming (OOP) works in Processing, let's embark on a short conceptual discussion of "objects" themselves. Imagine you were not programming in Processing, but were instead writing out a program for your day—a list of instructions, if you will. It might start out something like:
What is involved here? Specifically, what things are involved? First, although it may not be immediately apparent from how we wrote the above instructions, the main thing is you, a human being, a person. You exhibit certain properties. You look a certain way; perhaps you have brown hair, wear glasses, and appear slightly nerdy. You also have the ability to do stuff, such as wake up (presumably you can also sleep), eat, or ride the subway. An object is just like you, a thing that has properties and can do stuff.
22
+
What is involved here? Specifically, what things are involved? First, although it may not be immediately apparent from how we wrote the above instructions, the main thing is you—a human being, a person. You exhibit certain properties. You look a certain way; perhaps you have brown hair, wear glasses, and appear slightly nerdy. You also have the ability to do stuff, such as wake up (presumably, you can also sleep), eat, or ride the subway. An object is just like you, a thing that has properties and can do stuff.
23
23
<br/><br/>
24
-
So how does this relate to programming? The properties of an object are variables; and the things an object can do are functions. Object-oriented programming is the marriage of all of the programming fundamentals: data and functionality.
24
+
So, how does this relate to programming? The properties of an object are variables, and the things an object can do are functions. Object-oriented programming is the marriage of all of the programming fundamentals: data and functionality.
25
25
<br/><br/>
26
26
Let's map out the data and functions for a very simple human object:
27
27
<br/><br/>
@@ -76,7 +76,7 @@ <h3>Using an Object</h3>
76
76
<li>Increment car's location by speed.</li>
77
77
</ul>
78
78
79
-
To implement the above pseudo-code, we would define global variables at the top of the program, initialized them in setup(), and call functions to move and display the car in draw(). Something like:
79
+
To implement the above pseudo-code, we would define global variables at the top of the program, initialize them in setup(), and call functions to move and display the car in draw(). Something like:
80
80
<br/ ><br/>
81
81
82
82
<pre>
@@ -109,7 +109,7 @@ <h3>Using an Object</h3>
109
109
</pre>
110
110
111
111
<br/>
112
-
Object-oriented programming allows us to take all of the variables and functions out of the main program and store them inside a car object. A car object will know about its data - <em>color</em>, <em>location</em>, <em>speed</em>. The object will also know about the <em>stuff it can do</em>, the methods (functions inside an object) - the car can <em>drive</em> and it can be <em>displayed</em>.
112
+
Object-oriented programming allows us to take all of the variables and functions out of the main program and store them inside a car object. A car object will know about its data—<em>color</em>, <em>location</em>, <em>speed</em>. The object will also know about the <em>stuff it can do</em>, the methods (functions inside an object)—the car can <em>drive</em> and it can be <em>displayed</em>.
113
113
<br/><br/>
114
114
Using object-oriented design, the pseudocode improves to look something like this:
115
115
<br/><br/>
@@ -131,7 +131,7 @@ <h3>Using an Object</h3>
131
131
<li>Drive car object.</li>
132
132
</ul>
133
133
134
-
Notice we removed all of the global variables from the first example. Instead of having separate variables for car color, car location, and car speed, we now have only one variable, a Car variable! And instead of initializing those three variables, we initialize one thing, the Car object. Where did those variables go? They still exist, only now they live inside of the Car object (and will be defined in the Car class, which we will get to in a moment).
134
+
Notice: we removed all of the global variables from the first example. Instead of having separate variables for car color, car location, and car speed, we now have only one variable: a Car variable! And, instead of initializing those three variables, we initialize one thing: the Car object. Where did those variables go? They still exist, only now they live inside of the Car object (and will be defined in the Car class, which we will get to in a moment).
135
135
<br/><br/>
136
136
Moving beyond pseudocode, the actual body of the sketch might look like:
137
137
<br/ ><br/>
@@ -155,7 +155,7 @@ <h3>Using an Object</h3>
155
155
156
156
<h3>Writing the Cookie Cutter</h3>
157
157
158
-
The simple Car example above demonstrates how the use of objects in Processing makes for clean, readable code. The hard work goes into writing the object template, that is the class itself. When you are first learning about object-oriented programming, it is often a useful exercise to take a program written without objects and, not changing the functionality at all, rewrite it using objects. We will do exactly this with the car example, recreating exactly the same look and behavior in an object-oriented manner.
158
+
The simple Car example above demonstrates how the use of objects in Processing makes for clean, readable code. The hard work goes into writing the object template—that is, the class itself. When you are first learning about object-oriented programming, it is often a useful exercise to take a program written without objects and, not changing the functionality at all, rewrite it using objects. We will do exactly this with the car example, recreating exactly the same look and behavior in an object-oriented manner.
159
159
<br/><br/>
160
160
All classes must include four elements: name, data, constructor, and methods. (Technically, the only actual required element is the class name, but the point of doing object-oriented programming is to include all of these.)
161
161
<br/><br/>
@@ -227,7 +227,7 @@ <h3>Using an Object: The Details</h3>
227
227
<br/><br/>
228
228
<strong>Step 2. Initializing an object.</strong>
229
229
<br/><br/>
230
-
In order to initialize a variable (i.e., give it a starting value), we use an assignment operation - variable equals something. With a primitive (such as integer), it looks like this:
230
+
In order to initialize a variable (i.e., give it a starting value), we use an assignment operation—variable equals something. With a primitive (such as integer), it looks like this:
231
231
<br/ ><br/>
232
232
233
233
<pre>
@@ -247,11 +247,11 @@ <h3>Using an Object: The Details</h3>
247
247
248
248
In the above example, "myCar" is the object variable name and "=" indicates we are setting it equal to something, that something being a new instance of a Car object. What we are really doing here is initializing a Car object. When you initialize a primitive variable, such as an integer, you just set it equal to a number. But an object may contain multiple pieces of data. Recalling the Car class, we see that this line of code calls the <em>constructor</em>, a special function named <strong>Car()</strong> that initializes all of the object's variables and makes sure the Car object is ready to go.
249
249
<br/><br/>
250
-
One other thing; with the primitive integer "var," if you had forgotten to initialize it (set it equal to 10), Processing would have assigned it a default value, zero. An object (such as "myCar"), however, has no default value. If you forget to initialize an object, Processing will give it the value <em>null</em>. <em>null</em> means nothing. Not zero. Not negative one. Utter nothingness. Emptiness. If you encounter an error in the message window that says "NullPointerException" (and this is a pretty common error), that error is most likely caused by having forgotten to initialize an object.
250
+
One other thing: with the primitive integer "var," if you had forgotten to initialize it (set it equal to 10), Processing would have assigned it a default value—zero. An object (such as "myCar"), however, has no default value. If you forget to initialize an object, Processing will give it the value <em>null</em>. <em>null</em> means nothing. Not zero. Not negative one. Utter nothingness. Emptiness. If you encounter an error in the message window that says "NullPointerException" (and this is a pretty common error), that error is most likely caused by having forgotten to initialize an object.
251
251
<br/><br/>
252
252
<strong>Step 3. Using an object</strong>
253
253
<br/><br/>
254
-
Once we have successfully declared and initialized an object variable, we can use it. Using an object involves calling functions that are built into that object. A human object can eat, a car can drive, a dog can bark. Calling a function inside of an object is accomplished via dot syntax: variableName.objectFunction(Function Arguments);
254
+
Once we have successfully declared and initialized an object variable, we can use it. Using an object involves calling functions that are built into that object. A human object can eat; a car can drive; a dog can bark. Calling a function inside of an object is accomplished via dot syntax: variableName.objectFunction(Function Arguments);
255
255
<br/><br/>
256
256
In the case of the car, none of the available functions has an argument so it looks like:
In my experience, the use of constructor arguments to initialize object variables can be somewhat bewildering. Please do not blame yourself. The code is strange-looking and can seem awfully redundant: "For every single variable I want argument to that constructor?"
316
+
In my experience, the use of constructor arguments to initialize object variables can be somewhat bewildering. Please do not blame yourself. The code is strange-looking and can seem awfully redundant: "I need to place arguments inside the constructor for every single variable?"
317
317
<br/><br/>
318
-
Nevertheless, this is quite an important skill to learn, and, ultimately, is one of the things that makes object-oriented programming powerful. But for now, it may feel painful. Let's looks at how parameter works in this context.
318
+
Nevertheless, this is quite an important skill to learn, and, ultimately, is one of the things that makes object-oriented programming powerful. But for now, it may feel painful. Let's looks at how parameters work in this context.
319
319
<br/><br/>
320
320
<imgsrc="imgs/constructor_arguments.jpg">
321
321
<br/><br/>
322
-
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 <em>one purpose only</em>, to initialize the variables inside of an object. These are the variables that count, the car's actual color, the car's actual <em>x</em> location, and so on. The constructor's arguments are just <em>temporary</em>, and exist solely to pass a value from where the object is made into the object itself.
322
+
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 <em>one purpose only</em>: to initialize the variables inside of an object. These are the variables that count—the car's actual color, the car's actual <em>x</em> location, and so on. The constructor's arguments are just <em>temporary</em>, and exist solely to pass a value from where the object is made into the object itself.
323
323
<br/><br/>
324
324
This allows us to make a variety of objects using the same constructor. You might also just write the word <em>temp</em> in your argument names to remind you of what is going on (c vs. tempC). You will also see programmers use an underscore (c vs. c_) in many examples. You can name these whatever you want, of course. However, it is advisable to choose a name that makes sense to you, and also to stay consistent.
325
325
<br/><br/>
@@ -384,7 +384,7 @@ <h3>Objects are data types too!</h3>
384
384
<br/><br/>
385
385
If you were programming the Space Invaders game, for example, you might create a <em>Spaceship</em> class, an <em>Enemy</em> class, and a <em>Bullet</em> class, using an object for each entity in your game.
386
386
<br/><br/>
387
-
In addition, although not primitive, classes are data types just like integers and floats. And since classes are made up of data, an object can therefore contain other objects! For example, let's assume you had just finished programming a <em>Fork</em> and <em>Spoon</em> class. Moving on to a <em>PlaceSetting</em> class, you would likely include variables for both a <em>Fork</em> object and a <em>Spoon</em> object inside that class itself. This is perfectly reasonable and quite common in object-oriented programming.
387
+
In addition, although not primitive, classes are data types, just like integers and floats. And since classes are made up of data, an object can therefore contain other objects! For example, let's assume you had just finished programming a <em>Fork</em> and <em>Spoon</em> class. Moving on to a <em>PlaceSetting</em> class, you would likely include variables for both a <em>Fork</em> object and a <em>Spoon</em> object inside that class itself. This is perfectly reasonable and quite common in object-oriented programming.
388
388
<br/ ><br/>
389
389
390
390
<pre>
@@ -413,11 +413,10 @@ <h3>Objects are data types too!</h3>
413
413
</pre>
414
414
<br/>
415
415
416
-
When a primitive value (integer, float, etc.) is passed in a function, a copy is made. With objects, this is not the case, and the result is a bit more intuitive. If changes are made to an object after it is passed into a function, those changes will affect that object used anywhere else throughout the sketch. This is known as <em>pass by reference</em> since instead of a copy, a reference to the actual object itself is passed into the function.
416
+
When a primitive value (integer, float, etc.) is passed in a function, a copy is made. With objects, this is not the case, and the result is a bit more intuitive. If changes are made to an object after it is passed into a function, those changes will affect that object used anywhere else throughout the sketch. This is known as <em>pass by reference</em> since, instead of a copy, a reference to the actual object itself is passed into the function.
0 commit comments