Writing the Code

Now that we have seen how to construct and use a Basketball object, let's proceed to You can see the completed code is in the file Basketball.java that is in your project. First look at the basic structure. The class body appears between the curly braces. Most classes consist of the basic structure represented below by the three boxes.

Click on each part to show or hide the code (most of the comments from the original file are omitted here).

package lab2;

public class Basketball
{

// Instance variables represent the state or attributes
private boolean isInflated; private double diameter;
// Constructors initialize the instance variables
public Basketball(double givenDiameter) { isInflated = false; diameter = givenDiameter; }
// Methods define the operations
public double getDiameter() { return diameter; } public boolean isDribbleable() { // can be dribbled only if it is inflated return isInflated; } public double getCircumference() { double result = Math.PI * diameter; return result; } public void inflate() { isInflated = true; }
}

Class structure

1. Instance variables

The first thing to notice is that there are two variables that are declared outside of any method.
  private boolean isInflated;
  private double diameter;
These variables represent the attributes of a Basketball instance. As you saw in the picture on the previous page, they can have different values for different Basketball instances. For this reason they are called instance variables.

The details of the keywords public and private will become more clear later in the course. For now we'll just follow the rule that instance variables are always declared with the private keyword.

2. Constructors

The next thing to notice is the constructor:
  public Basketball(double givenDiameter)
  {
    isInflated = false;
    diameter = givenDiameter;
  }
The purpose of the constructor is to initialize the instance variables to correct initial values.

Here, we first set the variable isInflated to false. To initialize the diameter, remember than when we constructed a Basketball on the previous page, we supplied the diameter we wanted to use:

b = new Basketball(4.0);
When the code runs, the parameter givenDiameter will have the user-supplied value (4.0 in this example) so we store it into the instance variable diameter.
diameter = givenDiameter;

A constructor always has the same name as the class and it never has a return type. The constructor is declared public, because we expect it to be called from other classes, as we did in our BasketballTest on the previous page.

3. Methods

The remainder of the code consists of method definitions. These are the operations that we can perform on a Basketball. The methods getDiameter, getCircumference, and isDribbleable are accessor methods that return some information about the basketball's current state. Each such method includes a return statement with the value to be reported and a return type in its first line indicating the type of the value to be returned.

For example, the getCircumference method has a return type of double. In the method body between the curly braces, we compute the circumference from the diameter, and then return that value to the caller.

public double getCircumference()
{
  double result = Math.PI * diameter;
  return result;
}

The method inflate is a mutator method that changes the state of the object by updating the instance variable isInflated. It doesn't return any value, so it is declared with the return type void.

public void inflate()
{
  isInflated = true;
}