您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 24

The "BallWorld" class - version 8

The final version of our bouncing ball application illustrates two important object concepts: inheritance and me

We shall add to application a new kind of ball, one that moves randomly. We shall call this class RandomBall, and it will be an extension, a subclass of our existing Ball class.

The only different between our RandomBall class and our Ball class, shall be that obects of our RandomBall class randomly change their direction as they move. RandomBall, as a subclass of Ball automatically inherits all the variables and methods of the Ball class. Since we wish to create a RandomBall class of objects that behaves differently when it receives a "move" message than Ball objects, we need to create a new version of the "move" method. By defining a new "move" method in our RandomBall class, we shall override the "move" method that would be inherited.

Our new "move" method looks as follows:

public void move()

{

// 50% chance of chaning X motion

if( Math.random() > 0.5 )

dx = -dx;

// 50% chance of changing Y motion

if( Math.random() > 0.5 )

dy = -dy;

// move centre of object

x = x + dx;

y = y + dy;

}

Don't worry about the details, we simple have created 2 random tests, that mean there is a 50% chance of this method reversing the X , and possibly also the Y, movement values "dx" and "dy". The centre of the object (x, y) then has these movement values added to it as usual.

Note on Math.random()

This is an example of a pre-written class and method provided by Sun. There are many classes that have been created for common programming tasks.

The "random()" method of the Math class simple uses a pseudo-random number generator to provide a decimal value from zero, up to but not including 1 (e.g. 0.54, 0.00, 0.99 etc.). Thus by testing if this value is > 0.5 we simulate a 50% probability to give our RandomBall random movement.

As usual, we shall provide a constructor with the same name as the class, RandomBall. Since we are doing nothing different when creating a RandomBall than when creating a Ball, we can use the inherited "Ball" constructor method to set the centre, radius and movement variables. To refer to the inherited "Ball" constructor in the subclass RandomBall, we use the Java reserved word "super" -- to indicate the direct superclass of the RandomBall object.

Thus the constructor for RandomBall looks as follows (it simply pass the arguments on to the inherited constructor method of Ball):

public RandomBall (int newX, int newY, int newRadius, int newXMotion, int newYMotion)

{

// call the Ball constructor

super( newX, newY, newRadius, newXMotion, newYMotion );

}

Since we have no extra method for RandomBall objects, and no extra variables, the whole definition of the RandomBall class consists of the two methods we've just looked at. I.e. The full listing of the Java source file "RandomBall.java" is as follows:

// RandomBall.java

public class RandomBall extends Ball

{

public RandomBall (int newX, int newY, int newRadius, int newXMotion, int newYMotion)

{

// call the Ball constructor

super( newX, newY, newRadius, newXMotion, newYMotion );

}

public void move()

{

// 50% chance of chaning X motion

if( Math.random() > 0.5 )

dx = -dx;

// 50% chance of changing Y motion

if( Math.random() > 0.5 )

dy = -dy;

// move centre of object

x = x + dx;

y = y + dy;

}

} // class

The only changes we need to make to our BallWorld class is to create a new variable that refers to an object of class RandomBall, to initialise this variable (we'll make it black so it stands out from the coloured ones), and to make sure it is painted, moved and bounced in our "paint" method.

So do declare a variable "rBall" that can refer to an object of class RandomBall we add:

private RandomBall rBall; 

To create a new object, and make variable "rBall" refer to it, and to make this object black, we add the following to our "BallWorld" constructor:

rBall = new RandomBall(200, 200, 40, 5, 5);

rBall.setColor( Color.black );

To paint, move and bounce the ball, we add these lines to our "paint" method:

rBall.paint( g );

rBall.move();

and

bounce( rBall );

The full source file for "BallWorld.java - version 8"  and "RandomBall.java" are available for viewing (and can be opened in the Kawa editor).

Perform activity 9 – Compilation and running of "BallWorld.java" - version 8

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg