The problem Write a program that prompts the user to enter the center coordinates and radii of two circles and determines whether the second circle is inside the first or overlaps with the first. (Hint: circle2 is inside circle1 if the distance between the two centers <= |r1 - r2| and circle2 overlaps circle1 if the distance between the two centers <= r1 + r2. Test your program to cover all cases.)

Breaking it down public static void main ( String [] strings ) {
Scanner input = new Scanner ( System . in );
System . out
. print ( "Enter circle1’s center x and y-coordinates, and radius: " );
double x1 = input . nextDouble ();
double y1 = input . nextDouble ();
double radius1 = input . nextDouble ();
System . out
. print ( "Enter circle2’s center x and y-coordinates, and radius: " );
double x2 = input . nextDouble ();
double y2 = input . nextDouble ();
double radius2 = input . nextDouble ();
input . close ();
double distance = calculateDistance ( x1 , y1 , x2 , y2 );
if ( distance + radius2 <= radius1 ) {
System . out . println ( "circle2 is inside circle1" );
} else if ( distance <= radius1 + radius2 ) {
System . out . println ( "circle2 overlaps circle1" );
} else {
System . out . println ( "circle2 does not overlap circle1" );
}
}
private static double calculateDistance ( double x1 , double y1 , double x2 ,
double y2 ) {
double distance = Math . pow (( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 )
* ( y1 - y2 ), 0.5 D );
return distance ;
}

Output ```
Enter circle1’s center x and y-coordinates, and radius: 0.5 5.1 13
Enter circle2’s center x and y-coordinates, and radius: 1 1.7 4.5
circle2 is inside circle1
```

Geometry two circles posted by Justin Musgrove on 09 April 2016

Tagged: java, java-exercises-beginner, intro-to-java-10th-edition, and ch3

Share on: Facebook Google+