<< Chapter < Page Chapter >> Page >

Note that if only one value is provided to color(), it will be interpreted as a grayscale value. Add a second value, and it will be used for alpha transparency. When three values are specified, they are interpreted as either RGB or HSB values. Adding a fourth value applies alpha transparency."

Therefore, the calls to the color method in the two calls to the Car constructor in Listing 2 create and pass color values for pure red and pure blue as parameters to the constructor.

Termination of the setup method

That completes the discussion of the setup method in Listing 2 . When the method terminates, the size of the display window has been set to 200 x 200 pixels. Two objects of the Car class have been instantiated and those object's references have been saved inthe instance variables named myCar1 and myCar2 .

Without having examined the constructor code for the Car class yet, about all we know about the Car objects is that a red color value was passed to the constructor for one object and a blue color value was passed to the constructor for the other object.

This looks like a good time to examine the constructor for the Car class in order to understand the meaning of the other constructor parameters in Listing 2 .

Constructor for the Car class

The code fragment in Listing 3 shows the beginning of the Car class down through the constructor. (The code fragment in Listing 3 was extracted from Listing 1 to make it easier to discuss.)

Listing 3. beginning of the car class.

class Car { color c;float xpos; float ypos;float xspeed; // The Constructor is defined with arguments.Car(color tempC, float tempXpos,float tempYpos, float tempXspeed) {c = tempC; xpos = tempXpos;ypos = tempYpos; xspeed = tempXspeed;}
Listing 3. Beginning of the Car class.

Instance variables

The car class begins by declaring four instance variables to hold values for:

  • A color
  • An x-coordinate value
  • A y-coordinate value
  • A speed value

The constructor parameters

Comparing the order of the constructor parameters in Listing 3 with the parameter values in Listing 2 tells us that the constructor for the first instantiated object of the Car class received the following values :

  • A color = red
  • An x-coordinate value = 0
  • A y-coordinate value = 100
  • A speed value = 2

Similarly, the constructor for the second instantiated object of the Car class received the following values:

  • A color = blue
  • An x-coordinate value = 0
  • A y-coordinate value = 10
  • A speed value = 1

The constructor body

The four statements in the body of the constructor in Listing 3 simply save the values of the incoming parameters in the corresponding instance variables discussed above .

Interpretation of the constructor parameters

Image 3 shows a screen shot of the display window while the sketch is running.

Image 3. screen shot of the sketch during execution.

Screen shot of the sketch during execution.
Image 3. Screen shot of the sketch during execution.

Looking at Image 3 , you can probably guess that the color values passed to the constructor are used to establish the visible color of each of the two rectangles.

Recall that this sketch is an animation where the rectangles move across the screen from left to right at different speeds You will see later that thecoordinate values received by the constructor specify the initial positions of the rectangles and that the y-coordinate values never change.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, The processing programming environment. OpenStax CNX. Feb 26, 2013 Download for free at http://cnx.org/content/col11492/1.5
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'The processing programming environment' conversation and receive update notifications?

Ask