java 第五周实验【报告1】

1.封装一类三角形对象Triangle,该类对象具有三条边的属性,具有初始化三角形的功能、修改边长的功能、判断三条边能否构成三角形的功能、求周长的功能、求面积的功能。

 

 

 

 

public class TestTriangle {
	
	private double a,b,c;
	
	TestTriangle(double a,double b,double c){
		this.a = a;
		this.b = b;
		this.c = c;
	}
	
	TestTriangle(){
		this.a = 6;
		this.b = 8;
		this.c = 10;
	}
	
	 public void set_edge(double a,double b,double c){
		this.a = a;
		this.b = b;
		this.c = c;
	}
	
	public double get_Area(double a,double b,double c){
		return (this.a+this.b+this.c);
			
	}
	
	public double get_Perimer(double a,double b,double c){
		double s;
		s=(this.a+this.b+this.c)/2;
		return(Math.sqrt(s*(s-this.a)*(s-this.b)*(s-this.c)));
	}
	
	public boolean is_Triangle(double a,double b,double c)
	{
		if(this.a+this.b>this.c&&this.a+this.c>this.b&&this.c+this.b>this.a)
		{
			System.out.println("是三角形");
			return true;
		}
		else{
			System.out.println("不是三角形");
			return false;
		}
	}
	
	public static void main(String[] args) {
		TestTriangle t = new TestTriangle();
		t.get_Area(3,4,5);		
		t.is_Triangle(3,4,5);
		t.get_Perimer(3,4,5);
		double c,x;
		c=t.get_Area(3, 4, 5);
		x=t.get_Perimer(3,4,5);
		
		System.out.println("面积为 "+c+"   周长为 "+x);
	}

}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章