[상속 실습문제2] 다음과 같은 조건을 만족하는 프로그램을 작성 하시오
도형의 x,y 좌표 값과 각 도형의 면적, 둘레를 계산하는 프로그램이다. 해당 구현 클래스 다이어그 램과 클래스의 구조를 참고하여 프로젝트를 완성하시오.
Point Class
package com.hw2.model.vo;
public class Point {
private int x;
private int y;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void draw() {
System.out.print("x : " + x + ", y : " + y);
}
}
Circle Class
package com.hw2.model.vo;
public class Circle extends Point {
private int radius;
public Circle() {
}
public Circle(int x, int y, int radius) {
super(x,y);
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public void draw() {
super.draw();
System.out.printf(", radius : %d, 면적 : %.1f, 둘레 : %.1f",radius, Math.PI * radius * radius , Math.PI * radius * 2);
}
}
Rectangle Class
package com.hw2.model.vo;
public class Rectangle extends Point {
private int width;
private int height;
public Rectangle() {
}
public Rectangle(int x, int y, int width, int height) {
super(x,y);
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public void draw() {
super.draw();
System.out.print(", 면적 : " + width * height + ", 둘레 : " + (width + height)*2);
}
}
실행 Class
package com.hw2.run;
import com.hw2.model.vo.Circle;
import com.hw2.model.vo.Rectangle;
public class Run {
public static void main(String[] args) {
// 크기가 2인 Circle, Rectangle 각각 객체 배열 할당
Circle[] c = new Circle[2];
Rectangle[] r = new Rectangle[2];
// 위의 사용 데이터를 참고하여 각각 초기화
c[0] = new Circle(1, 2, 3);
c[1] = new Circle(3, 3, 4);
r[0] = new Rectangle(-1, -2, 5, 2);
r[1] = new Rectangle(-2, 5, 2, 8);
// 각 도형의 draw 메소드 실행
for(int i = 0; i < c.length; i++) {
c[i].draw();
System.out.println();
}
for(int i = 0; i < r.length; i++) {
r[i].draw();
System.out.println();
}
}
}
컴파일