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 Rectangle. Here’s the UML
diagram:
Rectangle
- a: MyPoint
- b: MyPoint
- c: MyPoint
- d: MyPoint
+ Rectangle(a: MyPoint, b: MyPoint, c: MyPoint, d: MyPoint)
+ equals(other: Rectangle): boolean
+ toString(): String
+ perimeterLength(): double
+ read(sc: Scanner): Rectangle
Notes:
● The data fields represent the four corners of a rectangle.
● The equals method should determine if this Rectangle 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),
d = (0.6, 6.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 Rectangle.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//Rectangle.java
import java.util.Scanner;
public class Rectangle {
// attributes
private MyPoint a;
private MyPoint b;
private MyPoint c;
private MyPoint d;
// constructor taking values for all 4 corner points
public Rectangle(MyPoint a, MyPoint b, MyPoint c, MyPoint d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
// returns true if this rectangle equals to other
public boolean equals(Rectangle other) {
return a.equals(other.a) && b.equals(other.b) && c.equals(other.c)
&& d.equals(other.d);
}
// returns a String containing rectangle info
public String toString() {
return "a = " + a + ", b = " + b + ", c = " + c + ", d = " + d;
}
// returns perimeter of rectangle
public double perimeterLength() {
// finding length and width from corner points, returning perimeter
// using formula 2(l+w)
double length = a.distance(b);
double width = a.distance(c);
return 2 * (length + width);
}
// reads 4 points, then creates and returns a Rectangle.
public static Rectangle read(Scanner sc) {
MyPoint p1 = MyPoint.read(sc);
MyPoint p2 = MyPoint.read(sc);
MyPoint p3 = MyPoint.read(sc);
MyPoint p4 = MyPoint.read(sc);
return new Rectangle(p1, p2, p3, p4);
}
}