Question
Complete the following from instructions above. class Point implements Shape { final int x, y; Point(int x, int y) { this.x = x; this.y =
Complete the following from instructions above.
class Point implements Shape {
final int x, y; Point(int x, int y) { this.x = x; this.y = y; } public boolean equals(Object that) { // TODO } public int hashCode() { // TODO } public String toString() { // TODO } } class Line implements Shape { final Point p1, p2; Line(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } public boolean equals(Object that) { // TODO } public int hashCode() { // TODO } public String toString() { // TODO } } class Triangle implements Shape { final Point p1, p2, p3; Triangle (Point p1, Point p2, Point p3) { this.p1 = p1; this.p2 = p2; this.p3 = p3; } public boolean equals(Object that) { // TODO } public int hashCode() { // TODO } public String toString() { // TODO } }
--------------------------------------------------------------------------------------------
Driver:
import java.util.HashSet;
import java.util.Set;
public class Assignment {
public static void main(String[] args) {
Set s = new HashSet();
Point[] points = {new Point(1,2), new Point(3, 4), new Point(4, 3), new Point(2, 1),
new Point(1,2), new Point(3, 4), new Point(4, 3), new Point(2, 1) };
for(Point p: points) { s.add(p); }
System.out.println(s);
Line l1 = new Line(points[0], points[1]);
Line l2 = new Line(points[1], points[0]);
s.add(l1);
s.add(l2);
System.out.println(s);
Triangle p1 = new Triangle(points[0], points[1], points[2]),
p2 = new Triangle(points[1], points[2], points[0]),
p3 = new Triangle(points[2], points[0], points[1]),
p4 = new Triangle(points[2], points[1], points[0]),
p5 = new Triangle(points[1], points[0], points[2]),
p6 = new Triangle(points[0], points[2], points[1]);
s.add(p1);
s.add(p2);
s.add(p3);
s.add(p4);
s.add(p5);
s.add(p6);
System.out.println(s);
}
}
interface Shape {}
--------------------------------------------------------------------------------------------------------- Sample Output When you run the main method in the provided code template, you should get output similar to the following. [(1,2), (2,1), (3,4), (4,3)] [(1,2), (2,1), (3,4), (4,3), Line [(1,2), (3,4)]] [Triangle [(1,2), (3,4), (4,3)], (1,2), (2,1), (3,4), (4,3), Line [(1,2), (3,4)]]
You will complete the class Point, Line, and Triangle in the provided code template. A point (x,y) is equal to the point (z,v) ifx == ?, and y-= y, A line (p1,P2 s equal to the line (pI,p2) if either . P1 equals pl, P2 equals pa or . Pi equals pg, P2 equals pi. A triangle (pi,p2.Pa) is equal to the triangle (p.p,%) if either pi equals p1 P2 equals p2, Pa equals or P1 equals p, P2 equals p, P3 equals pl or Pi equals p3 P2 equals P1 Ps equals p2 or . P1 equals P4, P2 equals P4, P3 equals?or ? P1 equals p, p2 equals pl, p3 equals p?or * pi equals p, P2 equals p3, Ps equals p2 Your implementation of equals and hashCode should be consistent so that two equal objects will not coexist in a HashSetStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started