public abstract class GeometricObject {

String color = "white";
boolean filled;

    public GeometricObject() {
    }
    
    public GeometricObject(String color,boolean filled) {
     this.color = color;
     this.filled = filled;
    }    
    
    public abstract double getArea();
    
    public abstract double getPerimeter();
}
---------------------------------------------------------------
public class Circle extends GeometricObject implements Comparable{
   
  double radius;
   
  public Circle() {
  }
   
  public Circle(double radius) {
    this.radius = radius;
  }
   
  public Circle(double radius,String color,boolean filled) {
    this.radius = radius;
    this.color = color;
    this.filled = filled;
  }
   
  public double getArea(){
    return Math.PI * radius * radius;
  }  public double getPerimeter(){
    return 2 * radius * Math.PI;
  }
}
---------------------------------------------------------------
public class Rectangle extends GeometricObject{
    double width;
    double height;    public Rectangle() {
    }
    
    public Rectangle(double width,double height){
     this.width = width;
     this.height = height;
    }
    
    public Rectangle(double width,double height,String color,boolean filled){
     this.width = width;
     this.height = height;
     this.color = color;
        this.filled = filled;
    }
    
    public double getArea(){
     return height * width;
    }
    
    public double getPerimeter(){
     return 2 * (height + width);
    }
}
---------------------------------------------------------
public class Shiyan3{
  public static void main(String[] args){
    Circle c1=new Circle(3);
    Circle c2=new Circle(4);
    Rectangle r1=new Rectangle(2.3,4.5);
    Rectangle r2=new Rectangle(3.1,5.1);
    double result=Sum.sumArea(GeometriObject a[]);//问题出在这个地方,但是我不知道怎么修改。。求教了。
    System.out.println(result);
  }
}
求大神们帮帮忙,修改了一晚上都不行。小弟在此恭候大神来临!

解决方案 »

  1.   

    你这不是修改的问题了,你还有一些函数没有实现,怎么可能出的来结果呢!Sum.sumArea(GeometriObject[])函数你都没实现
      

  2.   

    public class Sum{
      Circle c1=new Circle();
      Circle c2=new Circle();
      Rectangle r1=new Rectangle();
      Rectangle r2=new Rectangle();
      public static double sumArea(GeometriObject[] a){
        GeometriObject [] a={c1,c2,r1,r2};
    double sumarea=c1.getArea()+c2.getArea()+r1.getArea()+r2.getArea();
    return sumarea;
      }
    }
    不好意思哈,少了一个类。。补上这个还是不行,一样的错误
      

  3.   

    double result=Sum.sumArea(GeometriObject a[]);//问题出在这个地方,但是我不知道怎么修改。。求教了。
     改为这样:GeometriObject[] a = new GeometriObject[数组长度] ;
    double result=Sum.sumArea(a);或者这样
    double result=Sum.sumArea(new GeometriObject[数组长度]);
      

  4.   

     double result=Sum.sumArea(GeometriObject a[]);//问题出在这个地方,但是我不知道怎么修改。。求教了。
    调用方法时,要传实际参数,GeometriObject a[] 只是定义了参数,你把这两个概念混淆了。楼上正解。
      

  5.   

    主要错误:形参和实参没弄清。
    形参:主要用于接收,你写一个数据类型+变量名就可以。例如(int a)
    实参:主要给形参传递实际参数(一个确切的值)。例如(5)
      

  6.   

    延续楼主采用数组的思路修改了代码:
    1. Circle.java 类去掉 implements Comparable 
    2. 修改Sum.javapublic class Sum {
    public static double sumArea(GeometricObject[] a) {
    double sumarea = 0.0;
    for(GeometricObject obj : a) {
    sumarea += obj.getArea();
    }

    return sumarea;
    }
    }
    3. 修改Shiyan3.javapublic class Shiyan3 {
    public static void main(String[] args) {
    Circle c1 = new Circle(3);
    Circle c2 = new Circle(4);
    Rectangle r1 = new Rectangle(2.3, 4.5);
    Rectangle r2 = new Rectangle(3.1, 5.1);

    GeometricObject[] a = new GeometricObject[4];
    a[0] = c1;
    a[1] = c2;
    a[2] = r1;
    a[3] = r2;

    double result = Sum.sumArea(a);
    System.out.println(result);

    }
    }
    注:建议用List传递
      

  7.   

    这个不错哦,楼主传的数组,数组注意一下用法就是,反正有length属性