-
Notifications
You must be signed in to change notification settings - Fork 0
/
week11_1.java
73 lines (65 loc) · 1.48 KB
/
week11_1.java
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
interface Shape{
final double PI = 3.14;
void draw();
double getArea();
default public void redraw() {
System.out.print("---다시 그립니다. ");
draw();
}
}
class Circle implements Shape{
private int radius = 0;
public Circle(int r) {
radius = r;
}
@Override
public void draw() {
System.out.println("반지름이 " + radius + "인 원입니다.");
}
@Override
public double getArea() {
return radius * radius * PI;
}
}
class Oval implements Shape{
private int radius = 0;
private int height = 0;
public Oval(int r, int h) {
radius = r;
height = h;
}
@Override
public void draw() {
System.out.println(radius + 'x'+ height + "에 내접하는 타원입니다.");
}
@Override
public double getArea() {
return radius * height * PI;
}
}
class Rect implements Shape{
private int width = 0;
private int height = 0;
public Rect (int w, int h) {
width = w;
height = h;
}
@Override
public void draw() {
System.out.println(width + 'x'+ height + "크기의 사각형입니다.");
}
@Override
public double getArea() {
return width * height;
}
}
public class week11_1 {
public static void main(String[] args) {
Shape [] list = new Shape[3];
list[0] = new Circle(10);
list[1] = new Oval(20, 30);
list[2] = new Rect(10, 40);
for(int i=0; i<list.length; i++) list[i].redraw();
for(int i =0; i<list.length; i++) System.out.println("면적은 " + list[i].getArea());
}
}