import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * This class represents a triangle. A triangle has three points. * * @author Franck van Breugel */ public class Triangle { private Point first; private Point second; private Point third; //@ invariant this.first != null && this.second != null && this.third != null /** * Initializes this triangle with the given points. * * @param first a point of this triangle. * @pre. first != null * @param second a point of this triangle. * @pre. second != null * @param third a point of this triangle. * @pre. third != null */ public Triangle(Point first, Point second, Point third) { this.setFirst(first); this.setSecond(second); this.setThird(third); } /** * Initializes this triangle as a deep copy of the triangle. * * @param triangle a triangle. * @pre. triangle != null */ public Triangle(Triangle triangle) { this(new Point(triangle.getFirst().getX(), triangle.getFirst().getY()), new Point(triangle.getSecond().getX(), triangle.getSecond().getY()), new Point(triangle.getThird().getX(), triangle.getThird().getY())); } /** * Returns the first point of this triangle. * * @return the first point of this triangle. */ public Point getFirst() { return this.first; } /** * Sets the first point of this triangle to the given point. * * @param first the new first point of this triangle. * @pre. first != null */ private void setFirst(Point first) { this.first = first; } /** * Returns the second point of this triangle. * * @return the second point of this triangle. */ public Point getSecond() { return this.second; } /** * Sets the second point of this triangle to the given point. * * @param second the new second point of this triangle. * @pre. second != null */ private void setSecond(Point second) { this.second = second; } /** * Returns the third point of this triangle. * * @return the third point of this triangle. */ public Point getThird() { return this.third; } /** * Sets the third point of this triangle to the given point. * * @param third the new third point of this triangle. * @pre. third != null */ private void setThird(Point third) { this.third = third; } /** * Returns the hash code of this triangle. * * @return the hash code of this triangle. */ public int hashCode() { return this.getFirst().hashCode() + this.getSecond().hashCode() + this.getThird().hashCode(); } /** * Returns the points of this triangle as a sorted list. * * @return the points of this triangle as a sorted list. */ private List toList() { List list = new ArrayList(); list.add(this.getFirst()); list.add(this.getSecond()); list.add(this.getThird()); Collections.sort(list); return list; } /** * Tests whether this triangle is the same as the given object. * Two triangles are the same if they have the same points. * * @param object an object. * @return true if this triangle is the same as the given object, * false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { Triangle other = (Triangle) object; equal = this.toList().equals(other.toList()); } else { equal = false; } return equal; } /** * Returns a string representation of this triangle. * * @return a string representation of this triangle. */ public String toString() { return this.toList().toString(); } /** * Returns an alias of this triangle. * * @return an alias of this triangle. */ public Triangle getAlias() { return this; } /** * Returns a shallow copy of this triangle. * * @return a shallow copy of this triangle. */ public Triangle getShallowCopy() { return new Triangle(this.getFirst(), this.getSecond(), this.getThird()); } /** * Returns a deep copy of this triangle. * * @return a deep copy of this triangle. */ public Triangle deepCopy() { Point first = new Point(this.getFirst().getX(), this.getFirst().getY()); Point second = new Point(this.getSecond().getX(), this.getSecond().getY()); Point third = new Point(this.getThird().getX(), this.getThird().getY()); return new Triangle(first, second, third); } }