Complete the following code to test whether two circles, each having a user-defi
ID: 3671108 • 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, c mutually contained. Sample Runs (user input in red): Input the radius of the first circle: 10 Input the radius of the second circle: 15 Disjoint Input the radius of the first circle: 5 Input the radius of the second circle: 50 Circle 1 is mutually contained in circle 2 Input the radius of the first circle: 50 Input the radius of the second circle: 5 Circle 1 mutually contains circle 2 Input the radius of the first circle: 15 Input the radius of the second circle: 35 OverlappingExplanation / Answer
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class CircleOverlap {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
// prompt user for radius of circle1
System.out.print("Input the radius of the first circle: ");
double radius1 = scanner.nextDouble();
double xcenter1 = 0;
double ycenter1 = 0;
// prompt user for radius of circle2
System.out.print("Input the radius of the second circle: ");
double radius2 = scanner.nextDouble();
double xcenter2 = 40;
double ycenter2 = 0;
// calculating distance
double distance = Math
.sqrt((Math.pow(xcenter2 - xcenter1, 2) + Math.pow(ycenter2
- ycenter1, 2)));
// checking the circle overlapping or not
if ((distance <= Math.abs(radius1 - radius2))) {
System.out.println("circle1 is mutually contained in circle2");
} else if (distance < (radius1 + radius2)) {
System.out.println("Overlapping");
} else {
System.out.println("Disjoint");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
OUTPUT:
Input the radius of the first circle: 10
Input the radius of the second circle: 15
Disjoint
Input the radius of the first circle: 5
Input the radius of the second circle: 50
circle1 is mutually contained in circle2
Input the radius of the first circle: 50
Input the radius of the second circle: 5
circle1 is mutually contained in circle2
Input the radius of the first circle: 15
Input the radius of the second circle: 35
Overlapping