在我的程序中找不到错误

时间:2013-03-26 04:42:22

标签: java comparable

第一个代码块在我的主程序中以静态方法 在这个主程序中,我设置了多边形,它们的区域也在getArea()方法中找到,我后来打印出来,但是我在打印数据后使用这个setArea()方法来测试compareTo()方法。
第二个块是我的重写比较,在我的Polygon类中实现了可比性 为什么setArea()方法没有更新区域?
我认为这可能是一个静态问题,但不确定?

public static void printPolyData(){

    try{


        for(int i=0;i<=poly_count-1;i++){

        System.out.println("polygon #" + (i+1)); 
        System.out.println(poly_list.get(i).getSides() + " sides");
        System.out.println(poly_list.get(i).arrayToString());
        System.out.println("Area of Polygon = " + poly_list.get(i).getArea());
        System.out.println("Area of Polygon = " + poly_list.get(i).dListToString());


        }




                poly_list.get(0).setArea(0.00090);
                poly_list.get(1).setArea(0.00094);
                poly_list.get(2).setArea(0.000901);


                System.out.println("Comparing polygon0 (Area=" 
                + poly_list.get(0).getArea() + ") with polygon1 (Area=" 
                + poly_list.get(1).getArea() + ") : " +
                + poly_list.get(0).compareTo(poly_list.get(1)));

                System.out.println("Comparing polygon1 (Area=" 
                + poly_list.get(1).getArea() + ") with polygon0 (Area=" 
                + poly_list.get(0).getArea() + ") : " +
                + poly_list.get(1).compareTo(poly_list.get(0)));

                System.out.println("Comparing polygon0 (Area=" 
                + poly_list.get(0).getArea() + ") with polygon2 (Area=" 
                + poly_list.get(2).getArea() + ") : " +
                + poly_list.get(0).compareTo(poly_list.get(2)));


    }
    catch (Exception e){

        System.out.println("error printing polygon data. " + e.getMessage());

    }


}




 @Override
    public int compareTo(Polygon otherPoly) {



        double otherPolysArea = otherPoly.getArea(); 
        final double error = 0.00005;

        // otherPoly is bigger. current polygon smaller.
        if((this.getArea() - otherPolysArea) < -error){

            return -1;
        }
        // otherPoly is smaller. current polygon bigger.
        else if((this.getArea() - otherPolysArea) > error){

            return 1;
        }
        else{return 0;} // polygons are considered same.

    }

public void setArea(double area){


    this.area = area;

}
  

比较polygon0(Area = 2.0)和polygon1(Area = 1.0):1
  比较polygon1(Area = 1.0)和polygon0(Area = 2.0): - 1   比较Polygon0(Area = 2.0)和polygon2(Area = 1.5):1

0 个答案:

没有答案