简体   繁体   English

如何重写基于周长进行比较的矩形类中的compareTo方法?

[英]How to override the compareTo method in the rectangle class that compares based on perimeter?

I have a rectangle class here and I need to override the compareTo method so that it compares based on the rectangle's perimeters. 我这里有一个矩形类,我需要重写compareTo方法,以便它根据矩形的周长进行比较。 I know the current compareTo method I have is wrong but I don't know how to fix it. 我知道我当前使用的compareTo方法是错误的,但我不知道如何解决。

here is the code: 这是代码:

public class Rectangle implements Comparable<Rectangle> {
    private double length;
    private double width;
        private double perimeter;


    public Rectangle(double l, double w){
        this.length = l;
        this.width = w;
    } 

    public  double getLength() {
        return length;
    }

    public  double getWidth(){
        return width;
    }

        public void setLength(double l){
            length= l;
        }
        public void setWidth(double w){
            width = w;
        }

    public double getPerimeter(){
        perimeter = 2*(width + length);
                return perimeter;
    }


        @Override
        public int compareTo(Rectangle other){
                String t=Double.toString(perimeter);
                String o=Double.toString(other.perimeter);
        int comp = t.compareTo(o);

               return comp;
        }

        @Override
        public String toString(){
            return "Rectangle: "+ width +" by "+ length ;
        }




}

Try this 尝试这个

 public int compareTo(Rectangle otherRectangle) {
    return Double.compare(this.getPerimeter(), otherRectangle.getPerimeter());
}

Try this: You need to use getPerimeter because your calculation logic lies there: 尝试以下操作:您需要使用getPerimeter,因为您的计算逻辑就在这里:

@Override
    public int compareTo(Rectangle other){
          return Double.compare(getPerimeter(), other.getPerimeter());
    }

All the other answers are correct. 所有其他答案都是正确的。 This answer just adds an array of rectangles and sorts them based on their perimeters using java.util.Array.sort to demonstrate that the comparator is working. 这个答案只是添加了一个矩形数组,并使用java.util.Array.sort根据其周长对它们进行了排序,以证明比较器正在工作。 Also, the calculation of the perimeter is moved into the constructor, instead of the getPerimeter method. 同样,周长的计算也将移到构造函数中,而不是getPerimeter方法。

import java.util.Arrays;

public class Rectangle implements Comparable<Rectangle> {
    private double length;
    private double width;
    private double perimeter;


    public Rectangle(double l, double w){
        this.length = l;
        this.width = w;
        perimeter = 2 *(width+length);
    } 

    public  double getLength() {
        return length;
    }

    public  double getWidth(){
        return width;
    }

    public void setLength(double l){
        length= l;
    }
    public void setWidth(double w){
        width = w;
    }

    public double getPerimeter(){
        perimeter = 2*(width + length);
        return perimeter;
    }


    @Override
    public int compareTo(Rectangle other){
        return Double.compare(perimeter, other.perimeter);
    }

    @Override
    public String toString(){
        return "Rectangle: "+ width +" by "+ length ;
    }

    public static void main(String args[]) {
        Rectangle [] arrayOfRectangles = new Rectangle [3];
        arrayOfRectangles[0] = new Rectangle(1.0,1.0);
        arrayOfRectangles[1] = new Rectangle(6.0,5.0);
        arrayOfRectangles[2] = new Rectangle(2.0,3.0);
        Arrays.sort(arrayOfRectangles);
        for (int i = 0; i < 3; i++) {
            System.out.println(arrayOfRectangles[i]);
        }
    }




}

Few things - 一些事情 -

  1. You can evaluate the parameter of a Rectangle within the constructor instead of the getter as 您可以在构造函数中而不是getter中评估Rectangle的参数,如下所示:

     public Rectangle(double l, double w) { this.length = l; this.width = w; this.perimeter = 2 * (this.width + this.length); } 

    and then your perimeter s getter and setter be like - 然后你的perimeter吸气器和坐骑器就像-

     public double getPerimeter() { return perimeter; } public void setPerimeter(double perimeter) { this.perimeter = perimeter; } 
  2. You can compare using the Double class compare method as 您可以使用Double类的compare方法compare

     @Override public int compareTo(CompareToRectangle other) { return Double.compare(this.perimeter, other.perimeter); } 
  3. You can test the same using a test method as - 您可以使用以下测试方法进行测试-

     public static void testRectangle() { Rectangle first = new Rectangle(2.00, 3.00); Rectangle second = new Rectangle(4.00, 3.00); if (first.compareTo(second) == 0) { System.out.println("Similar rectangles."); } else if(first.compareTo(second) == 1) { System.out.println("First rectangle bigger than second."); } else { System.out.println("Second rectangle is bigger than first."); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM