Requirements : //Code in simple java language As per usual, be sure to leave som
ID: 3855980 • Letter: R
Question
Requirements:
//Code in simple java language
As per usual, be sure to leave some comments (in your code) on how certain complex / unique parts of your program work.
Be sure to follow the instructions outlined [below] for each class / function EXACTLY as described. This includes small things such as making sure your methods are defined with the right names, using the appropriate / necessary data type for your parameter(s) / return type, etc.
Your final solution should NOT have any print statements anywhere in either file. You can of course utilize print statements to test your code / numbers as your developing this program, just be sure to delete them once you're done.
Tips:
Be sure to complete "step 1" before moving on to "step 2"; as the class described in step 2 is dependent on the class made during step 1.
You may notice that the formulas for the "intersect()" functions share a common aspect, so I recommend creating a function to deal with that portion of the needed calculation. Maybe make it a static function in class 'Point' which works with two 'Point' parameters. Think about it.
If you're unsure about a particular aspect the I didn't go over or purposely left vague for you to figure out on your own, yet you can't, feel free to contact me with your questions.
Be sure to test your classes function-by-function as you're developing your program and NOT all at once at the end.
STEP 1: Create a class called 'Point' with the following specifications.
NOTE: Nothing within this class should be private. Also these should NOT be static.
Attributes:
A double variable called "X"
A double variable called "Y"
Constructors:
A default constructor which will initialize the 'X' and 'Y' fields with zero.
A "Point(double, double);" constructor that'll assign the parameters' values into the 'X' and 'Y' fields
{ 1st parameter => X & 2nd parameter => Y }
------------------------------------------------------------------------------------------------------------
STEP 2: Create a class called 'Circle' with the following specifications.
NOTE: Nothing within this class should be private. Also these should NOT be static.
Attributes:
A double variable called "radius"
A Point object called "center"
HINT: (refer back to lecture on Aggregation)
Constructors:
A default constructor which will initialize the 'radius' field with zero.
NOTE: Make sure that 'center' is initialized with a "(0, 0) point" here.
A "Circle(double);" constructor that'll assign the parameter's value into the 'radius' field.
REQUIREMENT: You MUST invoke the class' own mutator method to handle this assignment.
NOTE: Make sure that 'center' is initialized with a "(0, 0) point" here.
A "Circle(Point, double);" that'll assign the parameters into their respective fields.
REQUIREMENT: You MUST invoke the class' own mutator method to handle the assignment operation for 'radius'.
Methods:
A mutator function, for attribute 'radius', called "setRadius".
REQUIREMENT: Make sure that if the parameter's value is negative, then the assigned value should be zero instead.
A function, with NO parameters, called "getArea" which (as the name implies) will calculate and return what the area of our Circle is.
Formula: (PI * R ^ 2)
NOTE: You can simply use the value of 3.14159 for 'PI'.
A function, with NO parameters, called "getCircumference" which (as the name implies) will calculate and return what the perimeter of our Circle is.
Formula: (2 * PI * R)
NOTE: You can simply use the value of 3.14159 for 'PI'.
A function called "intersect" with a 'Point' parameter which will return true if the argument lies within the Circle's area (including the edge), otherwise it'll return false.
TIP: The easiest way to figure this out is to compare the lengths of the radius and the distance between the two Points. If you don't know how to calculate a square root, here's an alternate formula you can use:
(p2.Y p1.Y)2 + (p2.X p1.X)2 radius2
EXTRA CREDIT: An overloaded "intersect" function with a 'Circle' parameter which will return true if the argument intersects with this Circle object (including the edge), otherwise it'll return false.
TIP: The easiest way to figure this out is to compare the lengths of the sum of the radii and the distance between the two center Points. If you don't know how to calculate a square root, here's an alternate formula:
(p2.Y p1.Y)2 + (p2.X p1.X)2 (radius_A + radius_B)2
Explanation / Answer
class Point
{
public double X;
public double Y;
public Point(double X,double Y)//argument constructor
{
this.X = X;
this.Y = Y;
}
}
class Circle
{
public double radius;
public Point center;
public Circle()//default constructor
{
radius = 0;
center.X = 0;
center.Y = 0;
}
//argument constructors
public Circle(double radius)
{
this.radius = radius;
center.X = 0;
center.Y = 0;
}
public Circle(double radius,Point center)
{
this.radius = radius;
this.center = center;
}
public void setRadius(double radius)
{
if(radius < 0)
this.radius = 0;
else
this.radius = radius;
}
public double getArea()
{
return 3.14159*radius*radius;
}
public double getCircumference()
{
return 2*3.14159*radius;
}
public boolean intersect(Point p)// check if circle intersects point p
{
if((p.Y - this.center.Y)*2 + (p.X - this.center.X)*2 <= radius*radius)
return true;
else
return false;
}
public boolean intersect(Circle c)// check if circle intersects circle c
{
if((c.center.Y - this.center.Y)*2 + (c.center.X - this.center.X)*2 <= (this.radius + c.radius)*2)
return true;
else
return false;
}
}
class TestCircle
{
public static void main (String[] args)
{
Point p1 = new Point(4.5,6.3);
Circle c1 = new Circle(10,p1);
System.out.println(" Area of circle c1 = "+c1.getArea());
System.out.println(" Circumference of circle c1 = "+c1.getCircumference());
Point p2 = new Point(5.6,7.4);
Circle c2 = new Circle(3,p2);
System.out.println(" Area of circle c2 = "+c2.getArea());
System.out.println(" Circumference of circle c2 = "+c2.getCircumference());
System.out.println(" Is circle c1 is intersected by point p2 : "+c1.intersect(p2));
System.out.println(" Is circle c1 is intersected by circle c2 : "+c1.intersect(c2));
}
}
Output:
Area of circle c1 = 314.159
Circumference of circle c1 = 62.8318
Area of circle c2 = 28.274309999999996
Circumference of circle c2 = 18.849539999999998
Is circle c1 is intersected by point p2 : true
Is circle c1 is intersected by circle c2 : true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.