These identical Monsters are kind of boring – and I am having trouble differentiating Wally from Louis – so let’s give each Monster a defining characteristic. For example, we could have Monsters with different colored eyes. What kind of thing is the eye color of a Monster? It is an attribute. Furthermore, some Monsters will have blue eyes and some will have green eyes, so this is an attribute that varies based on the Monster – different Monster objects will have different values for the eye color attribute. These type of attributes are called instance variables because their values will vary from object to object; in other words, each instance of a Monster will have a potentially different value for this attribute.
To give a Monster an attribute, we specify an identifier in the class to refer to the eye color. Identifiers should have names that make sense – for example, eyeColor is a good name for the name of the eye color. Identifiers cannot include spaces, so by convention when we would like to have a two-word identifier, like “eye color”, we drop the space and capitalize the first letter of the second word to get eyeColor.
When we define a variable, we must tell Java what type of thing the variable is going to keep track of; is it a number, or a letter, or an object of some kind? If the variable is keeping track of an integer, the type of the variable is int. If the variable is keeping track of a decimal number, the type of the variable is float, which as we discussed before is short for floating point number.
What type of variable is eyeColor? Well, we describe color on a computer screen using three parts: the amount of red, the amount of green, and the amount of blue. Since a color is really three numbers rather than one, instead of having just one variable for eye color, we need three: eyeColorRed, eyeColorGreen, eyeColorBlue. The type of each of these will be int. The Monster class with these instance variables added is shown in Figure 3.1.
Now that we have variables inside the object to keep track of the eye color of the Monster, we need a way to set up the eye color of each Monster. For now, let’s set the eye colors randomly. Recall that the main Processing object includes a method named random that generates a random number in some specified range. For example, random(255) will generate a random decimal number between 0 and 255 (including the 0 but not the 255). If we want the red value for the eye color to be set to a random number, we can add the statement eyeColorRed = random(255) to the beginning of the display method. This is our first example of a method call that returns a value – the random method not only takes a parameter but also returns a decimal number, which remember is called a float in Java. The statement above is an assignment statement that assigns a new value, namely the random value returned by random, to the instance variable eyeColorRed. Remember that assignment statements always have this form: the variable on the left hand side is assigned a new value, with the new value being the value of the right hand side. Furthermore, the value assigned must be of the same type as the variable on the left hand side. Hm...we have a problem. The type of eyeColor is int, but the type of the value returned by random is float. We want to take the decimal part off of the value returned by random and assign the integer part to eyeColor. To do this we put (int) in front of random. The int tells Java to convert the float returned by random into an int by simply ignoring any decimal part of the float. This peration is called a cast; we say we are casting the float into an int.
Figure 3.2 shows the code for assigning random values for each part of the eye color in display along with the sketch running.