|
At a minimum, what variables are needed? A good answer might be:The (X, Y) location of the center of the circle, and the radius. | |
Constructors
It would have been OK to have answered that the (X, Y) location of the upperleft corner, and the width and height of the containing rectangle are needed. But if Circle objects are only going to be used for circles (not ovals) the center and radius are better. Here is the class definition using this decision, with two constructors included: The first constructor has no parameters. It initializes the variables in the object to default values of zero. It is often useful to have a constructor like this, especially for objects who's data is expected to change. The second constructor allows the user to specify a value for each of the object's variables. It is convenient to call the parameters "x", "y", and "radius," but there is a slight problem. If the parameter "x" were used to initialize the object's variable "x" you might expect to see:
This would not do what you want. It would copy the value in the parameter x into itself; a do-nothing operation! Instead, the statement should be
The reserved word this is used to indicate this object's variable.
| |
QUESTION 4:Complete the constructor:
Click Here after you have answered the question
|