-1

I am building on top of my previous program which used sorting algorithms to sort through integers in an array.

I'm now tasked with creating an ArrayList of Rectangles, creating a Rectangle class which extends the Comparable interface, and implementing generics in my sorting methods.

In the end, the program needs to calculate and sort through the given area values of 10 different Rectangles.

The problem I am having is, I'm not sure how to modify my sorting algorithms to implement generics, and use the compareTo/getArea methods I have created in the Rectangle class.

I am fairly new to all of these concepts so my understanding is limited.

Here is a snippet of how I have created my ArrayList of 10 Rectangles (in descending values):

public static void main(String[] args) {

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

    for (int i=10;i>=1;i--) {
        Rectangle rectangle = new Rectangle((i+5)*2,(i+7)*6);
        list.add(rectangle);
    }
    ...
}

This is my Rectangle class which extends the Comparable interface and has the overridden compareTo method:

class Rectangle implements Comparable<Rectangle> {

    double width=1;
    double height=1;

    public Rectangle() { }

    public Rectangle(double recWidth, double recHeight) {
        this.width = recWidth;
        this.height = recHeight;
    }

    public double getWidth() {
        return width;
    }

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

    public double getHeight() {
        return height;
    }

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

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

    @Override
    public int compareTo(Rectangle r) {

        if (getArea() > r.getArea()) {
            return 1;
        }
        else if (getArea()<r.getArea()) {
            return -1;
        }
        else {
            return 0;
        }   
    }
    ...
}

And this is my current bubble sort method which is no longer functioning.

public static <R extends Comparable <Rectangle>> void bubbleSort(ArrayList<Rectangle> list) {

    for (int k = 1; k < list.size();k++) {
        for (int i = 0; i <list.size()-k; i++) {   

             R elem = list.getArea();
             R elem2 = list.getArea(i+1);

             if (elem.compareTo(elem2) >= 1) {
                 R temp = list.get(i);    
                 list.set(i, list.get(i+1)); 
                 list.set(i+1, temp); 
             }
         }
         printList(list);

    }
}

Error message:

 Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The method getArea() is undefined for the type ArrayList<Rectangle>
    The method getArea(int) is undefined for the type ArrayList<Rectangle>
    The method compareTo(Rectangle) in the type Comparable<Rectangle> is not applicable for the arguments (R)
    Type mismatch: cannot convert from Rectangle to R
    The method set(int, Rectangle) in the type ArrayList<Rectangle> is not applicable for the arguments (int, R)

Again, I'd like my bubble sort method to obtain the area of each Rectangle being compared and sort them accordingly.

Any guidance or assistance is appreciated. Thanks!

  • There are lot of issues in your code. For e.g `R elem = list.getArea()`. You need to take out objects inside list and then call getArea (list.get(x).getArea()).Next issue, Generics is wrongly perceived .I would suggest to go through the basics of java. – ialam Nov 02 '19 at 05:50

1 Answers1

0

You are trying to call the method getArea() on an ArrayList<> object. However, the class ArrayList<> does not have such a method. It is not possible to call

R elem = list.getArea();

when list is of type ArrayList<>.

Actually, you don't need to call getArea() in your bubble sort method at all. What the bubble sort method only care about is that the list contains objects which are implementing the Comparable interface. Your method declaration should be changed to:

public static <R extends Comparable <R>> void bubbleSort(ArrayList<R> list)

This way you are saying: "The list must contain objects of the generic type R, where the class R implements the Comparable<R> interface". When you replace R in your mind by your Rectangle class you will see that your class Rectangle implements Comparable<Rectangle> fits that requirement.

Inside your bubble sort method you don't use the getArea() method anymore (you can't), but only methods from the ArrayList<> and the method compareTo() provided by the Comparable<> interface. Your code should look like this:

public static <R extends Comparable <R>> void bubbleSort(ArrayList<R> list) {

    for (int k = 1; k < list.size();k++) {
        for (int i = 0; i <list.size()-k; i++) {   

             R elem = list.get(i);
             R elem2 = list.get(i+1);

             if (elem.compareTo(elem2) > 0) {
                 list.set(i, elem2); 
                 list.set(i+1, elem); 
             }
         }
         printList(list);

    }
}

You are using get(i) and get(i+1) to get the pair you want to check. Then inside the if() statement you simply swap the positions.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • Hi, thank you for the feedback. I've adjusted my bubble sort method accordingly and it seems to run as needed. However, I found that my program isn't using the compareTo method defined in the Rectangle class. It simply sorts by the Rectangle object; not the area of each rectangle. (E.g it prints 'Rectangle@' followed by a random alphanumeric characters for each Rectangle in the list, and sorts them.) Is there something I'm missing? – suppressive Nov 02 '19 at 21:51
  • @suppressive Your `compareTo()` method is already using the `getArea()` method. Why do you think it does not call the `compareTo()` method? What happens when you write a `System.out.println()` statement inside the `compareTo()` method, do you get any output? For the "random alphanumeric characters" see https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – Progman Nov 02 '19 at 22:30