补考:第二题
2022-08-16 本文已影响0人
小金hhh
import java.util.Scanner;
class Circle{
private double radius;
Circle(double radius){
this.radius=radius;
}
Circle(){
radius=0;
}
double getRadius(){
return radius;
}
void setRadius(double r){
radius=r;
}
double getArea(){
return Math.PI*radius*radius;
}
double getPerimeter(){
return 2*Math.PI*radius;
}
public String toString(){
return "Circle(r:"+radius+")";
}
}
class Cylinder{
private double height;
private Circle circle;
Cylinder(double height,Circle circle){
this.height=height;
this.circle=circle;
}
Cylinder(){
height=0;
circle.setRadius(0);
}
double getHeight(){
return height;
}
void setHeight(double height){
this.height=height;
}
Circle getCircle(){
return circle;
}
void setCircle(Circle circle){
this.circle=circle;
}
double getArea(){
return 2*Math.PI*circle.getRadius()*circle.getRadius()+circle.getPerimeter()*height;
}
double getVolume(){
return circle.getArea()*height;
}
public String toString(){
return "Cylinder(h:"+height+","+circle.toString()+")";
}
}
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()));
}
}
}
}