当前位置: 代码迷 >> Java相关 >> 接口Comparable使用时提示出错。。
  详细解决方案

接口Comparable使用时提示出错。。

热度:199   发布时间:2010-04-12 21:30:11.0
接口Comparable使用时提示出错。。
/**
  写一个抽象类:GeometricObject,并实现Comparable接口,定义max()方法,求两个GeometricObject对象中较大的一个
。编写测试程序,使用max()方法求两个圆中的较大者和两个矩阵中的较大者。
*/
//////////////////////////////////////////////////////
//编写测试程序:
//TestGeometricObject.java
import java.util.Scanner;
public class TestGeometricObject {
    public static void main(String[] args){
    GeometricObject f1= new Circle();
    GeometricObject f2= new Circle();
    GeometricObject R1=new Rectangel();
    GeometricObject R2=new Rectangel();
    ////
    Scanner scanner=new Scanner(System.in);
    System.out.println("input the informaion of Circle,eg: radius: ");
    double  radius1= scanner.nextDouble();
    ((Circle)f1).setRadius(radius1);
    //////
    System.out.println("input the informaion of Circle,eg: radius: ");
    double radius2=scanner.nextDouble();
    ((Circle)f2).setRadius(radius2);
    ////
    System.out.println("input the infromation of Rectangel,eg: width and heigth: ");
    double width1=scanner.nextDouble();
    double heigth1=scanner.nextDouble();
   
    ((Rectangel)R1).setWidth(width1);
    ((Rectangel)R1).setHeight(heigth1);
    ///
    System.out.println("input the infromation of Rectangel,eg: width and heigth: ");
    double width2=scanner.nextDouble();
    double heigth2=scanner.nextDouble();
    //
    ((Rectangel)R2)..setWidth(width2);
   ((Rectangel)R2).setHeight(heigth2);
    ////
    System.out.println("the max area of circle is "+Max.max(f1,f2));
    System.out.println("the max area of rectangel is "+Max.max(R1,R2));
    }
}
///////////////////////////////////////////////////////////////////////
//  GeometricObject.java
public abstract class GeometricObject implements Comparable{
   private String color;
   private boolean Filled;
   private String name;
   ///
   public  GeometricObject(){
   
       this("",false,"white");
   }
   public GeometricObject(String name, boolean Filled, String color) {
       this.name=name;
       this.Filled=Filled;
       this.color=color;
}
////
   public String getColor(){
       return color;
   }
   public boolean isFilled(){
       return Filled;
   }
   public String getName(){
       return name;
   }
  
   ///
   public void setColor(String color){
       this.color=color;
   }
   public void setFilled(boolean x){
       Filled=x;
   }
   public void setName(String name){
       this.name=name;
   }
   //////////////
   public abstract double getArea();
   public abstract double getPerimetric();
   ///////
   //////
   public String toString(){
       String output="";
       output+=name+",has: "+Filled+",color: "+color;
       return output;
   }

}
////////////////////////////////////
//Circle.java
public class Circle extends GeometricObject {
    private double radius;
    //
    public Circle(){
        this("circle",false,"white",0.0);
    }
    public Circle(String name, boolean Filled, String color,double radius){
        super(name,Filled,color);
        this.radius=radius;
    }
    ///
    public double getRadius(){
        return radius;        
    }
    public void setRadius(double radius){
        this.radius=radius;
    }
    ///
    public double getArea(){
        return Math.PI*radius*radius;
    }
    public double getPerimetric(){
        return 2*Math.PI*radius;
    }
    /////

    public int compareTo(Object o) {
   
        return (int)(getArea()-((Circle)o).getArea());
    }
}
//////////////////////////////////////////////////////
//Rectangel.java
public class Rectangel extends GeometricObject {
    private double width;
    private double height;
    ////
    public Rectangel(String name, boolean Filled, String color,double width, double height){
        super(name,Filled,color);
        this.width=width;
        this.height=height;
    }
    public Rectangel(){
        this("Rectangel",false,"white",0,0);
    }
    ///////
    public double getWidth(){
        return width;
    }
    public double getHeight(){
        return height;
    }
    ////
    public void setWidth(double width){
        this.width=width;
    }
    public void setHeight(double height){
        this.height=height;
    }
    ////
    public double getArea(){
        return width*height;
    }
    public double getPerimetric() {
        
        return 2*(width+height);
    }
    public int compareTo(Object o) {
        
        return (int)(getArea()-((Rectangel )o).getArea());
    }
        public String toString(){
        String output="";
        output+=getName()+",has color: "+isFilled()+","+getColor()+",Area: "+getArea()+",Perimetic: "+getPerimetric();
        /////
        return output;
    }
}
///////////////////////////////////////////////////////////////////////
//Max.java
public class Max  {
    public static String max(Comparable c1,Comparable c2){
          if( c1.compareTo(c2)>0 )
              return c1.toString()+"大于"+c2.toString();
          else
             return  c1.toString()+"小于"+c2.toString();
    }
}
///
/**
  在编译的时候,代码c1.comparableTo(c2)通不过。请问此处compareTo()是否用错了。。那么该如何来实现对比。
   
搜索更多相关的解决方案: 提示  接口  Comparable  

----------------解决方案--------------------------------------------------------
  相关解决方案