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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

314<br />

Alas, if there is no tortilla, there is no way to make a burrito. Lastly, notice how the for<br />

loop is used to run through the list of toppings. Using an array for the toppings <strong>and</strong> the<br />

array’s length property as the limit on the for loop, any number of toppings can be added<br />

to the recipe, <strong>and</strong> the for loop will automatically loop through the entire array, using the<br />

value of the array’s length property to determine when to stop. Throughout the rest of<br />

the book, you’ll see many similar examples using for loops <strong>and</strong> arrays.<br />

Finally, here’s a complete <strong>Processing</strong> implementation that actually uses the class. Enter all<br />

the following code into <strong>Processing</strong> <strong>and</strong> run the sketch—or better yet, download the<br />

complete code (BurritoRecipe.pde) from the Download section of the friends of ED site<br />

(www.friendsofed.com/).<br />

/*<br />

title: BurritoRecipe class example<br />

description: create some tasty burritos<br />

created: August 9, 2005<br />

revised: March 10, 2006<br />

October 23, 2006<br />

by: Ira Greenberg<br />

note: Enjoy <strong>and</strong> use plenty of guac.<br />

*/<br />

void setup(){<br />

// create some burrito recipes<br />

//use constructor 1<br />

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

b1.printRecipe();<br />

//use constructor 2<br />

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

b2.printRecipe();<br />

//use constructor 3<br />

String[]tpgs = {"tomato", "lettuce", "corn"};<br />

BurritoRecipe b3 = new BurritoRecipe("whole wheat", "pinto", ➥<br />

"beef", tpgs , 8);<br />

b3.printRecipe();<br />

}<br />

class BurritoRecipe {<br />

// properties<br />

int size;<br />

String tortillaFlavor;<br />

String meatType;<br />

String beanType;

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

Saved successfully!

Ooh no, something went wrong!