JAVA Design a program (name it Circles) to determine if a circle is either compl
ID: 3751711 • Letter: J
Question
JAVA
Design a program (name it Circles) to determine if a circle is either completely inside, overlapping with, or completely outside another circler. The program asks the user to enter the center point (X1, Y1) and the radius (R1) for the first circle C1, and the center point (X2, Y2) and the radius (R2) for the second circle C2. The program then determines if the second circle C2 is either completely inside, or overlapping with, or completely outside the first circle C1. Hint: use the sum of R1 and R2 and the distance between the centers to solve the problem. Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs.
Sample run 1:
Circle 1 center is: (0,0)
Circle 1 radius is: 6
Circle 2 center is: (1,1)
Circle 2 radius is: 1
Judgment: Circle 2 is completely inside circle 1
Explanation / Answer
CircleInsideOrOverLaps.java
import java.util.Scanner;
public class CircleInsideOrOverLaps {
public static void main(String[] args) {
//Declaring variables
int x1, x2, y1, y2;
int r1, r2;
//Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//getting the center of the first circle (x1,y1)
System.out.print("Please enter X1: ");
x1 = sc.nextInt();
System.out.print("Please enter y1: ");
y1 = sc.nextInt();
//Getting the radius of circle 1
System.out.print("Please enter Radius 1: ");
r1 = sc.nextInt();
//getting the center of the second circle (x2,y2)
System.out.print("Please enter X2: ");
x2 = sc.nextInt();
System.out.print("Please enter Y2: ");
y2 = sc.nextInt();
//Getting the radius of circle 2
System.out.print("Please enter Radius 2: ");
r2 = sc.nextInt();
//calling the method by passing the points as arguments
double distance = calcDistance(x1, y1, x2, y2);
//Checking whether the circle2 lies inside , outside or overlaps circle1
if (distance < Math.abs(r1 - r2)) {
System.out.println("Circle2 is completely inside Circle1");
} else if (distance == (r1 + r2)) {
System.out.println("Circle2 overlaps Circle 1");
} else {
System.out.println("Circle 2 is completely inside circle 1");
}
}
//Calculating the distance between two points
private static double calcDistance(int x1, int y1, int x2, int y2) {
return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
}
}
_________________
Output:
Please enter X1: 0
Please enter y1: 0
Please enter Radius 1: 6
Please enter X2: 1
Please enter Y2: 1
Please enter Radius 2: 1
Circle2 is completely inside Circle1
________Could you plz rate me well.Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.