-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircle2D.java
More file actions
89 lines (71 loc) · 2.63 KB
/
Copy pathCircle2D.java
File metadata and controls
89 lines (71 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public class Circle2D {
// Data fields
private double x;
private double y;
private double radius;
// No-arg constructor: default circle (0, 0) center and radius 1
public Circle2D() {
this.x = 0;
this.y = 0;
this.radius = 1;
}
// Constructor with specified x, y, and radius
public Circle2D(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
// Getter methods
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getRadius() {
return radius;
}
// Return the area of the circle
public double getArea() {
return Math.PI * radius * radius;
}
// Return the perimeter (circumference) of the circle
public double getPerimeter() {
return 2 * Math.PI * radius;
}
// Return true if the specified point (x, y) is inside this circle
public boolean contains(double x, double y) {
double distance = Math.sqrt(Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2));
return distance <= radius;
}
// Return true if the specified circle is inside this circle
public boolean contains(Circle2D circle) {
double distance = Math.sqrt(Math.pow(circle.x - this.x, 2) + Math.pow(circle.y - this.y, 2));
return distance + circle.radius <= this.radius;
}
// Return true if the specified circle overlaps with this circle
public boolean overlaps(Circle2D circle) {
double distance = Math.sqrt(Math.pow(circle.x - this.x, 2) + Math.pow(circle.y - this.y, 2));
return distance < this.radius + circle.radius && distance > Math.abs(this.radius - circle.radius);
}
}
import java.util.Scanner;
public class TestCircle2D {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// User-created circle
System.out.print("Enter x, y, and radius: ");
double x = input.nextDouble();
double y = input.nextDouble();
double radius = input.nextDouble();
Circle2D userCircle = new Circle2D(x, y, radius);
// Default circle
Circle2D defaultCircle = new Circle2D();
System.out.println("Area: " + userCircle.getArea());
System.out.println("Perimeter: " + userCircle.getPerimeter());
System.out.println("Contains point (1,1)? " + userCircle.contains(1, 1));
System.out.println("User circle inside default circle? " + defaultCircle.contains(userCircle));
System.out.println("Circles overlap? " + defaultCircle.overlaps(userCircle));
input.close();
}
}