Java Program to Check Whether Given Circle Resides in Boundary Maintained by Two Other Circles

In the previous article, we have seen Java Program to Find Circumference of a Circle

In this article we are going to see how to check whether given circle resides in boundary maintained by two other circles using Java programming language.

Java Program to Check Whether Given Circle Resides in Boundary Maintained by Two Other Circles

Before Jumping into the program directly let’s see how we can check whether given circle resides in boundary maintained by two other circle.

Explanation:

Let there are 2 circles named ‘A‘ and ‘B‘ having common center c1(0,0)

Given radius of outer circle = R

and radius of inner circle = r, both are drawn from the same center c1(0,0).

Now a new circle ‘C‘ is to be formed outside the smaller circle but inside the bigger circle.

So, let the coordinates of the center of new circle be c2(x,y)

And the radius of new circle = rad

In order to Check Whether Given Circle Resides in Boundary Maintained by Two Other Circle or not

We have a formula to calculate the distance between the center (0,0) and the co-ordinates of the circle to be checked.

D = √(x2 + y2)

If D+rad <= R and D-rad >= R-r, then the circle fits or else doesn’t fit.

Example:

x=1

y=2

R=3

r=4

rad=5

D= √(x2+y2) = √(1+4) = √5

Now, D+rad <= R and D-rad >= R-r

=> √5+5 <= 3 and 5-√5 >= -1

=> (False) and (False) = False, therefore circle doesn’t fit according to logic

Let’s see different ways to check whether given circle resides in boundary maintained by two other circles.

Method-1: Java Program to Check Whether Given Circle Resides in Boundary Maintained by Two Other Circle By Using Static Value

Approach:

  • Declare an integer variable say ‘x’, ‘y’ and assign the value to it, which holds the coordinate value of the center of the new circle C.
  • Declare a double variable say ‘R’ and assign the value to it, which holds the value of radius of the bigger circle A.
  • Declare a double variable say ‘r’ and assign the value to it, which holds the value of radius of the smaller circle B.
  • Declare a double variable say ‘rad’ and assign the value to it, which holds the value of radius of the new circle C.
  • Now Declare a double variable say ‘D’ and find the value of it, which holds the value of distance between the common center of 2 circles A,B and the co-ordinates of the new circle C using the formula D = √(x2 + y2)
  • Now check if D + rad <= R and D – rad >= R – r then print “circle fits” else “circle doesn’t fit”

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
        int x = 1 ;
        int y =2 ;
        double R =3 ;
        double r = 4;
        double rad = 5;
        // formula to find distance between the common center of 2 circles A,B 
        // and the co-ordinates of the new circle C
        double D =Math.sqrt((x*x) + (y*y));
        // Checking the corners of circle 
        if (D + rad <= R && D - rad >= R - r)  
            System.out.println("Circle Fits");  
        else
            System.out.println("Circle Doesn't Fit");
    }
}

Output:

Circle Doesn't Fit

Method-2: Java Program to Check Whether Given Circle Resides in Boundary Maintained by Two Other Circle By Using User Input Value

Approach:

  • Declare an integer variable say ‘x’, ‘y’, it holds the coordinate value of the center of the new circle C.
  • Declare a double variable say ‘R’ ,  it holds the value of radius of the bigger circle A.
  • Declare a double variable say ‘r’ , it holds the value of radius of the smaller circle B.
  • Declare a double variable say ‘rad’ , it holds the value of radius of the new circle C.
  • Then we will take the value of “x”, “y”, “R”, “r”, “rad”, as user input using scanner class.
  • Now Declare a double variable say ‘D’ and find the value of it, which holds the value of distance between the common center of 2 circles A,B and the co-ordinates of the new circle C using the formula D = √(x2 + y2)
  • Now check if D + rad <= R and D – rad >= R – r then print “circle fits” else “circle doesn’t fit”

Program:

import java.util.*;
class Main
{
    public static void main(String [] args)
    {
        // scanner class obj ref
        Scanner s = new Scanner(System.in);  
        System.out.println("Enter the value of center coordinate x of new circle C:");
        int x = s.nextInt(); // to take user input value
        System.out.println("Enter the value of center coordinate y of new circle C:");
        int y =s.nextInt();
        System.out.println("Enter the value of radius R of bigger circle A:");
        double R =s.nextDouble();
        System.out.println("Enter the value of radius r of smaller circle B:");
        double r = s.nextDouble();
        System.out.println("Enter the value of radius rad of new circle C:");
        double rad = s.nextDouble();

        // formula to find distance between the common center of 2 circles A,B 
        // and the co-ordinates of the new circle C
        double D =Math.sqrt((x*x) + (y*y));
        // Checking the corners of circle 
        if (D + rad <= R && D - rad >= R - r)  
            System.out.println("Circle Fits");  
        else
            System.out.println("Circle Doesn't Fit");
    }
}

Output:

Case-1:
Enter the value of center coordinate x of new circle C:
5
Enter the value of center coordinate y of new circle C:
3
Enter the value of radius R of bigger circle A:
8
Enter the value of radius r of smaller circle B:
4
Enter the value of radius rad of new circle C:
1
Circle Fits

Case-2:
Enter the value of center coordinate x of new circle C:
5
Enter the value of center coordinate y of new circle C:
3
Enter the value of radius R of bigger circle A:
7
Enter the value of radius r of smaller circle B:
3
Enter the value of radius rad of new circle C:
3
Circle Doesn't Fit

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Articles: