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

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 15

The "Ball" class

The bouncing ball application has at its core a class "Ball". We wish to program a system to have multiple bouncing balls. 

The Ball class we develop below will illustrate the following object concepts in Java:

      class
      variable
      method
      method reply
      method arguments

State and attributes

Each ball has the following state:

      its current (X, Y) position in the window
      its direction and amount of movement (direction and distance for X and Y values for each move)
      its colour
      its size (i.e. a radius size)

Note on US spelling of "color"

Since Java was developed in the US, and they spell colour as "color", we shall adopt the US spelling of "color" since some pre-written Java classes refer to the term.

US spellings such as "color" and "center" are simply a feature of Java that one has to accept.

This state corresponds to the following attributes:

      x position
      y position
      color
      radius size
      x motion (we'll call this "dx" from the common maths terms)
      y motion (we'll call this "dy")

So at present our class looks as follows:

class Ball

Attributes y
x
color
radius
dx
dy
Operations  

Operations

We will wish to both interrogate and set the following attributes:

      x position
      y position
      x motion (we'll call this "dx" from the common maths terms)
      y motion (we'll call this "dy")

We shall define a "get" access method for each attribute:

      getX
      getY
      getXMotion
      getYMotion

As is common for two related values, we can define two "set" access methods to set the (X, Y) position of a ball, and to set the (dx, dy) motion of a ball:

      setXY
      getMotion

We shall also define another method, that will move the Ball object by the values we set for its X and Y motion

      move

We will also need an operation to set up a new Ball object and an operation to display a ball. 

An operation to set up a new object is called a constructor. In Java a constructor is implemented as a method with the same name as the class, so we shall call our constructor operation simply "Ball".

For reasons we won't go into now, it is the convention to call the operation for an object to display itself "Paint". So we shall provide our Ball object with a "Paint" operation.

Our Ball class now looks as follows:

class Ball

Attributes y
x
color
radius
dx
dy
Operations Ball
paint
move
getX
getY
getXMotion
getYMotion
setXY
setMotion

If we define the X and Y coordinates to be whole numbers, and also the X and Y motion attributes to be whole numbers, we are able to describe each operation in terms of 

Implementation in Java

To implement our Ball class in Java we need to write a source file whereby:

      each attribute will become a Java variable (since we have no computed attributes)
      each operation will become a Java method

The Java source file "Ball.java" looks as follows, although for now we have replaced the statements of each method with a comment. The full source file for "Ball.java"   is available for viewing.

// Ball.java

import java.awt.*;

// Class representing the MODEL of a ball

public class Ball

{

 /////////////////////////// VARIABLES //////////////

  // Ball is circle, with centre (x,y) and radius "radius"

          int x;

          int y;

          int radius;

// each time ball moves it moves (dx, dy)

          int dx;

          int dy;

  // colour of ball is "color" (note we use US spelling)

          Color color;

///////////////////////// METHODS //////////////

// initialise new ball object

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

{

      x = newX;

      y = newY;

      radius = newRadius;

      color = Color.blue;       // default colour is blue

      dx = newXMotion;

      dy = newYMotion;

 }

 

// display the ball (using location and color variables)

 

public void paint (Graphics g)

{

      g.setColor(color);

      g.fillOval(x,y,radius, radius);

 }

// set the location of the centre of the ball

public void setXY(int newX, int newY)

{

      x = newX;

      y = newY;

 }

public int getX()

{

      return x;

public int getY()

{

      return y;

}

public int getXMotion()

{

      return dx;

}

public int getYMotion()

{

      return dy;

}

public void setColor( Color newColor )

{

      color = newColor;

}

public void setMotion( int newXMotion, int newYMotion )

{

      dx = newXMotion;

      dy = newYMotion;

}

public void move()

{

      x = x + dx;

      y = y + dy;

}

} // end of class definition
  

Perform activity 1 – Compilation of "Ball.java"

For simplicity we have used whole numbers (integers) for all variables, with the exception of the colour. As you will explore in a later unit, in Java colours are represented with objects of the class Color. Integers are indicated in Java by the term int .

The form of Java methods

As you may recall from earlier in the unit, to design each Java method we need to consider: 

      the return type (if any) of a method’s reply
      the name of the method
      a list of any arguments (and their types) required by the method

Methods that do not return any reply

Java uses the reserved word "void" to indicate that a method does not return a reply.
 

Each of these is considered below (for now we will continue to ignore the statements to be executed by each method).

The methods we have implement illustrate:
 

return type method name Java method implementation
void Ball Ball (int newX, int newY, int newRadius, int newXMotion, int newYMotion)
void Paint void paint (Graphics g)
void move void move()
int getX int getX()
int getY int getY()
int getXMotion int getXMotion()
int getYMotion int getYMotion()
void setXY void setXY(int newX, int newY)
void setMotion void setMotion( int newXMotion, int newYMotion )

We can see from the above table (and the Java source file) that each "get" method returns an integer -- this makes sense since the role of "get" methods is to return the value of an attribute (possibly after appropriate calculation). The two "set" methods both require arguments: "setXY" requires the new X and Y values, and "setMotion" requires the new "dx" and "dy" motion values.

The "Ball" constructor method requires the initial values for each attribute (except "color" which for now we shall assume is blue). The "paint" methods requires an argument that tells it which graphics object (i.e. which part of some window usually) to draw the representation of the object. The "move" method returns no reply, and requires no arguments -- it will use the values of its "dx" and "dy" variables to update the position of its "x" and "y" variables.

Back to top

basicline.gif (169 bytes)

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