In: Computer Science
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x && this.y == other.y; } // Returns a String like “(4.5, -4.8)” public String toString() { return "(" + x + ", " + y + ")"; } // Creates and returns a new MyPoint based on data from a // Scanner. If there’s nothing to be read in, it returns null. public static MyPoint read(Scanner sc) { if (!sc.hasNext()) return null; double x = sc.nextDouble(); double y = sc.nextDouble(); return new MyPoint(x, y); } } Your job is to write a class called Triangle. Here’s the UML diagram: Triangle - a: MyPoint - b: MyPoint - c: MyPoint + Triangle(a: MyPoint, b: MyPoint, c: MyPoint) + equals(other: Triangle): boolean + toString(): String + perimeterLength(): double + read(sc: Scanner): Triangle Notes: The data fields represent the three corners of a triangle. The equals method should determine if this Triangle equals other. Hint: use the equals method of MyPoint. The toString method should return a String like “a = (5.5, 4.0), b = (34.6, -3.3), c = (0, 6)”. The perimeterLength method should return the length of the perimeter. Hint: use the distance method of MyPoint. The read method should be defined as usual; it should be similar to the read method in part 3 of Codelab Project 2. That is, if there’s nothing more to read in, return null. Otherwise, read in the appropriate number of MyPoints (hint: use the read method of MyPoint) and return a new Triangle.
Please follow inline comments and screenshots for better understanding.
code...
MyPoint.java
import java.util.Scanner;
public class MyPoint {
private double x;
private double y;
public MyPoint() {
this(0, 0);
}
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
// Returns the distance between this MyPoint and other
public double distance(MyPoint other) {
return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2));
}
// Determines if this MyPoint is equivalent to another MyPoint
public boolean equals(MyPoint other) {
return this.x == other.x && this.y == other.y;
}
// Returns a String like "(4.5, -4.8)"
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
// Creates and returns a new MyPoint based on data from a // Scanner. If there’s
// nothing to be read in, it returns null.
public static MyPoint read(Scanner sc) {
if (!sc.hasNext())
return null;
double x = sc.nextDouble();
double y = sc.nextDouble();
return new MyPoint(x, y);
}
}
Triangle.java
import java.util.Scanner;
// class Triangle
public class Triangle {
// private MyPoints a,b,c
private MyPoint a;
private MyPoint b;
private MyPoint c;
// constructor
public Triangle(MyPoint a, MyPoint b, MyPoint c) {
this.a = a;
this.b = b;
this.c = c;
}
// toString method
@Override
public String toString() {
return "a = "+a+", b = "+b+", c = "+c;
}
// equals method return true if all the points of both the triangle are same
public boolean equals(Triangle other) {
if (other != null) {
return this.a.equals(other.a) && this.b.equals(other.b) && this.c.equals(other.c);
}
return false;
}
// return perimeter using distance method of MyPoing class
public double perimeterLength() {
double perimeter = a.distance(b) + b.distance(c) + a.distance(c);
return perimeter;
}
// return a new Triangle instance by reading the points from read method of MyPoint class
public static Triangle read() {
Scanner sc = new Scanner(System.in); // creating Scanner instance
// reading three points
MyPoint a = MyPoint.read(sc);
MyPoint b = MyPoint.read(sc);
MyPoint c = MyPoint.read(sc);
// returning new triangle instance
return new Triangle(a, b, c);
}
// main method
public static void main(String[] args) {
// creating a Triangle instance using Trangle.read() method
Triangle triangle1 = Triangle.read();
// calling PerimeterLenght() to get perimeter
System.out.format("Perimeter : %.2f\n", triangle1.perimeterLength());
// printing the triangle
System.out.println(triangle1);
// creating a triangle my passing MyPoint instance
Triangle triangle2 = new Triangle(new MyPoint(5.5, 4.0), new MyPoint(34.6, -3.3), new MyPoint(0, 6));
// printing triangle2
System.out.println(triangle2);
// calling equals method to check both are equals
System.out.println(triangle1.equals(triangle2));
}
}
outputs..