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

Repair the following class code so that it runs without crashing and produces so

ID: 3556924 • Letter: R

Question

Repair the following class code so that it runs without crashing and

      produces sorted output using the sort method of the Collections helper class.

/** Elipse Class, COSC 237, Mock Final, Question 1   */

import java.util.*;

public class Elipse                     {

int a, b;

public Elipse(int minor1, int minor2) {

    a = minor1; b = minor2;

}

public double area() {

    return Math.PI * a * b;

}

public String toString() {

    return ("E(" + a + ", " + b + ")");

}

public static void main(String[] args) {

    ArrayList<Elipse> list = new ArrayList<Elipse>();

    list.add(new Elipse(2, 3));

    list.add(new Elipse(1, 3));

    list.add(new Elipse(4, 5));

    list.add(new Elipse(9, 3));

    list.add(new Elipse(1, 1));

   

    Collections.sort(list);

    System.out.println(list);

    // Expected output:

    // [E(9, 3), E(4, 5), E(2, 3), E(1, 3), E(1, 1)]

}

}

Explanation / Answer

import java.util.*;

public class Elipse
{
int a, b;
public Elipse(int minor1, int minor2) {
    a = minor1; b = minor2;
}
public double area() {
    return Math.PI * a * b;
}

public String toString() {

    return ("E(" + a + ", " + b + ")");

}

public static void main(String[] args) {

    ArrayList<Elipse> list = new ArrayList<Elipse>();

    list.add(new Elipse(2, 3));

    list.add(new Elipse(1, 3));

    list.add(new Elipse(4, 5));

    list.add(new Elipse(9, 3));

    list.add(new Elipse(1, 1));

    Collections.sort(list,new ElipseComperator());

    System.out.println(list);

    // Expected output:

    // [E(9, 3), E(4, 5), E(2, 3), E(1, 3), E(1, 1)]

}

}
class ElipseComperator implements Comparator<Elipse>
{
public int compare(Elipse obj1,Elipse obj2)
{
    if(obj1.area()>obj2.area())
      return -11;
    else if(obj1.area()==obj2.area())
      return 0;
    else
      return 1;
}
}