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

Need some help with this java problem Implement the following generic Java metho

ID: 3797263 • Letter: N

Question

Need some help with this java problem

Implement the following generic Java method using an O(n2) sort and a comparator: public static void aSort(E[] list, Comparator comparator). Write test program that creates a list of at least 5 elements of the class type created in problem 3 above, calls the above method to sort the list, then outputs the sorted list via calls to toString.

Problem 3 Question which I have also been having trouble implementing

Create a Java class named “Circle” that implements java.io.Serializable interface (for more information see: https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html) and models a circle based on radius. Radius cannot be less than zero. Implement the getter and setter method for radius. Also include an overriding of toString in the circle class. Create the class' comparator which compares two objects of type Circle.

Explanation / Answer

############Circle.java############
package chegg;
import java.io.Serializable;
/*Create a Java class named “Circle” that implements java.io.Serializable interface*/
public class Circle implements Serializable {
   /**
   * Just use for serialization
   */
   private static final long serialVersionUID = -4221949048077749292L;
   private float redius;
   public Circle() {
   }
   public Circle(float radius) throws Exception {
       if (radius < 0) {
           throw new Exception("Radius cannot be less than zero");
       }
       this.redius = radius;
   }
   public float getRedius() {
       return redius;
   }
   public void setRedius(float radius) throws Exception {
       if (radius < 0) {
           throw new Exception("Radius cannot be less than zero");
       }
       this.redius = radius;
   }
   // include an overriding of toString in the circle class
   @Override
   public String toString() {
       return "Circle [radius=" + redius + "]";
   }
}

################CircleComparator##########
package chegg;
import java.util.Comparator;
// Create the class' comparator which compares two objects of type Circle
public class CircleComprator implements Comparator<Object> {
   @Override
   public int compare(Object o1, Object o2) {
       if (o1 == null || o2 == null) {
           return -1;
       }
       Circle c1 = (Circle) o1;
       Circle c2 = (Circle) o2;
       if (c1.getRedius() > c2.getRedius()) {
           return 1;
       } else {
           return -1;
       }
   }
}
###########circleComparatorTestClass.java#######
package chegg;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CircleComparatorTestClass {
   // Write test program
   public static void main(String[] args) {
       try {
           // creates a list of at least 5 elements of the class type created
           // in problem 3
           Circle[] circleList = new Circle[5];
           Circle c1;
           c1 = new Circle(4);
           Circle c2 = new Circle(1);
           Circle c3 = new Circle(6);
           Circle c4 = new Circle(3);
           Circle c5 = new Circle(2);
           circleList[0] = c1;
           circleList[1] = c2;
           circleList[2] = c3;
           circleList[3] = c4;
           circleList[4] = c5;
           // calls the aSort method to sort the list,
           CircleComparatorTestClass.aSort(circleList, new CircleComprator());
       } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }
   public static <T> void aSort(T[] list,
           @SuppressWarnings("rawtypes") Comparator circleComprator) {
       List<T> ll = Arrays.asList(list);
       System.out.println(" Unsorted List : " + ll);
       Collections.sort(ll, new CircleComprator());
       // outputs the sorted list via calls to toString.
       System.out.println(" Sorted List : " + ll);
   }
}

####Output############
Unsorted List : [Circle [radius=4.0], Circle [radius=1.0], Circle [radius=6.0], Circle [radius=3.0], Circle [radius=2.0]]
Sorted List : [Circle [radius=1.0], Circle [radius=2.0], Circle [radius=3.0], Circle [radius=4.0], Circle [radius=6.0]]


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote