go to previous page   go to home page   go to next page

Answer:

The complete constructor is seen below.


Complete Constructor


public class Cone
{
  // instance variables
  private double radius;  // radius of the base
  private double height;  // height of the cone

  // constructor
  public Cone( double rad, double hei )
  {
    radius = rad;
    height = hei;
  }

  // methods
 
}

The constructor copies values from its parameters to the instance variables of the new object. You might wonder why you need to do this. Why not just leave the data in the parameters? There are two reasons:

  1. The constructor's parameters can be seen only by its own statements. A method such as volume() cannot see the parameters of the constructor.
  2. Data in parameters is temporary. Parameters are used to communicate data, not to hold data.

Think of a parameter as a scrap of paper containing information handed to the constructor. The constructor has to copy the information to someplace permanent that can be seen by the other methods.


QUESTION 9:

The names for the constructor's parameters are awkward. What can be done about this?


go to previous page   go to home page   go to next page