//此程序写出三个类,triangle,lader,circle;其中triangle类具有类型为double的a,b,c边以及周长,面积属性,//具有周长,面积以及修改三边的功能,还有判断能否构成三角形的boolean类。//lader类具有类型为double的上底下底和高,面积属性,具有返回面积的功能//circlle 类具有类型为double的半径,周长和面积。具有返回周长面积的功能。//Triangle类 class triangle{ public double a; public double b; public double c; public triangle(){ a=0.0; b=0.0; c=0.0; } //下面是引用对象的值 public triangle(double a,double b,double c){ this.a=a; this.b=b; this.c=c;} private boolean isTrue(){ if(a+b>c&&a+c>b&&b+c>a)return true; else return false; } public double getPerimeter(){ if(this.isTrue())return a+b+c; else return -1; } public double getArea(){ double s=(a+b+c)/2; if(this.isTrue())return Math.sqrt(s*(s-a)*(s-b)*(s-c)); else return -1; } public static void main(String[] args){ triangle t=new triangle(); System.out.println(t.getPerimeter()); System.out.println(t.getArea()); triangle q=new triangle(1.2,1.2,1.2); System.out.println(q.getPerimeter()); System.out.println(q.getArea()); } } //Lader类 class Lader { double sd; double xd; double height; double area; //下面是引用对象 Lader(double sd,double xd,double height){ this.sd=sd; this.xd=xd; this.height=height; } public void area(){ area=1.0/2*(sd+xd)*height; System.out.println("梯形面积是:"+area); } } //Circle类 class Circle { double r; double circle; double s; Circle(double r){ this.r=r; } void area(){ circle=2*Math.PI*r; s=Math.PI*Math.pow(r, 2); System.out.println("圆的周长为:"+circle); System.out.println("圆的面积是:"+s); } }
时间: 2024-10-27 08:46:52