题目
用Java语言编写,要求申明三角形类,继承图形抽象类,计算三角形的周长和面积!
提问时间:2020-10-18
答案
图形抽象类的代码:
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
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的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
热门考点
- 1不马上打开礼物,要等客人离开后打开礼物,英语翻译呢?
- 2草原一文主要向我们展示了内蒙古草原的什么和什么,表达了.
- 385减去5与16的积,所得的差去除40,商是多少
- 4初二英语选择恰当的词填空
- 5Na Al N 最高价氧化物对应水化物间相互反应的离子方程式
- 6电磁波在现代信息传递中,起着极其重要的作用,下面利用了电磁波的是( ) A.电视广播 B.无线电通信 C.移动电话 D.固定电话
- 7王师傅和徒弟一起干活,王师傅比徒弟多做了40个零件,两人做的零件个数的比是10:9.两人一共做了多少个零件?
- 8小玲从1月1日开始写大字,第一天写了4个,以后每天比前一天多写相同数量的大字,结果全月一共写了589个大字,小玲每天比前一天多写多少个大字?( ) A.1 B.2 C.5 D.6
- 9该是吃早饭的时候了用英语怎么说
- 10【文言文】孔子过泰山侧中的“何为不去也”的“去”、“小子识之”的“识”、“之”分别是什么意思?