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

20.21 Please code using Java and please show an example that it will run sucessf

ID: 3676479 • Letter: 2

Question

20.21

Please code using Java and please show an example that it will run sucessfully in an JAVA IDE. Code the following,

(Use Comparator) Write the following generic method using selection sort and a comparator.

public static <E> void selectionSort(E[ ] list, Comparator<? super E> comparator)

Write a test program that creates an array of 10 GeometricObjects and invokes this method using the GeometricObjectComparator introduced inListing 20.4 to sort the elements. Display the sorted elements. Use the following statement to create the array.

GeometricObject[] list = {new Circle(5), new Rectangle(4, 5),

new Circle(5.5), new Rectangle(2.4, 5), new Circle(0.5),

new Rectangle(4, 65), new Circle(4.5), new Rectangle(4.4, 1),

new Circle(6.5), new Rectangle(4, 5)};

LISTING 20.4 GeometricObjectComparator.java

import java.util.Comparator;

public class GeometricObjectComparato

implements Comparator<GeometricObject>, 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;

}

}

Explanation / Answer

SolutionDemo.java


import java.util.Comparator;
abstract class GeometricObject {

String color = "red";
boolean filled;
java.util.Date CreatedDate;

/** Construct a default geometric object */
GeometricObject() {//constructor Geometricbject
CreatedDate = new java.util.Date();
}

/** Construct a geometric object with color and filled value */
GeometricObject(String color, boolean filled) {//constructor Geometricbject with two args
CreatedDate = 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 CreatedDate;
}

@Override
public String toString() {
return "created on " + CreatedDate + " color: " + color
+ " and filled: " + filled;
}

/** Abstract method getArea */
public abstract double getArea();

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


class Rectangle extends GeometricObject {//Rectangle inherits Geometricobject class

double width1;
double height1;

  
public Rectangle(double width1, double height1) {//Recangle constructor with two args
this.width1 = width1;
this.height1 = height1;
}

public Rectangle(double width1, double height1, String color, boolean filled) {
this.width1 = width1;
this.height1 = height1;
setColor(color);
setFilled(filled);
}

/** Return width1 */
public double getwidth1() {
return width1;
}

/** Set a new width1 */
public void setwidth1(double width1) {
this.width1 = width1;
}

/** Return height1 */
public double getheight1() {
return height1;
}

/** Set a new height1 */
public void setheight1(double height1) {
this.height1 = height1;
}

@Override
/** Return area */
public double getArea() {
return width1 * height1;
}

@Override
/** Return perimeter */
public double getPerimeter() {
return 2 * (width1 * height1);
}

@Override
/** Override the toString method in the Object class */
public String toString() {
return super.toString() + " Rectangle, Created: "
+ getDateCreated() + ", width1: " + width1
+ ", height1: " + height1;
}
}

class GeometricObjectComparator implements Comparator<GeometricObject>, 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;
}
}
}

class Circle extends GeometricObject {//circle class inherits geometricobject

private double radius;

  
public Circle(double radius) {//constructor with args
this.radius = radius;
}

public Circle(double radius,String color, boolean filled) {//constructor with args
this.radius = radius;
setColor(color);
setFilled(filled);
}

/** 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
/** Override the toString method in the Object class */
public String toString() {
return super.toString() + ", Circle, Created: "
+ getDateCreated() + ", Radius: " + radius;
}
}

public class SolutionDemo {

public static void main(String[] args) {
// Create an array of 10 GeometricObjects
GeometricObject[] list = {new Circle(5), new Rectangle(4, 5),
new Circle(5.5), new Rectangle(2.4, 5), new Circle(0.5),
new Rectangle(4, 65), new Circle(4.5), new Rectangle(4.4, 1),
new Circle(6.5), new Rectangle(4, 5)};

// Invoke selection sort using GeometricObjectComparator
selectionSort(list, new GeometricObjectComparator());

// Display the sorted elements
System.out.print("Sorted elements: ");
for (GeometricObject e : list) {
System.out.printf("%.2f ", e.getArea());
}
System.out.println();
}

/** Generic selection sort method */
public static <E> void selectionSort(E[] list,
Comparator<? super E> comparator) {
for (int i = 0; i < list.length - 1; i++) {
// Find the minimum in the list[i..list.length-1]
E currentMin = list[i];
int currentMinIndex = i;

for (int j = i + 1; j < list.length; j++) {
if (comparator.compare(currentMin, list[j]) > 0) {
currentMin = list[j];
currentMinIndex = j;
}
}

// Swap list[i] with list[currentMinIndex] if necessary
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
}

output:

Sorted elements: 0.79 4.40 12.00 20.00 20.00 63.62 78.54 95.03 132.73 260.00