I trying to create a 2d shapes for my assignment. For that I will first creating an abstract Shape2d class. My assignment clearly indicates that I need to override the equals(Object o)
method in the object class which will compare 2d shapes. I made the comparisons with comparing the shapes area and the perimeter disrespect to position in the 2d plane. But the problem here is, I cannot use the abstract methods of the Abstract class Shape2d. Since I need to override the method equals(Objcet o)
I cannot convert into equals(Shape2d o)
. What I can I do with that ? There is my code in the below.
/** This class is an abstract class and it is created for to be inherited specialized 2d shapes
* @version 02.26.2021
*/
public abstract class Shape2d
{
private int xLocation;
private int yLocation;
public Shape2d(int xLocation, int yLocation)
{
this.xLocation = xLocation;
this.yLocation = yLocation;
}
/** Getting the yLocation
* @version 02.26.2021
* @return
*/
public int getYLocation()
{
return yLocation;
}
/** Getting the xLocation
* @version 02.26.2021
* @return
*/
public int getXLocation()
{
return xLocation;
}
/** Abstact method which will be used for other 2d objects. Calculating the area of the 2d shape
* @version 02.26.2021
* @return
*/
public abstract double calculateArea();
/** Abstact method which will be used for other 2d objects. Calculating the perimeter of the 2d shape
* @version 02.26.2021
* @return
*/
public abstract double calculatePerimeter();
/** This method will calculate the distance of the two objects in absolute values.
* @param anyShape
* @return
*/
public double calculatingDistance(Shape2d anyShape)
{
if(anyShape instanceof Shape2d)
{
return Math.sqrt(Math.abs(Math.pow(yLocation - anyShape.yLocation,2) + Math.pow(xLocation - anyShape.xLocation, 2)));
}
return -1;
}
@Override
/** This method is Overrided. It will return the Cordinates of the center (x,y)
* @version 02.26.2021
*/
public String toString()
{
return "Center Cordinates of the Shape is: (" + xLocation +"," + yLocation + ")\n";
}
/** This method is overriding the equals method for finding out whether or not this shapes are the same
* disrespect to its position in the
* @version 02.26.2021
* @param anyShape
* @return
*/
public boolean equals(Shape2d anyShape)
{
return calculateArea() == anyShape.calculateArea() && calculatePerimeter() == anyShape.calculatePerimeter();
}
}