自己寫的,也才學不容易還望采納
import java.util.*;
abstract class Shape{
final static double PI=3.14;
public abstract double getPerimeter();
public abstract double getArea();
public static double sumAllArea(Shape []shapes) {
double x=0;
for(Shape e:shapes) {
x+=e.getArea();
}
return x;
}
public static double sumAllPerimeter(Shape []shapes) {
double y=0;
for(Shape e:shapes) {
y+=e.getPerimeter();
}
return y;
}
}
class Rectangle extends Shape{
private int width;
private int length;
public double getPerimeter() {
return 2*(width+length);
}
public double getArea() {
return width*length;
}
public Rectangle(int width,int length) {
this.width=width;
this.length=length;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", length=" + length + "]";
}
}
class Circle extends Shape{
private int radius;
public double getPerimeter() {
// TODO Auto-generated method stub
return 2*PI*radius;
}
public double getArea() {
// TODO Auto-generated method stub
return PI*radius*radius;
}
public Circle(int radius) {
this.radius=radius;
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n;
n=in.nextInt();
Shape shapes[]=new Shape[n];
for(int i=0;i<n;i++) {
String s=in.next();
switch(s) {
case "rect":
int width,length;
width=in.nextInt();
length=in.nextInt();
shapes[i]=new Rectangle(width, length);
&