三角型 梯形 圆 在类中封装

class Trangle
{
	double sideA,sideB,sideC,area,length;
	boolean boo;
	public Trangle(double a,double b,double c)//三角形的三边abc
	{
		sideA=a;
		sideB=b;
		sideC=c;
		if(a+b>c&&b+c>a&&a+c>b)//abc构成三角形的条件表达式
			boo=true;//成立给boo赋值true
		else
			boo=false;//不成立给boo赋值false
	}
	double getLength()//求周长
	{
		return (sideA+sideB+sideC);
	}
	public double getArea()//求面积
	{
		if(boo)
		{
			double p=(sideA+sideB+sideC)/2.0;
			area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
			return area;
		}
		else
		{
			System.out.println("这不是一个三角型,不能计算面积");
			return 0;
		}
	}
	public void setABC(double a,double b,double c)//给三角形修改边长
	{
		sideA=a;
		sideB=b;
		sideC=c;
		if(a+b>c&&b+c>a&&a+c>b)
		{
			boo=true;
		}
		else
			boo=false;
	}
}
class Lader//定义类梯形
{
	public double above,bottom,height,area;
	public Lader(double a,double b,double h)//构造函数
	{
		above=a;
		bottom=b;
		height=h;
	}
	double setArea()//求梯形面积
	{
		return ((above+bottom)*height/2.0);
	}
}
class Circle//定义圆类
{
	double redius,area,length;
	public Circle(double r)//构造函数
	{
		redius=r;
	}
	double getLength()//求圆的周长
	{
		return (2*3.14*redius);
	}
	double getArea()//求圆的面积
	{
		return (3.14*redius*redius);
	}
}
public class AreaandLength
{
	public static void main(String arg[])
	{
		double length,area;
		Circle circle=null;
		Trangle trangle;
		Lader lader;
		circle=new Circle(10);//创建对象
		trangle=new Trangle(3,4,5);
		lader=new Lader(3,4,10);
		length=circle.getLength();
		System.out.println("圆的周长为:"+length);
		area=circle.getArea();
		System.out.println("圆的面积为:"+area);
		length=trangle.getLength();
		System.out.println("三角形的周长为:"+length);
		area=trangle.getArea();
		System.out.println("三角形的面积为:"+area);
		area=lader.setArea();
		System.out.println("梯形的面积为:"+area);
		trangle.setABC(0,0,0);
		area=trangle.getArea();
		System.out.println("三角形的面积为:"+area);
	}
}
运行结果:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值