派生类

Description
先定义一个圆类Circle,有double类型的半径属性,有带参数的构造函数和求面积的computeArea方法(为保护型的,返回值为面积,圆周率取3.14);
然后定义一个从Circle派生的圆柱类Cylinder,增加double类型的高属性,有带参数的构造函数以及一个计算圆柱体积的成员函数,此函数中要用到从父类继承来的求面积方法。实现并测试这个类,在main()函数中读入数据, 然后输出体积。
要求主函数中必须调用Cylinder的构造函数语句。

Input
第一行输入数据的组数N,
然后有N组数据。每组数据由两个整型数组成,一个是半径,一个是高,数据之间以空格分隔。

Output
输出圆柱体积(保留2位有效数字)。
Sample Input
2
10 10
20 5

Sample Output
3140.00
6280.00

import java.util.*;
class Circle
{
	public double radius;
	public Circle(double radius)
	{
		this.radius=radius;
	}
	public double computeArea(double radius)
	{
		return (3.14*radius*radius);
	}
}
class Cylinder extends Circle
{
double high;
	public Cylinder(double radius,double high) {
		super(radius);
		this.high=high;
		// TODO Auto-generated constructor stub
	}
	double t=super.computeArea(radius);
	public void tiji(double high)
	{
		System.out.printf("%.2f\n",t*high);
	}
}
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
double radius,high;
int t;
Scanner in=new Scanner(System.in);
while(in.hasNext()) {
t=in.nextInt(); 
for(int i=0;i<t;i++) 
{
radius=in.nextDouble();
high=in.nextDouble();
Cylinder A1=new Cylinder(radius,high);
A1.tiji(high);
	}
	high=0;
	radius=0;}}
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不关我事~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值