Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Create a Geometric object list that has combination of Circle and Rectangle object. 2. User Collection object utility to sort that list based on

1. Create a Geometric object list that has combination of Circle and Rectangle object.

2. User Collection object utility to sort that list based on GeometricObjectComparator class attached.

3. Just submit your project6.java.

TestComparator.java GeometricObjectComparator.java Circle.java GeometricObject.java Rectangle.java

//TestCompatator.java import java.util.Comparator;

public class TestComparator { public static void main(String[] args) { GeometricObject g1 = new Rectangle(5, 5); GeometricObject g2 = new Circle(5);

GeometricObject g = max(g1, g2, new GeometricObjectComparator()); System.out.println("The area of the larger object is " + g.getArea()); } public static GeometricObject max(GeometricObject g1, GeometricObject g2 , Comparator c) { if (c.compare(g1, g2) > 0) return g1; else return g2; } }

------------------------------------------------------------------------------------------------------------------ //GeometricObjectComparator import java.util.Comparator;

public class GeometricObjectComparator implements Comparator, java.io.Serializable { public int compare(GeometricObject o1, GeometricObject o2) { double area1 = o1.getArea(); double area2 = o2.getArea();

if (area1 < area2) return -1; else if (area1 == area2) return 0; else return 1; } }

------------------------------------------------------------------------------------------------------------------- // Circle extends GeometricObject.java package ProrityQueueComparator;

public class Circle extends GeometricObject{ private double radius;

public Circle() { }

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; } /* Print the circle info */ public void printCircle() { System.out.println("The circle is created " + getDateCreated() + " and the radius is " + radius); } @Override public String toString() { return super.toString() + " Circle Area: " + getArea() + " "; } }

-------------------------------------------------------------------------------------------------------------------------- //GeomtericObject package ProrityQueueComparator;

public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;

/** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); }

/** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); 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 dateCreated */ public java.util.Date getDateCreated() { return dateCreated; }

@Override public String toString() { return "created on " + dateCreated + " color: " + color + " and filled: " + filled; } //@Override //public abstract int compareTo(GeometricObject g); /** Abstract method getArea */ public abstract double getArea();

/** Abstract method getPerimeter */ public abstract double getPerimeter(); }

------------------------------------------------------------------------------------------------------------------------- //Rectangle extends GeomtericObject package ProrityQueueComparator;

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 super.toString() + " Rectangle Area: " + getArea() + " "; } /* @Override public void howToColor() { System.out.println("Color all four sides"); } */ }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial Databases 2nd Symposium Ssd 91 Zurich Switzerland August 1991 Proceedings Lncs 525

Authors: Oliver Gunther ,Hans-Jorg Schek

1st Edition

3540544143, 978-3540544142

More Books

Students also viewed these Databases questions

Question

Write a c + + code that rotates a 3 x 3 array counterclockwise

Answered: 1 week ago