1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| import java.util.Scanner; abstract class Shape { double PI = 3.14; public abstract double getPerimeter(); public abstract double getArea(); } class Rectangle extends Shape { int wide, len; Rectangle(int a, int b) { wide = a; len = b; } @Override public double getPerimeter() { return 2 * (wide + len); } @Override public double getArea() { return wide * len; } public String toString(){ return "[width=" + wide + ", length=" + len + "]"; } } class Circle extends Shape { int radius; Circle(int _radius) { radius = _radius; } @Override public double getPerimeter() { return radius * 2 * PI; } @Override public double getArea() { return radius * radius * PI; } public String toString(){ return "[radius=" + radius + "]"; } } public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); Shape A[] = new Shape[n]; int k = 0, j = 0; double sumAllArea = 0, sumAllPerimeter = 0; for (int i = 0; i < n; i++) { String S = in.next(); if (S.equals("rect")) { int wide = in.nextInt(), len = in.nextInt(); in.nextLine(); A[i] = new Rectangle(wide, len); } else if (S.equals("cir")) { int radius = in.nextInt(); in.nextLine(); A[i] = new Circle(radius); } sumAllArea += A[i].getArea(); sumAllPerimeter += A[i].getPerimeter(); } System.out.println(sumAllPerimeter); System.out.println(sumAllArea); System.out.print("["); for (int i = 0; i < n; i++) { if(i != 0) System.out.print(", "); if (A[i] instanceof Rectangle) { System.out.print("Rectangle "); System.out.print(A[i].toString()); } else { System.out.print("Circle "); System.out.print(A[i].toString()); } } System.out.println("]"); for(int i = 0;i < n;i++) { if(A[i] instanceof Rectangle) { System.out.println("class Rectangle,class Shape"); }else { System.out.println("class Circle,class Shape"); } } in.close(); } }
|