Complete the following code to test whether two circles, each having a user-defi
ID: 640169 • Letter: C
Question
Complete the following code to test whether two circles,
each having a user-defined radius and a fixed center point lying along the same horizontal line, are disjoint, overlapping, or mutually contained.
public class CircleOverlap {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input the radius of the first circle: ");
double radius1 = in.nextDouble();
double xcenter1 = 0;
double ycenter1 = 0;
System.out.print("Input the radius of the second circle: ");
double radius2 = in.nextDouble();
double xcenter2 = 40;
double ycenter2 = 0;
// Your work goes here
}
}
Disjoint Mutually contained OverlappingExplanation / Answer
//Java program prompts for radii of two circles
//and prints if the circle 1 is contained int,disjoint or coincident .
//CircleOverlap.java
import java.util.Scanner;
public class CircleOverlap
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double xcenter1 = 0;
double ycenter1 = 0;
//read radius of the circle1
System.out.print("Circle 1:Input the radius of the first circle: ");
double radius1 = in.nextDouble();
double xcenter2 = 40;
double ycenter2 = 0;
System.out.print("Circle 2:Input the radius of the second circle: ");
//read radius of the circle2
double radius2 = in.nextDouble();
//condtions for disjoint,contained and coincident of two circles
/*If d > r0 + r1 then there are no solutions, the circles are separate.
If d < |r0 - r1| then there are no solutions because one circle is contained within the other.
If d = 0 and r0 = r1 then the circles are coincident and there are an infinite number of solutions. */
//Calculating the distance between two circles
//d=Squrt((x1-x2)+(y1-y2))
double distance = Math.pow((xcenter1 - xcenter2) * (xcenter1 - xcenter2) +
(ycenter1 - ycenter2) * (ycenter1 - ycenter2), 0.5);
if (radius2 >= radius1 && distance <= (radius2 - radius1))
{
System.out.println("Circle 1 is inside Circle 2.");
}
else if (radius1 >= radius2 && distance <= (radius1 - radius2) )
{
System.out.println("Circle 2 is inside Circle 1.");
}
else if (distance > (radius1 + radius2))
{
System.out.println("Circle 2 does not overlap Circle 1.");
}
else
{
System.out.println("Circle 2 overlaps Circle 1.");}
}
}//end of the class
------------------------------------------------------------------------------------
Sample output:
Circle 1:Input the radius of the first circle: 10
Circle 2:Input the radius of the second circle: 15
Circle 2 does not overlap Circle 1.
Hope this helps you..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.