04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

this.salsaTemperature+" alarm salsa.");<br />

}<br />

}<br />

else {<br />

println("Uh, you'll need to give me some ingredients\n" + ➥<br />

"if you actually want me to produce a recipe.");<br />

}<br />

// go to next line after printing<br />

// cooking instructions to the screen<br />

println();<br />

}<br />

}<br />

There are a couple of ways of using a class in <strong>Processing</strong>. For one, you can simply add it in<br />

your main tab with the rest of your sketch code, just as you’ve been doing with functions.<br />

(To use a custom class or function, you must also include <strong>Processing</strong>’s setup() function.)<br />

You can also enter a class (or function) in a separate tab. The sketch will still see it/treat it<br />

as if it exists in the main tab. Finally, you can also work in Java mode, which I’ll cover in<br />

Chapter 14. For the BurritoRecipe example, I used the simplest solution, <strong>and</strong> just entered<br />

the class code directly below the setup() function. At the top of the sketch, within<br />

the setup() function, I instantiated three BurritoRecipe objects, <strong>and</strong> called the<br />

printRecipe() method for each of them. When the sketch runs, it outputs burrito<br />

cooking directions, based on the arguments I passed when I instantiated the three<br />

BurritoRecipe objects.<br />

In the first instantiation statement, I didn’t pass any arguments:<br />

BurritoRecipe b1 = new BurritoRecipe();<br />

The printRecipe() method outputs the following:<br />

Burrito Recipe:<br />

---------------<br />

Uh, you'll need to give me some ingredients<br />

if you actually want me to produce a recipe.<br />

In the second instantiation statement, I passed three arguments:<br />

BurritoRecipe b2 = new BurritoRecipe ("spinach", "black", "chicken");<br />

The printRecipe() method outputs the following:<br />

Burrito Recipe:<br />

---------------<br />

Steam or lightly pan heat a spinach tortilla.<br />

Sauté fresh onions, garlic, cilantro, slowly mixing in black beans ➥<br />

<strong>and</strong> white wine.<br />

Grill chicken along with fresh green pepper, jalapeno pepper, ➥<br />

chile pepper, onions <strong>and</strong> garlic.<br />

OBJECT-ORIENTED PROGRAMMING<br />

317<br />

8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!