| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 610 人关注过本帖
标题:接口Comparable使用时提示出错。。
只看楼主 加入收藏
hwshtongxin
Rank: 1
等 级:新手上路
帖 子:35
专家分:4
注 册:2009-1-6
结帖率:66.67%
收藏
 问题点数:0 回复次数: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( (c2)>0 )
              return c1.toString()+"大于"+c2.toString();
          else
             return  c1.toString()+"小于"+c2.toString();
    }
}
///
/**
  在编译的时候,代码(c2)通不过。请问此处compareTo()是否用错了。。那么该如何来实现对比。
   
搜索更多相关主题的帖子: 提示 接口 Comparable 
2010-04-12 21:30
快速回复:接口Comparable使用时提示出错。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051692 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved