Ball.java

The first class that needs to be developed is an implementation of the PhysicalObject that extends CartesianObject. The Ball class does this in the sample application. The source code can be viewed by clicking here. We will go through the code for this class in detail so that even those new to Java can get a feel for what is being done and why.

Source code denoted between the /** ... */ lines are special comments for the JavaDoc utility. If you are not familiar with this utility, read about it in the Learning Java referenced the Framework Design section. The first part of the class tells the compiler that we are are going to need to use the CartesianObject class in the feynman.framework.system package. The class definition line tells the compiler that we are extending this class.

/*
* Feynman Java Simulation Framework Sample Application
*
* $author: Wes Bailey$
*/
import feynman.framework.system.CartesianObject;

/**
* Bean implementation of <b>CartesianObject</b> to define the physical
* characteristics of a ball in the Cartesian coordinate system.
*/
public class Ball extends CartesianObject {
Notice the JavaDoc says the Ball class is a Bean implementation of the CartesianObject class. This is true because it is a JavaBean and more importantly this class can be thought of a reference to the actual memory component being used. For experienced C programmers think of this as an easier way to work with pointers!

The next part of the Bean are the local instance variables. This terminology refers to the scope of these named variables. If this is not clear to you, then check the Learning Java reference.

// ---------------------------------------- instance variables.

// Velocity components
private double vx;
private double vy;
private double vz;

// Used to calculate the max height.
private double vyold;
private double maxheight;

// Acceleration components.
private double ax;
private double ay;
private double az;
If you notice, the x, y and z components are not defined like the velocity and acceleration components have been? The reason is because the CartesionObject parent class provides them. This illustrates the power of a property known as inheritance in Object Oriented languages. Since Ball extends CartesianObject, it can access the instance variables and methods defined in CartesianObject. This also illustrates the power of the Framework in that many of these types of capabilities are provided by it so you don't have to code it.

Two other variables have been defined. The vyold and the maxheight are used in the calculation of the maxmimum height that the ball achieves during its flight. How these are used will be seen when we discuss the implementation of the PhysicalMeasurement class.

The rest of the source to this point is a number of getter and setter methods that provide public access to our privately defined instance variables. This is the way a bean is developed and is a good programming practice to follow. No longer do you allow other methods and classes direct access to your instance variables. Instead they are manipulated through the get and set methods shown below. This will be a new concept for traditional C and C++ developers who might not have experience developing JavaBeans. Some of the source for the Ball class is listed below without the JavaDoc included. For a complete view of the class open Ball.java and read the source code.


...

public void setVy(double vy) {
	this.vy = vy;
}

public double getVy() {
	return vy;
}

public void setVyold(double vyold) {
	this.vyold = vyold;
}

...

public void setMaxheight(double maxheight) {
	this.maxheight = maxheight;
}

public double getMaxheight() {
	return maxheight;
}

For those of you new to Java notice the usage of the this keyword when assigning the value of the local instance variable to the parameter value. The keyword this refers to the local instance variable and allows distinction between it and the parameter value of the same name. Regardless of whether the parameter value has the same name as the instance variable, it is a good programming practice when developing a Bean to use this when assigning values to instance variables.

Previous Section | Next Section