java网课学习的练习题:在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积

java网课学习的练习题:在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积

网课地址:https://www.bilibili.com/video/BV1CJ411m7gg?p=57
在这里插入图片描述

练习要求如上
Circle类

public class Circle {
	double radius;

	Circle() {
		radius = 1;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	public double getArea() {
		return 3.14*radius*radius;
	} 
}

Cylinder类

public class Cylinder extends Circle {
	double length;
	
	public Cylinder() {
		this.length=1;
	}
	//获取圆柱的高
	public double getLength() {
		return length;
	}
//设置圆柱的高
	public void setLength(double length) {
		this.length = length;
	}
	//返回和计算圆柱的体积
	public double getVolume() {
		return this.getArea()*this.length;
	}
}

TestCylinder

public class TestCylinder {
	public static void main(String[] args) {
		Cylinder cylinder = new Cylinder();
		//设置圆的半径和圆柱的高都为2
		cylinder.setRadius(2);
		cylinder.setLength(2);
		
		cylinder.getRadius();
		cylinder.getLength();
		
		System.out.println("圆的面积:"+cylinder.getArea());
		System.out.println("圆柱的体积:"+cylinder.getVolume());
	}
}

运行结果
在这里插入图片描述

请大家多多指教!谢谢!

  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值