求圆柱的体积

Java圆柱体积计算示例
本文通过定义Java中的Circle和Cylinder类实现圆柱体积的计算。Circle类用于计算圆面积,Cylinder类继承Circle类计算圆柱体积。演示了不同访问权限下类成员的使用情况。

课堂练习6

定义一个圆类(Circle),其所在的包为bzu.info.software;定义一个圆柱类Cylinder,其所在的包为bzu.info.com;定义一个主类A,其所在的包也为bzu.info.com,在A中生成一个Cylinder对象,并输出其体积。编译并运行该类。

试着改变求体积方法的访问权限,查看并分析编译和运行结果

Cylinder类和A类置于不同的包中,通过对求体积方法设置不同的访问权限,查看并分析编译和运行结果

package bzu.info.software;

public class Circle {
double r,area;
public Circle(double r){
	this.r=r;
}
public void setR(double r) {
	this.r=r;
}
public double getR() {
	return r;
}
public double getArea() {
	area=3.14*r*r;
	return area;
}
}
package bzu.info.com;
import bzu.info.software.Circle;
public class Cylinder {
Circle circle;
double height;
public Circle getCircle() {
	return circle;
}
public void setCircle(Circle circle) {  
    this.circle = circle;
}
double getVolme() {
	return circle.getArea()*height;
}
public double getHeight() {  
    return height;  
}
public void setHeight(double height) {  
    this.height = height;  
}  
}
package bzu.info.com;
import bzu.info.software.Circle;
public class A {

	public static void main(String[] args) {
		Cylinder cylinder=new  Cylinder();
		Circle circle=new Circle(10);
		circle.setR(10);
		cylinder.setCircle(circle);  
        cylinder.setHeight(20); 
        double s=cylinder.getVolme();
        System.out.println("底面半径:"+circle.getR()+",圆柱的高:"  
                +cylinder.getHeight()+",圆柱的体积:"+s); 
	}

}


在 Java 中实现计算圆柱体积的代码可以有多种方式,以下为你提供几种不同的实现示例。 ### 简单实现 ```java class Cylinder { double radius; double height; public Cylinder(double radius, double height) { this.radius = radius; this.height = height; } public double getVolume() { return Math.PI * radius * radius * height; } } public class Main { public static void main(String[] args) { Cylinder cylinder = new Cylinder(7, 15); System.out.println("圆柱体的体积是: " + String.format("%.2f", cylinder.getVolume())); } } ``` 在上述代码中,定义一个 `Cylinder` ,该包含两个成员变量 `radius` 和 `height`,用于表示圆柱的底面半径和高度。`getVolume` 方法用于计算圆柱体积,其公式为 `π * r² * h`。在 `Main` 的 `main` 方法中,创建了一个 `Cylinder` 对象,并调用 `getVolume` 方法计算并输出圆柱体积。 ### 接口实现 ```java interface _3D { double volume(); double h = 15; double r = 7; } class circolumn implements _3D { public double volume() { return Math.PI * r * r * h; } } public class Main { public static void main(String[] args) { circolumn column = new circolumn(); System.out.println("圆柱体的体积是: " + String.format("%.2f", column.volume())); } } ``` 此代码中,定义一个 `_3D` 接口,其中包含 `volume` 方法用于计算体积。`circolumn` 实现了 `_3D` 接口,并实现了 `volume` 方法来计算圆柱体积。在 `Main` 的 `main` 方法中,创建了 `circolumn` 对象,并调用 `volume` 方法计算并输出圆柱体积。 ### 继承实现 ```java class Circle { double radius; public Circle(double radius) { this.radius = radius; } } class Cylinder extends Circle { double height; public Cylinder(double radius, double height) { super(radius); this.height = height; } public double getVolume() { return Math.PI * radius * radius * height; } } public class Main { public static void main(String[] args) { Cylinder cylinder = new Cylinder(7, 15); System.out.println("圆柱体的体积是: " + String.format("%.2f", cylinder.getVolume())); } } ``` 在这个实现中,定义了 `Circle` 表示,`Cylinder` 继承自 `Circle` ,并添加了 `height` 成员变量表示圆柱的高度。`getVolume` 方法用于计算圆柱体积。在 `Main` 的 `main` 方法中,创建了 `Cylinder` 对象,并调用 `getVolume` 方法计算并输出圆柱体积
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值