Exp03-圆柱体类Cylinder——类的组合。 【问题描述】已知圆柱体Cylinder类由底面圆心(Point类的对象)、底面半径,高等成员组成,圆柱体底面圆平行于XY轴构成的平面。编写Java程序,输入底面圆心floorCenter的坐标(x,y,z)、底面半径radius和高height,构造一个圆柱体对象cylinder。 【样例输入1】
|
import java.util.Scanner;
class Point{
private double x,y,z;
public Point(double x, double y, double z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
public Point()
{
this(0,0,0);
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
@Override
public String toString() {
String str=String.format("(%.3f,%.3f,%.3f)",x,y,z);
return str;
}
}
class Cylinder{
private Point floorCenter ;
private double floorRaidus;
private double floorHeight;
public Cylinder(Point floorCenter, double floorRaidus, double floorHeight) {
super();
this.floorCenter = floorCenter;
this.floorRaidus = floorRaidus;
this.floorHeight = floorHeight;
}
public Point getFloorCenter() {
return floorCenter;
}
public void setFloorCenter(Point floorCenter) {
if(floorCenter == null)
this.floorCenter = floorCenter;
else
System.out.println("The floor center object is null,can't assign again!");
}
public double getFloorRaidus() {
return floorRaidus;
}
public void setFloorRaidus(double floorRaidus) {
this.floorRaidus = floorRaidus;
}
public double getFloorHeight() {
return floorHeight;
}
public void setFloorHeight(double floorHeight) {
this.floorHeight = floorHeight;
}
public double getFloorage()
{
return Math.PI*floorRaidus*floorRaidus;
}
public double getSuperficial_area()
{
return 2*getFloorage()+2*Math.PI*floorRaidus*floorHeight;
}
public double getVolume()
{
return getFloorage()*floorHeight;
}
public Point getCylindeCenter()
{
return new Point(floorCenter.getX(),floorCenter.getY(),floorCenter.getZ()+floorHeight/2);
}
}
public class Classcombination {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.printf("Please enter the x, y and z coordinates of the floor circle center, the radius and height of the cylinder:\n");
double x=sc.nextDouble();
double y=sc.nextDouble();
double z=sc.nextDouble();
double radius=sc.nextDouble();
double height=sc.nextDouble();
sc.close();
Cylinder cylinder=new Cylinder(new Point(x,y,z), radius, height);
System.out.printf("Floorage=%.3f\n",cylinder.getFloorage());
System.out.printf("Superficial area=%.3f\n",cylinder.getSuperficial_area());
System.out.printf("Volume=%.3f\n",cylinder.getVolume());
System.out.println("Cylinder center coorinates are "+cylinder.getCylindeCenter());
}
}