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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Obviously, PI is never going to change, <strong>and</strong> the complier will enforce this if you use the<br />

static <strong>and</strong> final keywords in declaring it. If someone eventually tries to assign some<br />

other value to PI, the compiler would generate an error <strong>and</strong> prevent them from doing it.<br />

For the curious among you, the keyword double is the Java data type associated with PI.<br />

The double data type is similar to the float type, except a double requires twice as much<br />

storage as a float (8 bytes vs. 4 bytes), <strong>and</strong> also has greater precision (accuracy to 15 digits).<br />

However, double precision is not really necessary for the types of things most people do<br />

with <strong>Processing</strong>, so float was made the default primitive data type for any decimal values<br />

in <strong>Processing</strong>.<br />

Constructors<br />

As mentioned before, constructors are methods with the same name as the class, automatically<br />

invoked when an object is instantiated (created with the new keyword, as in new<br />

BurritoRecipe()). In addition, the constructor returns a reference to the object created.<br />

Thus, in the complete instantiation statement<br />

BurritoRecipe myRecipe = new Burrito Recipe();<br />

the variable myRecipe, declared of type BurritioRecipe, is assigned a reference to the<br />

newly created BurritoRecipe object.<br />

//3 constructors--default, basic, monster<br />

//default burrito recipe constructor<br />

BurritoRecipe (){<br />

}<br />

//regular burrito recipe constructor<br />

BurritoRecipe (String tortillaFlavor, String beanType, ➥<br />

String meatType){<br />

//initialize properties<br />

this.tortillaFlavor = tortillaFlavor;<br />

this.beanType = beanType;<br />

this.meatType = meatType;<br />

}<br />

//monster burrito recipe constructor(uuummmm)<br />

BurritoRecipe (String tortillaFlavor, String beanType, ➥<br />

String meatType, String[]toppings, ➥<br />

int salsaTemperature){<br />

//initialize properties<br />

this.tortillaFlavor = tortillaFlavor;<br />

this.beanType = beanType;<br />

this.meatType = meatType;<br />

this.toppings = toppings;<br />

this.salsaTemperature = salsaTemperature;<br />

}<br />

OBJECT-ORIENTED PROGRAMMING<br />

309<br />

8

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

Saved successfully!

Ooh no, something went wrong!