前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >已知三角形边长,求三角形面积

已知三角形边长,求三角形面积

作者头像
Twcat_tree
发布2022-11-30 20:13:15
发布2022-11-30 20:13:15
51200
代码可运行
举报
文章被收录于专栏:二猫の家二猫の家
运行总次数:0
代码可运行

利用海伦公式求面积:

1.编写三角形类

代码语言:javascript
代码运行次数:0
复制
package com.sanj.bean;

import com.sanj.exception.NotSanjiaoException;

import java.math.BigDecimal;

public class Sanj {

     private int x;
	    private int y;
	    private int z;

	    public Sanj() {
	    }

	    public Sanj(int x, int y, int z) {
	        this.x = x;
	        this.y = y;
	        this.z = z;
	    }

	    /**
	     * 获取三角形面积
	     * @return
	     */
	    public double getArea(){
	        //利用海伦公式求三角形面积
	        BigDecimal bigDecimal1 = new BigDecimal((this.x+this.y+this.z));
	        BigDecimal bigDecimal2 = new BigDecimal(2);
	        double p = bigDecimal1.divide(bigDecimal2, BigDecimal.ROUND_HALF_UP).doubleValue();
	        double area = Math.sqrt(p * (p - this.x) * (p - this.y) * (p - this.z));
	        return area;
	    }

	    /**
	     * 展示三角形边长
	     */
	    public void showInfo(){
	        System.out.println("三角形信息:");
	        System.out.println("x边:" + this.x + " y边:" + this.y + " z边:" + this.z );
	    }

	    /**
	     * 检查三边是否能组成三角形
	     */
	    public void check() throws NotSanjiaoException {
	        //校验三条边长非负数
	        if (this.x <= 0 || this.y <= 0 || this.z <= 0)
	            throw new NotSanjiaoException("x边:" + this.x + " y边:" + this.y + " z边:" + this.z + " 不能构成三角形");

	        //任意两边之和大于第三边
			if ((this.x + this.y) <= this.z || (this.x + this.z) <= this.y || (this.y + this.z) <= this.x)
				throw new NotSanjiaoException("x边:" + this.x + " y边:" + this.y + " z边:" + this.z + " 不能构成三角形");
			//任意两边之差小于第三边
			if ((this.x - this.y) >= this.z || (this.x - this.z) >= this.y || (this.y - this.z) >= this.x)
				throw new NotSanjiaoException("x边:" + this.x + " y边:" + this.y + " z边:" + this.z + " 不能构成三角形");
		}
}

2.编写自定义异常

代码语言:javascript
代码运行次数:0
复制
package org.example.exception;
	
	/**
	 * 自定义异常类
	 * 三边不能组成三角形异常
	 */
	public class NotSanjiaoException extends Exception{
	
	    public NotSanjiaoException() {
	
	    }
	
	    public NotSanjiaoException(String message) {
	        super(message);
	    }
	
	
	
	}

3.测试

代码语言:javascript
代码运行次数:0
复制
package org.example;

import org.example.bean.Sanj;
import org.example.exception.NotSanjiaoException;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws NotSanjiaoException {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入第一条边边长:");
        int x = sc.nextInt();
        System.out.println("输入第二条边边长:");
        int y = sc.nextInt();
        System.out.println("输入第三条边边长:");
        int z = sc.nextInt();
        Sanj sanj = new Sanj(x,y,z);
        sanj.check();
        sanj.showInfo();
        double area = sanj.getArea();
        System.out.println("三角面积:"+area);
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档