Question: public Circle(double radius) { this.radius = radius; } /** Return radius */ public double getRadius() { return radius; } /** Set a new radius */
public Circle(double radius) { this.radius = radius; }
/** Return radius */ public double getRadius() { return radius; }
/** Set a new radius */ public void setRadius(double radius) { this.radius = radius; }
@Override /** Return area */ public double getArea() { return radius * radius * Math.PI; }
/** Return diameter */ public double getDiameter() { return 2 * radius; }
@Override /** Return perimeter */ public double getPerimeter() { return 2 * radius * Math.PI; }
@Override public String toString() { return String.format("Circle [id-%d, radius-%.2f, area-%.2f, perimeter-%.2f]", this.getId(), radius, getArea(), getPerimeter()); } }
import java.util.Comparator;
public class GeoComparator implements Comparator
}
public abstract class GeometricObject { private static int idGen = 1; private String color = "white"; private boolean filled; private int id;
/** Construct a default geometric object */ protected GeometricObject() { id = idGen; idGen++; }
/** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { id = idGen; idGen++; this.color = color; this.filled = filled; }
/** Return color */ public String getColor() { return color; }
/** Set a new color */ public void setColor(String color) { this.color = color; }
/** Return filled. Since filled is boolean, * the get method is named isFilled */ public boolean isFilled() { return filled; }
/** Set a new filled */ public void setFilled(boolean filled) { this.filled = filled; }
/** Get id */ public int getId() { return id; }
/** Return a string representation of this object */ public String toString() { return "[id: " + id + " color: " + color + " and filled: " + filled + "]"; }
/** Abstract method getArea */ public abstract double getArea();
/** Abstract method getPerimeter */ public abstract double getPerimeter(); }
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator;
public class Main { public static void main(String[] args) { ArrayList
geoObjects.add(g1); geoObjects.add(g2); geoObjects.add(g3); geoObjects.add(g4); geoObjects.add(g5); geoObjects.add(g6); for (GeometricObject g: geoObjects) { if (g instanceof Circle) { System.out.println((Circle) g); } else { System.out.println((Rectangle) g); } }
//TODO1: sort the geo objects by increasing order of their perimeters, then by their areas, then by created dates //Note that you cannot modify GeometricObject.java, Circle.java, or Rectangle.java //TODO1 end System.out.println(" Increasing order of perimeter and then area and then dates: "); for (GeometricObject g: geoObjects) { if (g instanceof Circle) { System.out.println((Circle) g); } else { System.out.println((Rectangle) g); } } //TODO2: sort the geo objects by decreasing order of their perimeters (please DO NOT create a new comparactor class for this purpose.) //TODO2 end System.out.println(" Decreasing order of perimeter and then area and then dates: "); for (GeometricObject g: geoObjects) { if (g instanceof Circle) { System.out.println((Circle) g); } else { System.out.println((Rectangle) g); } } } }
public class Rectangle extends GeometricObject { private double width; private double height;
public Rectangle() { }
public Rectangle(double width, double height) { this.width = width; this.height = height; }
/** Return width */ public double getWidth() { return width; }
/** Set a new width */ public void setWidth(double width) { this.width = width; }
/** Return height */ public double getHeight() { return height; }
/** Set a new height */ public void setHeight(double height) { this.height = height; }
@Override /** Return area */ public double getArea() { return width * height; }
@Override /** Return perimeter */ public double getPerimeter() { return 2 * (width + height); } @Override public String toString() { return String.format("Rectangle [id-%d, width-%.2f, height-%.2f, area-%.2f, perimeter-%.2f]", this.getId(), width, height, getArea(), getPerimeter()); } }
in java code
GeometricObject.java, Circle.java, and Rectangle.java are provided for you. Please do not modify these three files.
You will complete the following two files:
File 1: GeoComparator.java
- Create a Comparator class for comparing GeometricObject objects by their perimeters.
File 2: Main.java
- Create an ArrayList of GeometricObject. (ALREADY DONE)
- Use Collections.sort to sort the list in the increasing order of the perimeters.
- Use Collections.sort to sort the list in the decreasing order of the perimeters. Use the reverseOrder() method on the comparator object in (2) to get a comparator that compares the GeometricObject objects in the reverse order of perimeters. Please do not define a new Comparator class.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
