<< Chapter < Page Chapter >> Page >

You shouldn't find any surprises in Listing 2. The code in Listing 2 simply constructs the GUI shown in Figure 2, placing the label, the buttons, andthe text area in their respective locations.

The remainder of the constructor for the Driver class

The remainder of the constructor for the Driver class is shown in Listing 3.

The remainder of the constructor for the driver class.

//Register a click event handler on each of the // buttonsareaButton.addEventListener( MouseEvent.CLICK,areaButtonHandler);circButton.addEventListener(MouseEvent.CLICK,circButtonHandler);volumeButton.addEventListener( MouseEvent.CLICK,volumeButtonHandler);}//end constructor

Listing 3 registers a click event handler on each of the buttons shown in Figure 2.

The getRandomValues method

The method named getRandomValues is shown in Listing 4.

The getrandomvalues method.

//Local utility method for getting and saving four // random values.private function getRandomValues():void{ randomChoice = Math.random();radius = uint(10*Math.random() + 1); rectWidth = uint(10*Math.random() + 1);rectHeight = uint(10*Math.random() + 1); }//end getRandomValues

This method is called by each of the event handler methods to get and save four random values that are subsequently used by the event handler. Therandom values are saved in the instance variables that are declared in Listing 1.

The event handler method named areaButtonHandler

The click event handler that is registered on the button labeled Area in Figure 2 is shown in Listing 5.

The event handler method named areabuttonhandler.

//Define click event handler methods. private function areaButtonHandler(event:MouseEvent):void{getRandomValues();if(randomChoice<0.5){ myShape = new MyCircle(radius);}else{ myShape = new MyRectangle(rectWidth,rectHeight);}//end elsetextArea.text = myShape.id() + myShape.area(); }//end areaButtonHandler

Decide between two classes

The code in Listing 5 uses the random value stored in randomChoice to decide whether to instantiate an object of the class named MyCircle or the class named MyRectangle .

Store object's reference as type IArea

The object's reference is stored in the instance variable named myShape , which is type IArea . Storage of the reference in a variable of that type is possible only because both objects implement the interface named IArea .

Call the id method using the reference

Then the code in Listing 5 uses the reference stored in myShape to call the id method and the area method on the object. The returned values are used to construct a new text string for the text area shownin Figure 3.

How is this possible?

Calling these methods using the reference of type IArea is possible only because abstract versions of both methods are inherited into both classesfrom the interface named IArea .

The other two click event handler methods

The click event handler methods that are registered on the Circumference button and the Volume button in Figure 2 are shown in Listing 6.

Questions & Answers

Explain the following terms . (1) Abiotic factors in an ecosystem
Nomai Reply
Abiotic factors are non living components of ecosystem.These include physical and chemical elements like temperature,light,water,soil,air quality and oxygen etc
Qasim
what is biology
daniel Reply
what is diffusion
Emmanuel Reply
passive process of transport of low-molecular weight material according to its concentration gradient
AI-Robot
what is production?
Catherine
Pathogens and diseases
how did the oxygen help a human being
Achol Reply
how did the nutrition help the plants
Achol Reply
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
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
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
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 respiration
Deborah
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
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, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask