Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Thanks in advance’ Description In this assignment, you will create: an abstract

ID: 3749741 • Letter: T

Question


Thanks in advance’
Description In this assignment, you will create: an abstract class called Shape that implements Comparable . a class Rectangle that extends Shape a class Square that extends Rectangle a class ShapeList that extends ArrayList-Shape a class Main for testing your code (10 pts) Define an abstract class named Shape . Shape must implement Comparable Shape must include a method with signature: int compare To(Shape otherShape) Shape must have an abstract method with signature: double compute Area0 . The compare To method must compare the area of itself with the area of otherShape . Shape must have an abstract method with signature: String toString) (10 pts) Define a class named Rectangle . Rectangle must extend Shape . Rectangle must have: o a constructor with signature: Rectangle(double width, double height) o a computeArea method that returns the product of width and height o an equals method that checks if two Rectangles have the same width and height o a toString method that returns the width and height of the Rectangle as a String. For example, if width 10 and height 3, then it would return "(10, 3)".

Explanation / Answer

Just run the main method and see the magic !! import java.util.ArrayList; // Implementing the Shape class as per given in question abstract class Shape implements Comparable { double width; double height; abstract double computeArea(); public abstract String toString(); @Override public int compareTo(Shape shape) { return Double.compare(this.computeArea(), shape.computeArea()); } } // Implementing the Rectangle class as per given in question class Rectangle extends Shape { Rectangle(double width, double height) { this.width = width; this.height = height; } public boolean equals(Rectangle r1, Rectangle r2) { return r1.height == r2.height && r1.width == r2.width; } @Override double computeArea() { return this.width * this.height; } @Override public String toString() { return "(" + this.width + ", " + this.height + ")"; } } // Implementing the Square class as per given in question class Square extends Rectangle { Square(double length) { super(length, length); } } class ShapeList extends ArrayList { public ShapeList largestShapes() { // First finding the largest area so that we can find shapes having this much area double largestArea = 0; for (Shape shape : this) { // Here current class extended arraylist so current class gets the properties of array list. // "this" in the above line means elements in current class means elements in arraylist // don't confuse with "this" :) largestArea = Math.max(largestArea, shape.computeArea()); } ShapeList largest = new ShapeList(); for (Shape shape : this) { if (shape.computeArea() == largestArea) { largest.add(shape); } } return largest; } public boolean hasDistinctAreas() { for (Shape s1 : this) { for (Shape s2 : this) { if (s1.computeArea() == s2.computeArea()) { // If two areas are equal then these two rectangles must be equal if (!s1.equals(s2)) return false; } } } // Default returns true return true; } } public class Main { public static void main(String[] args) { ShapeList shapes = new ShapeList(); for (int i = 1; i < 8; i++) { shapes.add(new Rectangle(i, i * 2)); } System.out.println(shapes.largestShapes()); System.out.println(shapes.hasDistinctAreas()); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote