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.

println();<br />

}<br />

}<br />

This method may look complex at first glance, but it is really just a bunch of if statements<br />

sending output to the text window. One thing that is new in the method is the use of the<br />

keyword null in some of the if statements. For example<br />

if (this.tortillaFlavor!=null){<br />

The conditional expression != means “is not equal to.” So the if statement is then true<br />

when this.tortillaFlavor is not equal to null. This type of logic can seem tricky when<br />

you first start using it. Just try to remember that if statements are checking if the condition<br />

between the parentheses is true, not for a universal truth. So, if I say ira = tall (I’m<br />

not really), then the conditional if (ira==tall) would evaluate to true, as would if<br />

(ira!=short). To explain why I used the null keyword in the first place, I need to go back<br />

to the discussion about declaring variables/properties.<br />

Notice that in the class, when I declare a property using the String data type, that String<br />

has an initial cap. (Remember that Java <strong>and</strong> <strong>Processing</strong> are case sensitive, so the capital S<br />

matters.) Also, throughout the book, when variables of a primitive type have been<br />

declared—such as int or float—all lowercase letters were used. The capital S in String<br />

should inform you that the String data type is a class. Variables or properties declared of<br />

type String, or of any class for that matter, are not primitive types like int, float, or<br />

boolean that actually evaluate to specific values, but instead are object reference variables<br />

that evaluate to the memory address where the objects are stored. Now you don’t need to<br />

worry about memory addresses. But what you do need to know is that the default value of<br />

an object reference, once it has been declared <strong>and</strong> before it has been defined, is null.<br />

In the printRecipe() method, I needed to find a way to keep the program from crashing<br />

when it couldn’t a find a value it expected. By writing the if statements the way I did, I<br />

ensured that if the property was initialized, <strong>and</strong> therefore wasn’t equal to null, the block<br />

of code would run. However, if it was still equal to null, then the relevant print statement<br />

would be skipped, keeping the program from crashing. This is actually a pretty common<br />

thing to do in coding, <strong>and</strong> you will see more of it later on in the book.<br />

Throughout the method, I also used the concatenation operator, +, to join the string literals<br />

in quotes with the properties. Remember, literals are just actual words or numbers that<br />

you write—for example, “art” (a string literal) or 10 (a number literal, which is referred to<br />

as a constant). A literal’s meaning or value is explicitly what is written. To join a literal with<br />

a property or variable, you need to use the concatenation operator, as follows:<br />

this.beanType + " beans <strong>and</strong> white wine."<br />

Also notice within the method that most of the if statements are nested within an outer<br />

if statement, which determines if there is a tortilla. If there isn’t a tortilla, then the program<br />

skips the remaining statements <strong>and</strong> outputs the following:<br />

"Uh, you'll need to give me some ingredients if you actually want me<br />

to produce a recipe."<br />

OBJECT-ORIENTED PROGRAMMING<br />

313<br />

8

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

Saved successfully!

Ooh no, something went wrong!