Question: Define a class 'RightTriangle' in 'RightTriangle.java'. The class need a contructor that takes two sides next to the right angle as double type. Adds methods
Define a class 'RightTriangle' in 'RightTriangle.java'. The class need a contructor that takes two sides next to the right angle as double type. Adds methods equals(), compareTo(), toString() so that it works as shown in 'MyArrayObjDemo.java'. Ignore the difference by rotation and consider a RightTriangle(3,4) and a RightTriangle(4,3) are equal Use their perimeters for comparison.
MyArrayObjDemo.java code:
package apps; public class MyArrayObjDemo { public static void main(String[] args) { MyArray rectangles = new MyArray(); Rectangle r1 = new Rectangle(2, 5); Rectangle r2 = new Rectangle(2, 4.9); System.out.println(r1.getArea() + ", " + r1.getPerimeter()); // 10.0, 14.0 rectangles.add(r1); rectangles.add(r2); rectangles.add(new Rectangle(1,9)); rectangles.printArray(); // printArray(3,5): Rect(2.0,5.0) Rect(2.0,4.9) Rect(1.0,9.0) System.out.println(rectangles.search(r2)); // 1 System.out.println(rectangles.search(new Rectangle(5.0,2.0))); // 0 rectangles.sort(); rectangles.printArray(); // printArray(3,5): Rect(1.0,9.0) Rect(2.0,4.9) Rect(2.0,5.0) MyArray rTriagles = new MyArray(); RightTriangle rt1 = new RightTriangle(3, 4); RightTriangle rt2 = new RightTriangle(1, 1); System.out.println(rt1.getArea() + ", " + rt1.getPerimeter()); // 6.0, 12.0 rTriagles.add(rt1); rTriagles.add(rt2); rTriagles.add(new RightTriangle(3.1, 3.9)); rTriagles.printArray(); // printArray(3,5): RT(3.0,4.0) RT(1.0,1.0) RT(3.1,3.9) System.out.println(rTriagles.search(rt2)); // 1 System.out.println(rTriagles.search(new RightTriangle(4,3))); // 0 rTriagles.sort(); rTriagles.printArray(); // printArray(3,5): RT(1.0,1.0) RT(3.1,3.9) RT(3.0,4.0) } } My code for Rectangle.java:
package apps; public class Rectangle implements Comparable{ double height; double width; public Rectangle(double height, double width) { this.height = height; this.width = width; } double getArea() { return width * height; } public double getPerimeter() { return 2 * (height + width); } public int compareTo(Rectangle rectangle) { return Double.compare(getArea(), rectangle.getArea()); } public String toString() { return "Rect(" + height + "," + width + ")"; } public boolean equals(Object obj) { if (this == obj) return true; Rectangle rectangle = (Rectangle) obj; return compareTo(rectangle) == 0; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
