Java第五周实验报告任务一【封装一类三角形对象Triangle】

/* (程序头部注释开始)

* 程序的版权和版本声明部分

* 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值