Add an equals method to the triangle class that you wrote. Two triangles would b
ID: 3623806 • Letter: A
Question
Add an equals method to the triangle class that you wrote. Two triangles would be equal if each of the sides were equal and the colours were the same, and the filled variable was the samethe Triangle class i wrote:
public class Triangle extends GeometricObject
{
double side1=1.0;
double side2=1.0;
double side3=1.0;
Triangle()
{
side1=1.0;
side2=1.0;
side3=1.0;
}
Triangle(double side11,double side22,double side33)
{
side1=side11;
side2=side22;
side3=side33;
}
public double getSide1()
{
return side1;
}
public double getSide2()
{
return side2;
}
public double getSide3()
{
return side3;
}
public double getArea()
{
double s=0.0;
double area=0.0;
s = (side1+side2+side3)/2;
area = Math.sqrt(s*((s-side1)*(s-side2)*(s-side3)));
return area;
}
public double getPerimeter()
{
double area=side1+side2+side3;
return area;
}
public String toString()
{
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
}
}
the GeometricObject class :
public class GeometricObject
{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject()
{
dateCreated = new java.util.Date();
}
public GeometricObject(String Color, boolean filled)
{
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
}
public boolean isFilled()
{
return filled;
}
public void setFilled(boolean filled)
{
this.filled = filled;
}
public java.util.Date getDateCreated()
{
return dateCreated;
}
public String toString()
{
return "created on" + dateCreated + " color : " + color + " and filled : " + filled;
}
}
thanks
Explanation / Answer
public boolean equals(Triangle t) {
return side1 == t.side1 && side2 == t.side2 && side3 == t.side3 && isFilled() == t.isFilled() && getColor().equals(t.getColor());
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.