/* (程序头部注释开始)
可以构成三角形
三角形的周长是:12.0
三角形的面积是:6.0
修改后的边长为:1.0
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:封装一类三角形对象Triangle
* 作 者: 张培培
* 完成日期: 2012年 09月 28日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:封装一类三角形对象Triangle,该类对象具有三条边的属性;
* 问题描述:具有初始化三角形的功能、修改边长的功能、判断三条边能否构成三角形的功能、求周长的功能、求面积的功能。
* 程序输出:
* 程序头部的注释结束*/
package zhang;
public class Triangle {
private double sideLength1;
private double sideLength2;
private double sideLength3;
public Triangle(double sideLength1,double sideLength2,double sideLength3)
{
this.sideLength1 = sideLength1;
this.sideLength2 = sideLength2;
this.sideLength3 = sideLength3;
}
public void set_sideLength1(double sideLength1)
{
this.sideLength1 = sideLength1;
}
public void set_sideLength2(double sideLength2)
{
this.sideLength2 = sideLength2;
}
public void set_sideLength3(double sideLength3)
{
this.sideLength3 = sideLength3;
}
public double get_sideLength1()
{
return this.sideLength1 ;
}
public double get_sideLength2()
{
return this.sideLength2 ;
}
public double get_sideLength3()
{
return this.sideLength3 ;
}
public void judgment()
{
if(sideLength1 + sideLength2 > sideLength3 && sideLength1 + sideLength3 > sideLength2 && sideLength2 + sideLength3 > sideLength1 && sideLength1 - sideLength2 < sideLength3 && sideLength1 - sideLength3 < sideLength2 && sideLength2 -sideLength3 < sideLength1)
{
System.out.println(" "+"可以构成三角形");
}
else
{
System.out.println(" "+"不能构成三角形");
}
}
public double perimeter()
{
return (this.sideLength1 + this.sideLength2 + this.sideLength3);
}
public double area()
{
double m =(this.sideLength1 + this.sideLength2 + this.sideLength3)/2;
return (Math.sqrt( m *( m - this.sideLength1) * (m - this.sideLength2)* (m - this.sideLength3)));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Triangle triangle = new Triangle(3.0, 4.0, 5.0);
triangle.judgment();
System.out.println("三角形的周长是:"+triangle.perimeter());
System.out.println("三角形的面积是:"+triangle.area());
triangle.set_sideLength1(1.0);
System.out.println("修改后的边长为:"+triangle.get_sideLength1());
}
}
可以构成三角形
三角形的周长是:12.0
三角形的面积是:6.0
修改后的边长为:1.0