JAVA题目~圆锥类Cone Exp03-2
【问题描述】定义一个圆锥类Cone,为其设置私有成员变量:radius(底面半径)和height(高)。添加两个参数的构造方法,用两个参数分别初始化两个成员变量;添加无参构造方法,无参构造方法的方法体为空。
公有的方法getVolume()返回圆锥的体积,方法getArea()返回圆锥的底面积;一个参数的方法setRadius()和setHeight()分别用参数设定圆锥的底面半径和高;getRadius()和getHeight()分别返回圆锥的底面半径和高。
在TestCone类的main()方法中创建Cone类的对象c1,c1的底面半径和高均从键盘输入,然后输出c1的底面积和体积。再用默认构造方法创建Cone类的对象c2,将c2的底面半径设置为c1的底面半径加1,将c2的高设置为c1的高加2,再输出c2的底面积和体积。输入输出格式如样例所示,所有浮点数保留小数点后3位。
【样例输入1】
3 4
【样例输出1】
The floorage of c1 is 28.274, and the volume is 37.699
The floorage of c2 is 50.265, and the volume is 100.531【样例输入2】
5 8
【样例输出2】
The floorage of c1 is 78.540, and the volume is 209.440
The floorage of c2 is 113.097, and the volume is 376.991
import java.util.Scanner;
class Cone
{
private double radius;
private double height;
public Cone(double radius, double height) {
super();
this.radius = radius;
this.height = height;
}
public Cone()
{
}
public double getVolume()
{
return getArea()*height/3.0;
}
public double getArea()
{
return Math.PI*radius*radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
public class TestCone {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double r=sc.nextDouble();
double h=sc.nextDouble();
Cone c1=new Cone(r,h);
System.out.printf("The floorage of c1 is %.3f , and the volume is %.3f\n",c1.getArea(),c1.getVolume());
Cone c2=new Cone();
c2.setRadius(c1.getRadius()+1);
c2.setHeight(c1.getHeight()+2);
System.out.printf("The floorage of c2 is %.3f , and the volume is %.3f",c2.getArea(),c2.getVolume());
sc.close();
}
}
版权声明:本文为qq_39953607原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END