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;
        else
            return 0;
    }

    enum Interaction {
        MATCHES,INTERSECTS,TOUCHES,NONE,NESTS,NESTED
    }

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

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        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 = ");
        obj1.x=sc.nextFloat();
        System.out.print("y1 = ");
        obj1.y=sc.nextFloat();
        System.out.print("r1 = ");
        obj1.r=sc.nextFloat();
        System.out.println("Enter the co-ordinates and radius of the second circle : ");
        System.out.print("x2 = ");
        obj2.x=sc.nextFloat();
        System.out.print("y2 = ");
        obj2.y=sc.nextFloat();
        System.out.print("r2 = ");
        obj2.r=sc.nextFloat();
                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.");
                else
                    System.out.println ( "Circles intersect each other.");
    }
}

Answer

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.");
        break;
    case INTERSECTS:
        System.out.println("Circles intersect each other.");
        break;
    ....
}

Leave a Reply

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