定义三角形类Criangle,其中有成员x,y,z,作为三条边的长。
构造方法Criangle(a,b,c)分别给x,y,z赋值;
方法求面积getArea;
方法显示三角形边长信息showInfo。
在主类方法中构造一个Criangle对象(三边为命令输入的三个数),
当三条边不能构成一个三角形时要抛出自定义异常NoCriangleException,
异常类能捕获异常并处理,否则显示三角形面积Area和边长Info。
Java定义三角形类Criangle。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- qq_43632454 2018-11-07 10:23关注
public class Criangle {
private int x;
private int y;
private int z;public Criangle(int x, int y, int z) { super(); this.x = x; this.y = y; this.z = z; } public double getArea() throws NoCriangleException { if (!isCriangle()) { throw new NoCriangleException("不是三角形"); } double p = (this.x + this.y + this.z) / 2; return Math.sqrt(p * (p - this.x) * (p - this.y) * (p - this.z)); } public boolean isCriangle() { if ((this.x + this.y > this.z) && (this.x + this.z > this.y) && (this.y + this.z > this.x)) { return true; } return false; } public void showInfo() { System.out.println("边长x="+this.x); System.out.println("边长y="+this.y); System.out.println("边长z="+this.z); }
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 谁有desed数据集呀
- ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
- ¥15 关于#hadoop#的问题
- ¥15 (标签-Python|关键词-socket)
- ¥15 keil里为什么main.c定义的函数在it.c调用不了
- ¥50 切换TabTip键盘的输入法
- ¥15 可否在不同线程中调用封装数据库操作的类
- ¥15 微带串馈天线阵列每个阵元宽度计算
- ¥15 keil的map文件中Image component sizes各项意思
- ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏