用Java语言编写,要求申明三角形类,继承图形抽象类,计算三角形的周长和面积!
题目
用Java语言编写,要求申明三角形类,继承图形抽象类,计算三角形的周长和面积!
答案
图形抽象类的代码:
abstract class MyShape {
abstract int calGirth(); //求周长
abstract double calSquare(); //求面积
}
三角形类的实现:
public class Triangle extends MyShape{
int borderA,borderB,borderC;
Triangle(int a,int b,int c){borderA = a; borderB = b; borderC = c;}
Triangle(){borderA = borderB = borderC = 0;}
@Override
int calGirth() {
return borderA + borderB + borderC;
}
@Override
double calSquare() {
double p = calGirth() / 2;
return Math.sqrt(p * (p - borderA) * (p - borderB) * (p - borderC));
}
public static void main(String[] args) {
Triangle test = new Triangle(3,4,5);
System.out.println("The girth of the triangle is " + test.calGirth());
System.out.println("The square of the triangle is " + test.calSquare());
}
}
实现两个抽象函数,测试结果正确,输出为:
The girth of the triangle is 12
The square of the triangle is 6.0
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
最新试题
- 英语翻译
- 为什么我写不出好文章
- Let’s go on a trip.和 I love to go on trips.如何翻译?
- 被硫酸酸化的浓KMnO4溶液可以吸收SO2吗?
- 如图,BE是角ABD的平分线,CF是角ACD的平分线,BF、CF相交于点G,角BDC=140度,角BGC=110度,求角A的度数
- 考察家乡的自然水域的考察报告
- 数学题:已知向量a与b的夹角为θ,a=(3,3),2b-a=(-1,1),求cosθ的值?
- 这个小女孩穿着雨衣用英语怎么说
- 一道简单的一元一次不等式应用题!
- 以不在同一直线上的四个点为顶点做平行四边形
热门考点