I have a problem with my homework, and I have problems with the work of the last part. What am I doing wrong here? It will not print “Circle 2 overlaps circle 1”. or "Circle 2 does not overlap circle 1". The first two cases of printing. Here is the question:
Write a program (TwoCircles.java) in which the user enters the central coordinates and radii of two circles and determines the geometric relationship between them and prints one of the following messages: 1. Circle 1 is inside circle 2. 2. Circle 2 is inside circle 1. 3 Circle 2 overlaps circle 1. 4. Circle 2 does not overlap circle 1.
Here is what I have for my code:
import java.util.Scanner;
public class TwoCircles {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Circle 1 center x-, y-coordinates, and radius: ");
double X1 = input.nextDouble();
double Y1 = input.nextDouble();
double radius1 = input.nextDouble();
System.out.print("Enter Circle 2 center x-, y-coordinates, and radius: ");
double X2 = input.nextDouble();
double Y2 = input.nextDouble();
double radius2 = input.nextDouble();
double distance = Math.pow((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2), 0.5);
if (radius2 >= radius1){
if (distance <= (radius2 - radius1))
System.out.println("Circle 1 is inside Circle 2.");}
else if (radius1 >= radius2){
if (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.");}
}
}
Any guidance appreciated
source
share