Sunteți pe pagina 1din 2

Exercise – Defining a Circle Class

Part 1.

The file Circle.java contains a partial definition for a class representing a circle. Save it
to your computer and study it to see what methods it contains. Then complete the Circle
class as described below. Note that you won't be able to test your methods until you
complete the test client class called CircleTester.java in part 2.

a) Declare three private double instance variables: x, y and radius. The instance
variables x and y represent the coordinates of the center of the circle.
b) Fill in the code for method toString, which should return a string containing the
center of the circle, x and y, and the radius of the circle.
c) Fill the code for the accessors: getX, getY and getRadius properly.
d) Fill the code for the mutators: setX, setY and setRadius properly. Note that the
setRadius will set the radius to the new value passed as parameter to the method only
when this new value is greater than or equal to 0.
e) Fill the code for the method area that returns the area of the circle. The area of a
circle is computed as:
PI*radius2

Use the method pow of the class Math to compute the square of a number in the
following manner:

double number=3.5;
double sqrNumber = Math.pow(number,2);

f) Use the value of PI provided by the constant Math.PI.


g) Fill the code of the method perimeter that returns the perimeter of the circle as:
2*PI*radius
h) Fill the code of the method diameter that returns the diameter of the circle.
i) Fill the code of the method isUnitCircle that returns true if the radius of the circle is 1
and is centered at the origin, i.e., the coordinates of the circles center are (0,0) and
false otherwise.

Part 2.

The file CircleTester.java contains a shell program that uses the Circle class above. Save
it to your computer and complete it as indicated by the comments in the file as follows.
a) Display the center and radius of circle1.
b) Set the radius of circle2 to 5.3
c) Display the center and radius of circle2.
d) Display the diameter, area and perimeter of circle1.
e) Display the diameter, area and perimeter of circle2.
f) Display a message that indicates whether or not circle1 is a unit circle.
g) Display a message that indicates whether or not circle2 is a unit circle.

Compile your application. Then, set a breakpoint on the first line of each method of the
class Circle and on the first line of the tester client CircleTester. Afterwards, run your
java project in debug mode to observe the flow of control.

S-ar putea să vă placă și