题目
编写一个完整的Java Application 程序。包含类Circle、Cylinder、Main,具体要求如下。
1)编写类Circle,表示圆形对象,包含以下成员
①属性:
- radius:私有,double型,圆形半径;
②方法: - Circle(double radius), 构造方法,用参数设置圆的半径
- Circle(),构造方法,将圆形初始化为半径为0。
- void setRadius(double r):用参数r设置radius的值
- double getRadius():返回radius的值
- double getArea(),返回圆形的面积
- double getPerimeter(),返回圆形的周长
- public String toString( ),将把当前圆对象的转换成字符串形式,例如圆半径为10.0,返回字符串"Circle(r:10.0)"。
(2)编写一个类Cylinder,表示圆柱形对象,包含以下成员
①属性: - height:私有,double型,圆柱体高度;
- circle:私有,Circle类型,圆柱体底面的圆形;
②方法: - Cylinder(double height,Circle circle), 构造方法,用参数设置圆柱体的高度和底面的圆形
- Cylinder(),构造方法,将圆柱体的高度初始化为0,底面圆形初始化为一个半径为0的圆形。
- void setHeight(double height):用参数height设置圆柱体的高度
- double getHeight():返回圆柱体的高度
- void setCircle(Circle circle):用参数circle设置圆柱体底面的圆形
- Circle getCircle():返回圆柱体底面的圆形
- double getArea(),重写Circle类中的area方法,返回圆柱体的表面积
- double getVolume(),返回圆柱体的体积
- public String toString( ),将把当前圆柱体对象的转换成字符串形式,例如半径为10.0,高为5.0,返回字符串"Cylinder(h:5.0,Circle(r:10.0))"。
(3)编写公共类Main,在main()方法中实现如下功能
输入一个整数n,表示有n个几何图形。
对于每一个几何图形,先输入一个字符串,“Circle”表示圆形,“Cylinder”表示圆柱体。
如果是圆形,输入一个浮点数表示其半径。要求计算其面积和周长并输出。
如果是圆柱体,输入两个浮点数分别表示其半径和高度。要求计算其面积和体积并输出。
输入样例:
在这里给出一组输入。例如:
4
Circle 10.0
Cylinder 10.0 10.0
Circle 1.1
Cylinder 1.1 3.4
输出样例:
在这里给出相应的输出。例如:
The area of Circle(r:10.0) is 314.16
The perimeterof Circle(r:10.0) is 62.83
The area of Cylinder(h:10.0,Circle(r:10.0)) is 1256.64
The volume of Cylinder(h:10.0,Circle(r:10.0)) is 3141.59
The area of Circle(r:1.1) is 3.80
The perimeterof Circle(r:1.1) is 6.91
The area of Cylinder(h:1.1,Circle(r:3.4)) is 96.13
The volume of Cylinder(h:1.1,Circle(r:3.4)) is 39.95
代码
import java.util.Scanner;
class Circle{
private double radis;
public Circle(double radius){
this.radis=radius;
}
public Circle(){
this.radis=0;
}
public void setRadius(double r){
this.radis=r;
}
public double getRadius(){
return this.radis;
}
public double getArea(){
return Math.PI*radis*radis;
}
public double getPerimeter(){
return Math.PI*radis*2;
}
public String toString(){
return "Circle(r:"+radis+")";
}
}
class Cylinder{
private double height;
private Circle circle;
public Cylinder(){
this.height=0;
this.circle=new Circle();
}
public Cylinder(double height,Circle circle){
this.height=height;
this.circle=circle;
}
public void setHeight(double height){
this.height=height;
}
public double getHeight(){
return height;
}
public void setCircle(Circle circle){
this.circle=circle;
}
public Circle getCircle(){
return this.circle;
}
public double getArea(){
return 2*circle.getArea()+height* circle.getPerimeter();
}
public double getVolume(){
return circle.getArea()*height;
}
public String toString(){
return "Cylinder(h:"+height+",Circle(r:"+circle.getRadius()+"))";
}
}
public class Main{
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for(int i = 0; i < n; i++) {
String str = input.next();
if(str.equals("Circle")) {
Circle c = new Circle(input.nextDouble());
System.out.println("The area of " + c.toString() + " is " + String.format("%.2f",c.getArea()));
System.out.println("The perimeterof " + c.toString() + " is "+ String.format("%.2f",c.getPerimeter()));
} else if(str.equals("Cylinder")) {
Cylinder r = new Cylinder(input.nextDouble(), new Circle(input.nextDouble()));
System.out.println("The area of " + r.toString() + " is " + String.format("%.2f",r.getArea()));
System.out.println("The volume of " + r.toString() + " is " + String.format("%.2f",r.getVolume()));
}
}
}
}
笔记
- 按部就班语法题,注意pi的写法和嵌套初始化。