Answer to Question #237787 in Java | JSP | JSF for Zia

Question #237787

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 GeomtricObjects and invokes this method using GeometricObjectComparator introduced in Listing 20.5 to sort the elements. Display the sorted elements. Use the following statement to create the array: GeometricObjectfi list1 = {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(5.4), new 

rectangle(6.6,1), 

new Circle(5.6), new rectangle(5, 4); Also in the same program, write the code that stores six strings by their last character. Use the follwing statement to create the array: String 0 list2 = {"red", "blue", "green", "orange", "yellow", "pink") 



1
Expert's answer
2021-09-15T20:43:19-0400
public abstract class GeometricObject {
    public abstract double getArea();
}


public class Circle extends GeometricObject {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
}


public class Rectangle extends GeometricObject {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double getArea() {
        return width * height;
    }
}


import java.util.Comparator;

public class GeometricObjectComparator implements Comparator<GeometricObject> {

    @Override
    public int compare(GeometricObject o1, GeometricObject o2) {
        return Double.compare(o1.getArea(),o2.getArea());
    }
}


import java.util.Comparator;

public class Main {
    public static <E> void selectionSort(E[] list, Comparator<? super E> comparator) {
        E tmp;
        int min;
        for (int i = 0; i < list.length; i++) {
            min = i;
            for (int j = i + 1; j < list.length; j++) {
                if (comparator.compare(list[min], list[j]) > 0) {
                    min = j;
                }
            }
            tmp = list[i];
            list[i] = list[min];
            list[min] = tmp;
        }

    }

    public static void main(String[] args) {
        GeometricObject[] list1 = {
                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(5.4), new Rectangle(6.6, 1),
                new Circle(5.6), new Rectangle(5, 4)};
        selectionSort(list1, new GeometricObjectComparator());
        for (GeometricObject geometricObject : list1) {
            System.out.println(geometricObject.getArea());
        }
        System.out.println();
        String[] list2 = {"red", "blue", "green", "orange", "yellow", "pink"};
        selectionSort(list2, Comparator.comparingInt(o -> o.charAt(o.length() - 1)));
        for (String color:list2) {
            System.out.println(color);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS