Question

Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

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\n");
       printArray(list);
      
       // calling sort methos
       GeometricObjectComparator comparator = new GeometricObjectComparator();
       selectionSort(list, comparator);
       System.out.println();
       System.out.println("After Sorting\n");
       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),

*/

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • write a completed program (included the main) for the Q: add an equals method to each...

    write a completed program (included the main) for the Q: add an equals method to each of the Rectangle circle and triangle classes introduced in this chapter. two shapes are considered equal if their fields have equivalent values. based on public class Circle implements Shape f private double radius; // Constructs a new circle with the given radius. public Circle (double radius) f this.radius - radius; // Returns the area of this circle. public double getArea) return Math.PI *radius *...

  • m The interface Measurable is defined as the following: /** An interface for methods that return...

    m The interface Measurable is defined as the following: /** An interface for methods that return the perimeter and area of an object. */ public interface Measurable { public double getPerimeter(); public double getArea(); } The class Rectangle implements the interface. The incomplete program is written as follows: 1 public class Rectangle 2 { private double width; private double height; public Rectangle(double w, double h) { width = w; height h; } { 3 4 5 6 7 8 9...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • Define an interface named Shape with a single method named area that calculates the area of...

    Define an interface named Shape with a single method named area that calculates the area of the geometric shape:        public double area(); Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, an accessor and a mutator method for the radius, and an implementation of the area method. Define another class named Rectangle that implements Shape. The Rectangle class should have instance variables...

  • Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public...

    Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public class InterfaceRunner { public static void main(String[] args) { ArrayList<GeometricSolid> shapes = new ArrayList<>(); shapes.add(new Sphere(10)); shapes.add(new Sphere(1)); shapes.add(new Cylinder(1, 5)); shapes.add(new Cylinder(10, 20)); shapes.add(new RightCircularCone(1, 5)); shapes.add(new RightCircularCone(10, 20)); /* * Notice that the array list holds different kinds of objects * but each one is a GeometricSolid because it implements * the interface. * */ for (GeometricSolid shape : shapes) { System.out.printf("%.2f%n",shape.volume());...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • For the code below write a public static main() method in class Student that: - creates...

    For the code below write a public static main() method in class Student that: - creates an ArrayList<Student> object called students - adds 4 new Student objects to the students list, with some made up names and dates - sort the students list by name and display the sorted collection to System.out. use function getCompByName() - sort the students list by enrollment date and display the sorted collection to System.out. use function getCompByDate() import java.util.Comparator;    import java.util.Date;    public...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT