当前位置: 代码迷 >> Java相关 >> 小弟有问题请大哥帮忙
  详细解决方案

小弟有问题请大哥帮忙

热度:126   发布时间:2005-08-17 17:03:00.0
小弟有问题请大哥帮忙
下面是我刚写的程序请问错误是是: abstract class GeometricObject
{ protected String color;
protected double weight;
protected GeometricObject()
{ color ="white";
weight=1.0;
}
protected GeometricObject(String color,double weight)
{this.color=color;
this.weight=weight;
}
public String getColor()
{return color;
}
public void setColor(String color)
{this.color=color;
}
public double getWeight()
{return weight;
}
public void setWeight(double weight)
{this.weight=weight;
}
public abstract double findArea();
public abstract double findPerimeter();
}
class Triangle extends GemometricObject
{ private double side1,side2,side3;
Triangle(double side1,double side2,double side3)
{side1=3.0;
side2=4.0;
side3=5.0;
}
public double findArea()
{double s=(side1+side2+side3)/2;
double area=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}
public double findPerimeter()
{double s=(side1+side2+side3)/2;
return s;
}
}
public class Example
{public static void main(String[] args)
{Triangle t=new Triangle();
double b=t.findArea();
System.out.println("the triangle''s s is:"+t.findPerimeter()+"now the triangle''s area is:"+t.findArea());
}
}
请各位大哥帮帮忙哈
----------------解决方案--------------------------------------------------------
// 代码修改如下,你的那个计算面积的方法我认为是错误,仅从程序角度帮你做了修改
abstract class GeometricObject
{
  protected String color;
  protected double weight;
  protected GeometricObject()
  {
    color ="white";
    weight=1.0;
  }
  protected GeometricObject(String color,double weight)
  {
    this.color=color;
    this.weight=weight;
  }
  public String getColor()
  {
    return color;
  }
  public void setColor(String color)
  {
    this.color=color;
  }
  public double getWeight()
  {
    return weight;
  }
  public void setWeight(double weight)
  {
    this.weight=weight;
  }
  public abstract double findArea();
  public abstract double findPerimeter();
}
class Triangle extends GeometricObject
{
  private double side1,side2,side3;
  Triangle()
  {
    side1=3.0;
    side2=4.0;
    side3=5.0;
  }
  public double findArea()
  {
    double s=(side1+side2+side3)/2;
    double area=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
    return area;
  }
  public double findPerimeter()
  {
    double s=(side1+side2+side3)/2;
    return s;
  }
}
public class Example
{
  public static void main(String[] args)
  {
    Triangle t = new Triangle();
    double b=t.findArea();
    System.out.println("the triangle''s sis:"+t.findPerimeter()+"now the triangle''s area   

is:"+t.findArea());
  }
}
----------------解决方案--------------------------------------------------------
  相关解决方案