声明三角形类,继承图形抽象类,计算三角形的面积和周长
public abstract class closedFigure
{
protected String shape;
protected closedFigure(String shape)
{this.shape=shape;
}protected closedFigure()
{this("未知");
}
public abstract double area();
public abstract double perimeter();
public void print()
{System.out.println("一个"+this.shape+","+this.toString()+",周长为"+this.perimeter()+",面积
为"+this.area());
}
}
public class Sjx extends closedFigure
{protected double a;
protected double b;
protected double c;
public Sjx(double a,double b,double c)
{super("三角形");
this.a=a;
this.b=b;
this.c=c;
}
public Sjx()
{this(0,0,0);
}public String toString()
{return "a边长"+this.a+",b边长"+this.b+",c边长"+this.c;
}
public double area()
{return (sqrt ((a+b+c)/2.0)* (((a+b+c)/2.0)-a) *(((a+b+c)/2.0)-b)*(((a+b+c)/2.0)-c));
}
public double perimeter()
{
return (a+b+c);
}
}
怎么在jdk中编译,而且他告诉我有两处错误。
------解决方案--------------------
唉,楼主的标题……以为真有什么好玩的事情……
2处错误是:
1、一个java文件中,最多只能有一个类(或接口)用public修饰,并且该类(或接口)的名称
和文件名相同,故将public abstract class closeFigure中的public去掉;
2、sqrt不能直接拿来用,改为Math.sqrt()就行了。
PS:类/接口的名称首字母要大写!
------解决方案--------------------
唉,楼主的标题……以为真有什么好玩的事情……
2处错误是:
1、一个java文件中,最多只能有一个类(或接口)用public修饰,并且该类(或接口)的名称
和文件名相同,故将public abstract class closeFigure中的public去掉;
2、sqrt不能直接拿来用,改为Math.sqrt()就行了。
PS:类/接口的名称首字母要大写!
------解决方案--------------------
不好意思,刚才在搞测试,多粘了一块代码,修正一下:
- Java code
import java.util.*; abstract class Figure{ protected String shape; protected Figure(String shape) { this.shape = shape; } protected Figure() { this("未知"); } public abstract double area(); public abstract double perimeter(); public void print() { System.out.println("一个" + this.shape + "," + this.toString() + ",周长为" + this.perimeter() + ",面积为" + this.area()); }}class Triangle extends Figure{ protected double a; protected double b; protected double c; public Triangle(double a, double b, double c) { super("三角形"); this.a = a; this.b = b; this.c = c; } public Triangle() { this(0, 0, 0); } public String toString() { return "a边长" + this.a + ",b边长" + this.b + ",c边长" + this.c; } public double area() { return (Math.sqrt((a + b + c) / 2.0) * (((a + b + c) / 2.0) - a) * (((a + b + c) / 2.0) - b) * (((a + b + c) / 2.0) - c)); } public double perimeter() { return (a + b + c); }}