Unit #7: Programming Assignment#9
Total
Points: 15
NOTES:
· Each
question is worth 7.5 points
· Your
programs should be adequately documented.
Problem
#1:
Write programs to implement the following:
1)
Circle.java
implements a class to represent a circle (see private variables and public
methods below)
2)
CircleTest.java
creates an instance (or object) of the Circle
class and calls all the public methods (see program output below)
Private
Variables:
The class will contain three integer private variables – x, y and r:
·
x
represents the x coordinate of the center of the circle
·
y
represents the y coordinate of the center of the circle
·
r
represents the radius of the circle
Public
Methods:
a)
The class has two constructors:
o
Constructor#1 : a default constructor, Circle() which sets x =
0, y= 0 and r = 0
b)
Constructor #2 : a parameter constructor, Circle (int a, int
b, int c)
which sets x = a, y = b and r = c , where a and , b, and c are specified input values
c)
public
int getX() :
returns the X coordinate of the center of the Circle
d)
public
int getY()
: returns the Y coordinate of the center of the Circle
e)
public
int getRadius()
: returns the radius of the Circle
f)
public
void setX(int a )
: sets the X coordinate of the center of the Circle to equal
the input value, a (specified by the user)
g)
public
void setY (int b )
: sets the Y coordinate of the center of the Circle to equal the input value, b (specified by the user)
h)
public
void setR(int c)
: sets the radius of the Circle to equal
the input value, c (specified by the user)
i)
public
double getArea() : computes
and returns the area of the Circle
j)
public
int checkQuadrant() : returns 1 if the Circle is inscribed in the first quadrant, 2
for second quadrant, 3 for third quadrant and 4 for the fourth quadrant.
For this problem, you can assume
that an input circle will always be inscribed completely within one of the four
quadrants. Hence, no input circle will overlap between more than one
quadrant.
Program output:
Input x Coordinate
5
Input y Coordinate
5
Input radius
3
The center of the
Circle = (5, 5)
Area of the Circle =
28.274
The circle is in
Quadrant#1
Changing the center
and radius of the Circle:
Input X Coordinate
-8
Input y Coordinate
12
Input radius
4
Area = 50.265
The center of the
Circle = (-8, 12)
Area of the Circle =
50.265
The circle is in
Quadrant#2
Problem #2:
Write programs to implement the following:
1)
Circle.java
implements a Circle class as specified above.
2)
CircleTestArray.java
implements the following:
a.
Creates an array (of input size) of type Circle.
b.
Creates and stores an instance of the Circle class for every cell in the array
c.
Takes integer input values for center of the circle (x and y
coordinates) and radius for every Circle object in the array
d.
Computes the area for every Circle
object in the array
e.
Prints the Quadrant# (1 or 2 or 3 or 4) for every Circle object in the array
f.
Prints the number of Circle objects in each quadrant (see program output below)
Program output:
How many circles do
you want to create?
4
Input x Coordinate for
Circle#1
5
Input y Coordinate for
Circle#1
5
Input radius for
Circle#1
2
Area for Circle#1 =
12.566370614359172
Circle#1 is in
Quadrant#1
Input x Coordinate for
Circle#2
6
Input y Coordinate for
Circle#2
-12
Input radius for
Circle#2
3
Area for Circle#2 =
28.274333882308138
Circle#2 is in
Quadrant#4
Input x Coordinate for
Circle#3
-5
Input y Coordinate for
Circle#3
10
Input radius for
Circle#3
4
Area for Circle#3 =
50.26548245743669
Circle#3 is in
Quadrant#2
Input x Coordinate for
Circle#4
-5
Input y Coordinate for
Circle#4
6
Input radius for
Circle#4
1
Area for Circle#4 =
3.141592653589793
Circle#4 is in
Quadrant#2
Number of Circles in
Quadrant#1 = 1
Number of Circles in
Quadrant#2 = 2
Number of Circles in Quadrant#3
= 0
Number of Circles in
Quadrant#4 = 1