//此程序写出三个类,triangle,lader,circle;其中triangle类具有类型为double的a,b,c边以及周长,面积属性, //具有周长,面积以及修改三边的功能,还有判断能否构成三角形的boolean类。 //lader类具有类型为double的上底下底和高,面积属性,具有返回面积的功能 //circlle 类具有类型为double的半径,周长和面积。具有返回周长面积的功能 //Lader类 class Lader { double shangDi; //上底 double xiaDi; //下底 double heigth; //高 double area; //面积 //下面是构造方法 public Lader(double shangDi,double xiaDi,double heigth){ this.shangDi=shangDi; this.xiaDi=xiaDi; this.heigth=heigth; } //下面是求面积 public double getArea(){ return (shangDi+xiaDi)*heigth/2; } } //Circle类 class Circle{ double r; //半径 double l; //周长 double area; //面积 public static final double PI=3.1415926; //下面是构造方法 public Circle(double r){ this.r=r; } //得到周长 public double getL(){ return 2*PI*r; } //得到面积 public double getArea(){ return 2*PI*r*r; } } public class Triangle { //size1,size2,size3分别是三角形的三条边 private double size1; private double size2; private double size3; private boolean isTriangle; //下面构造方法 public Triangle(double size1, double size2, double size3) { this.size1 = size1; this.size2 = size2; this.size3 = size3; isTriangle=triangle(); } //判断是否能构造三角形 private boolean triangle() { if((size1+size2>size3)&& (size1+size3>size2)&& (size2+size3>size1)&& (size1-size2<size3)&& (size1-size3<size2)&& size2-size3<size1) return true; return false; } //下面是得到三边长 public double getSize1() { return size1; } public void setSize1(double size1) { this.size1 = size1; } public double getSize2() { return size2; } public void setSize2(double size2) { this.size2 = size2; } public double getSize3() { return size3; } public void setSize3(double size3) { this.size3 = size3; } public boolean isTriangle() { return isTriangle; } public void setTriangle(boolean isTriangle) { this.isTriangle = isTriangle; } //得到面积 public double getArea() { if(isTriangle) { double c=getPermie()/2; return Math.sqrt(c*(c-size1)*(c-size2)*(c-size3)); } throw new IllegalArgumentException("三边不能构成上三角形!"); } //得到周长 public double getPermie() { if(isTriangle) return size1+size2+size3; throw new IllegalArgumentException("三边不能构成上三角行!"); } public static void main(String[] args) { Triangle triangle=new Triangle(3, 4, 5); Lader lader = new Lader(2.00,3.00,2.00); //三个参数分别是上底 下底 高 Circle circle = new Circle(3.00); //参数是圆的半径 System.out.println("面积是"+lader.getArea()); System.out.println("圆形周长是"+circle.getL()); System.out.println("圆形面积是"+circle.getArea()); System.out.println("三角形的周长:"+triangle.getPermie()); System.out.println("三角形的面积:"+triangle.getArea()); } }
运行结果如下:
面积是5.0 圆形周长是18.849555600000002 圆形面积是56.54866680000001 三角形的周长:12.0 三角形的面积:6.0