Objects

Objects!

There are many built-in data types in Processing, and lots more available to be used from Java (you can see them all here: @http://docs.oracle.com/javase/6/docs/api/).

There are basic data types - known as **primitive** data types - int, boolean, float and char are some of these. (Hint: these all start with a lower-case letter.) These are known as primitive because they can only store one "piece" of information. They don't have information about the information that they store (a.k.a **meta-information** or **meta-data**) and they certainly don't have a collection of information. They store very simple bits of information.

String, PImage, and PShape are examples of non-primitive or **composite** data types. These are known as **objects**. Objects **abstract** (keep only the most important) information about real world "things" or ideas.

Objects are stored in memory and have a few things in common: 1) Instance Variables - these are characteristics of the objects 2) Methods - these are functions that only apply to the objects 3) A Constructor (or Constructors!) - a way to make new versions of that object

The code for an object is known as a **class**. A class is to code what a blueprint is to a building. Each time you want to make a new object, you have to use the class to create a new version of that object. Each version (called an "**instance**") has its own **instance variables.** The class constructor(s) can be used to create the new object, and the methods for that class can be called on the new object.

Example: Imagine that you wanted to create a Person class. What kinds of characteristics would you need to keep track of? It probably depends on the application. Let's say we wanted to create a new system for online signups for a club. What would you need to know about a person? What would you need to be able to find out about each person?

In our example, we'll pick these characteristics to keep track of: -Their name -Their age -Their first spoken language

Our methods will be kind of boring for this example, but you could add others to make it more interesting. Some methods you would need are: -getName (you'll need to be able to get their name) -getAge (we need to know their age) -getFirstLanguage (we need to get their language) -growAYear (when they have a birthday)

We'll also need a way to create a person (the constructor) .

To create a new class in Processing, we need to start with the word **class**. To structure things nicely, we'll put this after setup and draw. Then, we put the name of the class.

code format="java" void setup { size(400, 400); }

void draw { line(10, 20, 30, 40); //just filler code }

class Person {

} code Next, we need to list our characteristics or **instance variables**.

code format="java" //other code omitted

class Person { String name; int age; String firstLanguageSpoken; } code We would normally put our **constructor** next. Let's do a simple constructor:

code format="java" //other code omitted

class Person { String name; int age; String firstLanguageSpoken;

Person {

name = "Bob";

age = 52;

firstLanguageSpoken = "French";

} } code (You might notice that this is not that useful, since every person will be a middle-aged man named Bob. Don't worry, we'll change that soon!)

Finally, we have to add the **methods** for these objects. code format="java" //other code omitted

class Person { String name; int age; String firstLanguageSpoken;

Person { name = "Bob"; age = 52; firstLanguageSpoken = "French"; }

String getName { return name; }

int getAge { return age; }

String getFirstLanguageSpoken { return firstLanguageSpoken; }

void growAYear { age = age + 1; } } code

So, now we have this beautiful blueprint! But how do we use it?

We need to go back up to our "main" program - setup and draw. To create a new Person object, we have to create a variable to store the Person object. We do this at the beginning: code format="java" Person pOne; void setup { } //...other code omitted code

Then, we need to make a person to go in pOne! We called the **constructor** for Person using the keyword **new.** code format="java" Person pOne; void setup { pOne = new Person; //...other code omitted } code We can then call some of pOne's methods on pOne and print out the results! code format="java" Person pOne; void setup { pOne = new Person; int a = pOne.getAge; println(a); //should print 52 String n = pOne.getName; //should print Bob println(name); pOne.growAYear; println(pOne.getAge); //should print 53 } //...other code omitted code

We could create more Person objects like this: code format="java" Person pOne; Person pTwo; void setup { pOne = new Person; pTwo = new Person; int a = pOne.getAge; println(a); //should print 52 String n = pOne.getName; //should print Bob println(name); pTwo.growAYear; //note that we're changing the new variable pTwo.growAYear; println(pOne.getAge); //should print 52 println(pTwo.getAge); //should print 54

} //...other code omitted code

Notice that pOne and pTwo were completely independent!

Now, let's fix up our Person class so that different Person objects have different names, ages, and first languages. We need to modify our constructor so that it takes in parameters for these three characteristics. code format="java" //...code before the class omitted

class Person { String name; int age; String firstLanguageSpoken;

Person(String n, int a, String fLang) { name = n;       age = a;        firstLanguageSpoken = fLang; } //...other class code omitted } code Notice that n, a, and fLang are just parameters - they can't be used anywhere else in our class or code, except in the **constructor**.

Now, we can set up two (or more!) different people objects: code format="java" Person pOne; Person pTwo;

void setup { pOne = new Person("Jerry", 13, "Swedish"); pTwo = new Person("Sue", 16, "Japanese"); String n = pOne.getName; println(n + "is a cool club member"); println(pOne.getAge); //should print 13 println(pTwo.getFirstLanguageSpoken + "is " + pTwo.getName + "'s first language"); } //...the rest of the code is omitted code

Right now, you might be slightly overwhelmed. Why not just keep track of each person separately? Well, you can imagine with many, many club members, it's nice to keep each person's information together. Also, when combined with arrays or other data structures, this makes for a great way to store lots of data!