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

Receiveing this error message when running the Experts code below please fix ---

ID: 3676861 • Letter: R

Question

Receiveing this error message when running the Experts code below please fix

----jGRASP exec: javac -g GeometricObject.java

GeometricObject.java:92: error: class, interface, or enum expected
import java.util.Comparator;
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

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;

}

}

View comments (1)

Expert 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),

*/

Explanation / Answer

TestComparator.java

public class TestComparator {
    public static void main(String[] args) {
        GeometricObjectComparator comp = new GeometricObjectComparator();

        GeometricObject rect1 = new Rectangle(2.3, 4.5);
        GeometricObject rect2 = new Rectangle(4.7, 5.9);
        GeometricObject circ1 = new Circle(3.6);
        GeometricObject circ2 = new Circle(5.6);

        System.out.println("The largest rectangle is " + comp.max(rect1, rect2));
        System.out.println("The largest circle is " + comp.max(circ1, circ2));

    }
}

GeometricObjectComparator.java

import java.util.Comparator;

public class GeometricObjectComparator
        implements Comparator<GeometricObject>, java.io.Serializable {
    static GeometricObjectComparator geo = new GeometricObjectComparator();

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

    public static GeometricObject max(GeometricObject o1, GeometricObject o2) {
        int returnValue = geo.compare(o1,o2);
        if (returnValue == 1)
            return o1;
        else if (returnValue == -1)
            return o2;
        else
            return null;
    }
}


Circle.java

// Circle.java: The circle class that extends GeometricObject
public class Circle extends GeometricObject implements Comparable{
    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;
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }

    public boolean equals(Circle circ1, Circle circ2) {
        if (circ1.getArea() == circ2.getArea())
            return true;
        else
            return false;
    }
}


GeometricObject.java

// GeometricObject.java: The abstract GeometricObject class
public abstract class GeometricObject {
    private String color = "white";
    private boolean filled;

    /**Default construct*/
    protected GeometricObject() {
    }

    /**Construct a geometric object*/
    protected GeometricObject(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
    }

    /**Getter method for color*/
    public String getColor() {
        return color;
    }

    /**Setter method for color*/
    public void setColor(String color) {
        this.color = color;
    }

    /**Getter method for filled. Since filled is boolean,
     so, the get method name is isFilled*/
    public boolean isFilled() {
        return filled;
    }

    /**Setter method for filled*/
    public void setFilled(boolean filled) {
        this.filled = filled;
    }

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

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


Rectangle.java


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


sample output

The largest rectangle is Rectangle@2a139a55                                                                                                                 
The largest circle is [Circle] radius = 5.6