编写一个完整的Java Application 程序。包含接口ShapeArea, Rectangle类,Triangle类及Test类,具体要求如下:(共计17分)
(1)为接口ShapeArea定义两个方法:(2分)
double getArea( ); //求一个形状的面积
double getPerimeter ( );//求一个形状的周长
⑵类 Rectangle:实现ShapeArea接口,并有以下属性和方法:(5分)
1) 属性
width: double类型,表示矩形的宽
length: double类型,表示矩形的长
2) 方法
Rectangle(double w, double h) //构造函数,为长宽赋值
toString( ) //成员方法,输出矩形的描述信息,如“width=1.0,length=2.0,perimeter=6.0, area=2.0”
⑶类Triangle:实现ShapeArea接口,并有以下属性和方法:(5分)
1) 属性
x,y,z: double型,表示三角形的三条边
s: 周长的1/2(注:求三角形面积公式为s*(s-x)(s-y)(s-z)的平方根,其中s=(x+y+z)/2 ,开方可用Math.sqrt(double)方法)
2)方法
Triangle(double x, double y, double z) //构造函数,给三条边和s赋初值。
toString( )//成员方法,输出三角形的描述信息,如“three sides:3.0,4.0,5.0,perimeter=12.0,area=6.0”
⑷Test类作为主类要完成测试功能(5分)
1) 生成Rectangle对象,并调用对象的toString()方法,输出对象的描述信息
2)生成Triangle对象,并调用对象的toString()方法,输出对象的描述信息
接口:ShapeArea
package 中国;
public interface ShapeArea {
double getArea();求一个形状的面积
double getPerimeter();//求一个形状的周长
}
类:Rectangle
package 中国;
public class Rectangle implements ShapeArea {
private double width;//表示矩形的宽
private double length;//表示矩形的长
public Rectangle(){}
public Rectangle(double w, double h){ //构造函数,为长宽赋值
this.width=w;
this.length=h;
}
@Override
public String toString() {
return "Rectangle [width=" + getWidth() + ", length=" + getLength() +
", perimeter="+getPerimeter()+", area="+getArea()+"]";
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return getWidth()*getLength();
}
@Override
public double getPerimeter() {
// TODO Auto-generated method stub
return (getWidth()+getLength())*2;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
}
类:Triangle
package 中国;
//注:求三角形面积公式为s*(s-x)*(s-y)*(s-z)的平方根,其中s=(x+y+z)/2 ,开方可用Math.sqrt(double)方法
public class Triangle implements ShapeArea {
private double x,y,z;//三角形的三边
private double s;//周长的一半
public Triangle(){}
public Triangle(double x, double y, double z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return "Triangle [x=" + x + ", y=" + y + ", z=" + z +
", perimeter="+getPerimeter()+", area="+getArea()+"]";
}
@Override
public double getArea() {
// TODO Auto-generated method stub
setS();
return Math.sqrt(getS()*(getS()-getX())*(getS()-getY())*(getS()-getZ()));
}
@Override
public double getPerimeter() {
// TODO Auto-generated method stub
setS();
return getS();
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
public double getS() {
return s;
}
public void setS(/* double s */) {
this.s = (getX()+getY()+getZ())/2;
}
}
Test类作为主类
package 中国;
public class Text {
public static void main(String[] args) {
Rectangle r = new Rectangle(1,2);
Triangle t = new Triangle(3,4,5);
System.out.println(r.toString());
System.out.println(t.toString());
}
}
结果: