Intersection/touching of two circles in Java using enums

I was asked to create two objects of class circle, enter the co-ordinates of the circle and the radius. After finding the distance and the square root of the radius, it should print if the circles intersect/touch, etc.

I should use an enum inside the class. Any idea of how to implement my code using an enum? I only got introduced to enums, so I’m not too sure what it is.

import java.util.*;
public class circle
    float x,y,r;

    public static int result(float x1, float y1, float r1, float x2, float y2, float r2)
        float sqrt_Dist_Circle = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
        float sqrt_Rad_Circle = (r1 + r2) * (r1 + r2);
        if (sqrt_Dist_Circle == sqrt_Rad_Circle)
            return 1;
        else if (sqrt_Dist_Circle > sqrt_Rad_Circle)
            return -1;
            return 0;

    enum Interaction {

    Interaction Interacts(circle c2)
        if (x==c2.x)
            return Interaction.MATCHES;
        return Interaction.NONE;

    public static void main(String[] args)
        Scanner sc = new Scanner(;
        circle obj1 = new circle();
        circle obj2 = new circle();
        System.out.println("Enter the co-ordinates and radius of the first circle : ");
        System.out.print("x1 = ");
        System.out.print("y1 = ");
        System.out.print("r1 = ");
        System.out.println("Enter the co-ordinates and radius of the second circle : ");
        System.out.print("x2 = ");
        System.out.print("y2 = ");
        System.out.print("r2 = ");
                float t = result(obj1.x, obj1.y, obj1.r,obj2.x,obj2.y,obj2.r);
                if (t == 1)
                    System.out.println ( "Circles touch each other.");
                else if (t < 0)
                    System.out.println ( "Circles do not touch each other.");
                    System.out.println ( "Circles intersect each other.");


An enum is basically a class with the primary purpose of holding constants of itself. It’s useful when you have a set number of options you want something to represent, e.g. in your case two circles can have exactly one of the listed interaction behaviors. So instead of your result function returning an integer, you could simply return Interaction, which would allow you to write return Interaction.MATCHES. Enums also allow you to use a switch case on them, so your final string output could look similar to this:

Interaction interaction = result(...);
switch (interaction) {
    case NONE:
        System.out.println("Circles do not touch each other.");
    case INTERSECTS:
        System.out.println("Circles intersect each other.");

Leave a Reply

Your email address will not be published. Required fields are marked *