Tuesday 17 January 2017

Chapter 20 Exercise 21, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

20.21 (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 in Listing 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)};
public class Circle extends GeometricObject {
    private double radius;

    /**Default constructor*/
    public Circle() {
        this(1.0);
    }

    /**Construct circle with a specified radius*/
    public Circle(double radius) {
        this(radius, "white", false);
    }

    /**Construct a circle with specified radius, filled, and color*/
    public Circle(double radius, String color, boolean filled) {
        super(color, filled);
        this.radius = radius;
    }

    /**Return radius*/
    public double getRadius() {
        return radius;
    }

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

    /**Implement the getArea method defined in GeometricObject*/
    public double getArea() {
        return radius*radius*Math.PI;
    }

    /**Implement the getPerimeter method defined in GeometricObject*/
    public double getPerimeter() {
        return 2*radius*Math.PI;
    }

    /**Override the equals() method defined in the Object class*/
    public boolean equals(Circle circle) {
        return this.radius == circle.getRadius();
    }

    @Override
    public String toString() {
        return "[Circle] radius = " + radius;
    }
}

public abstract class GeometricObject implements Comparable<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;
    }

    /** Return a string representation of this object */
    @Override
    public String toString() {
        return "created on " + dateCreated + "\ncolor: " + color +
                " and filled: " + filled;
    }

    @Override
    public int compareTo(GeometricObject o) {
        if (getArea() > o.getArea())
            return 1;
        else if (getArea() < o.getArea())
            return -1;
        else
            return 0;
    }

    public static GeometricObject max(GeometricObject o1, GeometricObject o2) {
        return (o1.compareTo(o2) >= 0) ? o1 : o2;
    }

    public static double sumArea(GeometricObject[] a) {
        double sum = 0;
        for (GeometricObject o : a) {
            sum += o.getArea();
        }
        return sum;
    }
    /** Abstract method getArea */
    public abstract double getArea();

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

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;
    }

    /** Return area */
    public double getArea() {
        return width * height;
    }

    /** Return perimeter */
    public double getPerimeter() {
        return 2 * (width + height);
    }
}

import java.util.Arrays;
import java.util.Comparator;

public class Exercise_21 {

    public static void main(String[] args) {
        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)
        };


        System.out.println("Before sort");
        for (GeometricObject o : list) {
            System.out.println(o.getArea());
        }


        selectionSort(list, new Comparator<GeometricObject>() {
            @Override
            public int compare(GeometricObject o1, GeometricObject o2) {
                double a1 = o1.getArea();
                double a2 = o2.getArea();
                if (a1 > a2)
                    return 1;
                if (a1 == a2)
                    return 0;
                else
                    return -1;
            }
        });

        System.out.println("After sort");
        for (GeometricObject o : list) {
            System.out.println(o.getArea());
        }

    }

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

        for (int i = 0; i < array.length - 1; i++) {

            E currentMin = array[i];
            int minIndex = i;
            for (int k = i + 1; k < array.length; k++) {
                if (comparator.compare(array[k], currentMin) < 0) {
                    currentMin = array[k];
                    minIndex = k;
                }
            }

            if (minIndex != i) {
                array[minIndex] = array[i];
                array[i] = currentMin;
            }
        }
    }

}

No comments :

Post a Comment