Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To be done using java and netbeans. Only need the working code. Two double data

ID: 3564750 • Letter: T

Question

To be done using java and netbeans. Only need the working code.

Two double data fields named x and y that specify the center of the circle with getter methods. A data field radius with a getter method. A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. A constructor that creates a circle with the specified x, y, and radius. A method getArea() that returns the area of the circle. A method getPerimeter () that returns the perimeter of the circle. A method contains (double x, double y) that returns true if the specified point (x, y) is inside this circle (see Figure 10.21a). A method contains(Circle2D circle) that returns true if the specified circle is inside this circle (see Figure 10.21b). A method overlaps (Circle2D circle) that returns true if the specified circle overlaps with this circle (see Figure 10.21c). FIGURE 10.21 (a) A point is inside the circle, (b) A circle is inside another circle, (c) A circle overlaps another circle. Draw the UML diagram for the class and then implement the class. Write a test program that creates a Circle2D object c1 (new Circle2D(2, 2, 5.5)), displays its area and perimeter, and displays the result of c1.contains(3, 3), c1.contains(new Circle2D(4, 5, 10.5)), and c1.overlaps (new Circle2D(3, 5, 2.3)).

Explanation / Answer

// Circle2D.java

public class Circle2D
{
private double x, y;
private double radius;
double getRadius()
{
return radius;
}
Circle2D()
{
x = 0;
y = 0;
radius = 1;
}
Circle2D(double a, double b, double r)
{
x = a;
y = b;
radius = r;
}
double getArea()
{
return 3.14 * radius * radius;
}
double getPerimeter()
{
return 2.0 * 3.14 * radius;
}
boolean contains(double x, double y)
{
double temp = (x - this.x) * (x - this.x) + (y - this.y) * (y - this.y);
if(temp < radius * radius) return true;
else return false;
}
boolean contains(Circle2D circle)
{
double temp = (circle.x - x) * (circle.x - x) + (circle.y - y) * (circle.y - y);
if(temp < radius * radius) return true;
else return false;
}
boolean overlaps(Circle2D circle)
{
double temp = (circle.x - x) * (circle.x - x) + (circle.y - y) * (circle.y - y);
if(temp <= radius + circle.radius && temp >= Math.abs(radius - circle.radius)) return true;
else return false;
}
}

// Circle2DTest.java

import java.util.Scanner;
public class Circle2DTest
{
public static void main(String args[])

{
double a1, b1, r1, p, q, a2, b2, r2;
Scanner in = new Scanner(System.in);
System.out.print("Enter the centre coordinates of the first circle: ");
a1 = in.nextDouble();
b1 = in.nextDouble();
System.out.print("Enter the radius of the first circle: ");
r1 = in.nextDouble();
Circle2D circle1 = new Circle2D(a1, b1, r1);
System.out.println("Area = " + circle1.getArea());
System.out.println("Perimeter = " + circle1.getPerimeter());
System.out.print(" Enter the coordinates of a point: ");
p = in.nextDouble();
q = in.nextDouble();
if(circle1.contains(p, q)) System.out.println("The point is inside the Circle");
else System.out.println("The point is outside the Circle");

System.out.print(" Enter the centre coordinates of the second circle: ");
a2 = in.nextDouble();
b2 = in.nextDouble();
System.out.print("Enter the radius of the second circle: ");
r2 = in.nextDouble();
Circle2D circle2 = new Circle2D(a2, b2, r2);
if(circle1.contains(circle2)) System.out.println("The second circle is inside the first circle");
else System.out.println("The second circle is outside the first circle");
if(circle1.overlaps(circle2)) System.out.println(" The second overlaps the first circle");
else System.out.println(" The second circle does not overlap the first circle");
}
}


/*
output:

Enter the centre coordinates of the first circle: -5 6
Enter the radius of the first circle: 8
Area = 200.96
Perimeter = 50.24

Enter the coordinates of a point: 4 5
The point is outside the Circle

Enter the centre coordinates of the second circle: 0 1
Enter the radius of the second circle: 9
The second circle is inside the first circle

The second circle does not overlap the first circle

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote