题目要求
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8a8df7484e0ed46848b74f2b93ddd57e.png)
样例
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/562f7489e8e54462f7b643b651c5e25a.png)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Shape[] shape = new Shape[n];
double sumArea=0,sumPerimeter=0;
for (int i=0;i<n;i++){
String s = sc.next();
if (s.equals("rect")){
int width = sc.nextInt();
int length = sc.nextInt();
shape[i] = new Rectangle(width,length);
}
else{
int radius = sc.nextInt();
shape[i] = new Circle(radius);
}
sumArea +=shape[i].getArea();
sumPerimeter += shape[i].getPerimeter();
}
System.out.println(sumPerimeter);
System.out.println(sumArea);
System.out.print("[");
for (int i=0;i<n;i++){
System.out.print(shape[i].toString());
if (i!=n-1) System.out.print(", ");
}
System.out.println("]");
for (int i=0;i<n;i++){
System.out.println(shape[i].getClass()+","+shape[i].getClass().getSuperclass());
}
}
}
abstract class Shape{
static double PI = 3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
class Rectangle extends Shape{
int length,width;
double area,perimeter;
public Rectangle(int width,int length){
this.width = width;
this.length = length;
}
@Override
public double getPerimeter() {
perimeter = 2*(width+length);
return perimeter;
}
@Override
public double getArea() {
area = width*length;
return area;
}
public String toString(){
return "Rectangle [width="+width+", length="+length+"]";
}
}
class Circle extends Shape{
int radius;
double area,perimeter;
public Circle (int radius){
this.radius = radius;
}
@Override
public double getPerimeter() {
perimeter = PI*2*radius;
return perimeter;
}
@Override
public double getArea() {
area = PI*radius*radius;
return area;
}
public String toString(){
return "Circle [radius="+radius+"]";
}
}