Post

2023.DEC.14(THU) JAVA DAY 9_원, 사각형 넓이_extends

✅ Daily Report

📌 TO-DO LIST

  • submit github blog post
  • lesson 34, 35
  • assigment: 원, 사각형 넓이_extends

✅ 원과 사각형의 넓이 구하기

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
public class Main {
    public static void main(String[] args) {
        Shape shape1 = new Circle("빨강", 4.0);
        Shape shape2 = new Rectangle("파랑", 3.0, 4.0);

        System.out.println("Shape1 면접 크키: " + shape1.getArea());
        System.out.println("Shape2 면접 크키: " + shape2.getArea());

        System.out.println("------------------------");

        if (shape1 instanceof Circle) {
            Circle circle = (Circle) shape1;
            circle.printCircleInfo();
        }

        System.out.println("------------------------");

        if (shape2 instanceof Rectangle) {
            Rectangle rectangle = (Rectangle) shape2;
            rectangle.printRectangleInfo();
        }
    }

}

//        Shape1 면접 크키: 50.24
//        Shape2 면접 크키: 12.0
//        ------------------------
//        도형의 색상: 빨강
//        도형의 면적: 50.24
//        원의 반지름: 4.0
//        ------------------------
//        도형의 색상: 파랑
//        도형의 면적: 12.0
//        사각형의 가로 길이: 3.0
//        사각형의 세로 길이: 4.0


public class Shape {
    protected String color;

    protected Shape(String color) {
        this.color = color;
    }

    public double getArea() {
        return 0.0;
    }

    protected void printInfo() {
        System.out.println("도형의 색상: " + color);
        System.out.println("도형의 면적: " + getArea());
    }
}

public class Circle extends Shape {
    private static final double PI = 3.14;
    private double radius;

    public Circle(String color, double radius) {
        super(color);
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return radius * radius * PI;
    }

    public void printCircleInfo() {
        super.printInfo();
        System.out.println("원의 반지름: " + this.radius);
    }

}

public class Rectangle extends Shape{
    private double width;
    private double height;

    public Rectangle(String color, double width, double height) {
        super(color);
        this.width= width;
        this.height= height;
    }

    @Override
    public double getArea() {
        return width * height;
    }

    public void printRectangleInfo() {
        super.printInfo();
        System.out.println("사각형의 가로 길이: " + this.width);
        System.out.println("사각형의 세로 길이: " + this.height);
    }
}
This post is licensed under CC BY 4.0 by the author.