7-4 jmu-Java-03オブジェクトベース向け-03-シェイプ

3027 ワード


1.長方形クラスと円形クラスCircleの定義
長方形クラス-クラス名:Rectangle、privateプロパティ:int width,length円形クラス-クラス名:Circle、privateプロパティ:int radiusコンストラクタの作成:パラメトリックコンストラクタ付き:Rectangle(width, length),Circle(radius)作成方法:public int getPerimeter()、周長を求めます.public int getArea()、面積を求めます.toStringメソッドは、Eclipseを使用して自動的に生成されます.
注意:
  • は、Math.PIを使用して円形の面積と周長を計算する.
  • 周長と面積を求める場合は、まずその値(小数ビット付き)を計算し、intに強制的に変換してから戻る.

  • 2.mainメソッド
  • は、2つの長さと幅を入力し、対応する配列に2つのRectangleオブジェクトを配置します.
  • 2 2行の半径を入力し、対応する配列に2つのCircleオブジェクトを配置します.
  • 出力1:上の2つの配列のすべてのオブジェクトの長さを合計します.
  • 出力2:上の2つの配列のすべてのオブジェクトの面積を合計します.
  • 最後に、Arrays.deepToStringを使用して、上記のRectangle配列とCircle配列の
  • をそれぞれ出力必要がある.
    思考:初めてこの問題をすると、コードの冗長性が深刻であることがわかります.継承、マルチステート思想を用いることで、上記のコードを大幅に簡略化することができる.
    サンプルを入力:
    1 2
    3 4
    7
    1

    出力サンプル:
    69
    170
    [Rectangle [width=1, length=2], Rectangle [width=3, length=4]]
    [Circle [radius=7], Circle [radius=1]]

     
    import java.util.Arrays;
    import java.util.Scanner;
    
    class Rectangle{
        private int length;
        private int width;
    
    
        public Rectangle(int width, int length)
        {
            this.length = length;
            this.width = width;
        }
    
        public int getPerimeter()
        {
            return (length + width) * 2;
        }
    
        public int getArea(){
            return width * length;
        }
    
        public String toString() {
            return "Rectangle [" + "width=" + width + ", length=" + length + ']';
        }
    }
    
    class Circle{
        private int radius;
        private double s;
    
        public Circle(int radius)
        {
            this.radius = radius;
        }
    
        public int getPerimeter()
        {
            s =  (Math.PI * 2 * radius);
            return (int)s;
        }
    
        public int getArea(){
            s = (Math.PI * radius * radius);
            return (int) s;
        }
    
        public String toString() {
            return "Circle [" + "radius=" + radius + ']';
        }
    }
    
    public class Main {
    
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
                Rectangle[] rectangle=new Rectangle[2];
                rectangle[0]=new Rectangle(in.nextInt(),in.nextInt());
                rectangle[1]=new Rectangle(in.nextInt(),in.nextInt());
                Circle[] circles=new Circle[2];
                circles[0]=new Circle(in.nextInt());
                circles[1]=new Circle(in.nextInt());
                System.out.println(rectangle[0].getPerimeter()+rectangle[1].getPerimeter()+
                        circles[0].getPerimeter()+circles[1].getPerimeter());
                System.out.println(rectangle[0].getArea()+rectangle[1].getArea()+
                        circles[0].getArea()+circles[1].getArea());
                System.out.println(Arrays.deepToString(rectangle));
                System.out.println(Arrays.deepToString(circles));
        }
    
    }