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 IDE. Please DO NOT use Toolkit. You can use a class

ID: 3676823 • Letter: 2

Question

20.21

Please code using Java IDE. Please DO NOT use Toolkit.

You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit?

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 void selectionSort(E[ ] list, Comparatorsuper 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, 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

// GeometricObject Interface
// putting all common methods and constants
public interface GeometricObject {
   double PI = 3.14;
   double getArea();
   double getPerimeter();
  
}

// Circle class that implements GeometricObject interface
class Circle implements GeometricObject{
   private double radius;
   public Circle(double radius) {
       this.radius = radius;
   }

  
   public double getRadius() {
       return radius;
   }


   public void setRadius(double radius) {
       this.radius = radius;
   }


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

   @Override
   public double getPerimeter() {
       return 2*PI*radius;
   }
   @Override
   public String toString() {
       return "Circle("+radius+")";
   }
}

//Rectangle class that implements GeometricObject interface
class Rectangle implements GeometricObject{

   private double height;
   private double width;
   // constructor
   public Rectangle(double height, double width) {
       this.height = height;
       this.width = width;
   }
  
   //getter and setters
   public double getHeight() {
       return height;
   }


   public void setHeight(double height) {
       this.height = height;
   }


   public double getWidth() {
       return width;
   }


   public void setWidth(double width) {
       this.width = width;
   }


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

   @Override
   public double getPerimeter() {
       return 2*(height+width);
   }
   @Override
   public String toString() {
       return "Rectangle("+height+", "+width+")";
   }
  
}

// comparator Class
import java.util.Comparator;

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

//Test class
class TestGeometric{

   // selection sort
   public static <E> void selectionSort(E[] list, Comparator<E> comparator)
   {
        for(int i=0; i<list.length -1; i++)
        {
            int iSmallest = i;

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

        }
   }

   // method to print
   public static <E> void printArray(E[] list)
   {

        for(int i=0; i<list.length; i++)
        {
            System.out.print(list[i] + ", ");
        }
   }
   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)};
       // printing before sorting
       System.out.println("Before sorting ");
       printArray(list);
      
       // calling sort methos
       GeometricObjectComparator comparator = new GeometricObjectComparator();
       selectionSort(list, comparator);
       System.out.println();
       System.out.println("After Sorting ");
       printArray(list);

   }
}

/*

Output:

Before sorting

Circle(5.0), Rectangle(4.0, 5.0), Circle(5.5), Rectangle(2.4, 5.0), Circle(0.5), Rectangle(4.0, 65.0), Circle(4.5), Rectangle(4.4, 1.0), Circle(6.5), Rectangle(4.0, 5.0),
After Sorting

Circle(0.5), Rectangle(4.4, 1.0), Rectangle(2.4, 5.0), Rectangle(4.0, 5.0), Rectangle(4.0, 5.0), Circle(4.5), Circle(5.0), Circle(5.5), Circle(6.5), Rectangle(4.0, 65.0),

*/