아래는 도형을 정의한 Shape클래스이다. 이 클래스를 조상으로 하는 Circle클래스와 Rectangle클래스를 작성하시오. 이 때, 생성자도 각 클래스에 맞게 적절히 추가해야 한다.
(1)
클래스명 : Circle
조상클래스 : Shape
멤버변수 : double r - 반지름
(2)
클래스명 : Rectangle
조상클래스 : Shape
멤버변수 : double width - 폭, double height - 높이
메서드 :
1. 메서드명 : isSquare
기 능 : 정사각형인지 아닌지를 알려준다.
반환타입 : boolean
매개변수 : 없음
[ 문제 ]
abstract class Shape {
Point p;
Shape() {
this(new Point(0,0));
}
Shape(Point p) {
this.p = p;
}
abstract double calcArea(); // 도형의 면적을 계산해서 반환하는 메서드
Point getPosition() {
return p;
}
void setPosition(Point p) {
this.p = p;
}
}
class Point {
int x;
int y;
Point() {
this(0,0);
}
Point(int x, int y) {
this.x=x;
this.y=y;
}
public String toString() {
return "["+x+","+y+"]";
}
}
[ 정답 ]
class Rectangle extends Shape{
double width;
double height;
Rectangle(double width, double height){
this(new Point(0,0), width, height);
}
Rectangle(Point p, double width, double height){
super(p);
this.width = width;
this.height = height;
}
boolean isSquare(){
return width*height !=0 && width==height;
}
public double calcArea(){
return width * height;
}
}
class Point {
int x;
int y;
Point() {
this(0,0);
}
Point(int x, int y) {
this.x=x;
this.y=y;
}
public String toString() {
return "["+x+","+y+"]";
}
}
class exercise{
public static void main(String[] args){
}
}
[ 나의 풀이 ]
class Rectangle extends Shape {
double width;
double height;
public Rectangle(){}
public Rectangle(Point p, int x, int y){
super();
width=x;
height=y;
}
public double calcArea(double w, double h){
return w*h;
}
public boolean isSquare(){
if( width*height == 0 )
return false;
return false;
}
}
Point(int x, int y) {
this.x=x;
this.y=y;
}
public String toString() {
return "["+x+","+y+"]";
}
}
얼추 비슷하게 푼 것 같은데 문제를 풀면서 많이 부족하다고 느꼈다.
1. Shape 추상클래스
1. abstract class Shape {
2. Point p; // Point타입의 참조변수 p를 선언
3.
4. Shape() {
5. this(new Point(0,0));
6. }
7.
8. Shape(Point p) {
9. this.p = p;
10. }
11.
12.
13. abstract double calcArea(); // 도형의 면적을 계산해서 반환하는 메서드
14.
15. Point getPosition() {
16. return p;
17. }
18.
19. void setPosition(Point p) {
20. this.p = p;
21. }
22. }
Shape() {
Point p = new Point(0,0);
this(p);
} 위 코드를 축약한 것이 아래 코드이다.
Shape(){ this(new Point(0,0));
}
this() 또는 this(매개변수) 는 다른 생성자 호출을 위한 명령어이다.
this(new Point(0,0))은 위 코드를 축약한 것이다.
1. 매개변수가 전달되지 않으면(X) Point 클래스의 x와 y값을 (0, 0)으로 초기화 한다.
2. 매개변수가 전달이 된다면(O) Point 클래스의 x와 y값을 전달되는 값 (x, y)로 초기화 한다.
2. 나는 필요없는 기본 생성자를 생성해주었다.
3.
Rectangle(double width, double height){
this(new Point(0,0), width, height);
}
전달되는 매개변수는 2개인데, this 생성자를 통해서 매개변수 3개를 초기화 하고 있다.
위에서도 말했다 시피 this ( ) 로 되어 있는 것은 "다른 생성자를 호출" 하는 것이다.
이 생성자는 어떤 생성자를 호출하고 있는 것일까? 바로 그 아래 생성자를 호출하고 있다.
Rectangle(Point p, double width, double height){
super(p);
this.width = width;
this.height = height;
}
매개변수로 3개 전달되는 생성자를 호출했다.
여기에 또 다시 super(p)라는 것이 쓰인다.
super ( ) 는 또 다시 조상 클래스의 생성자를 호출하는 것이다.
그렇다면 Rectange의 조상 클래스는 Shape클래스이다. 아래에서 Shape 클래스를 살펴보자.
abstract class Shape {
Point p;
Shape() {
this(new Point(0,0));
}
Shape(Point p) {
this.p = p;
} }
빨간 색으로 표시된 부분의 생성자를 호출하고 있다.
즉, 넘겨받은 p값 (즉, x와 y)로 초기화 한다는 것이다.
Point(int x, int y) {
this.x=x;
this.y=y;
}
'프로그래밍 > 코드분석' 카테고리의 다른 글
continue와 break문의 활용 (0) | 2017.01.28 |
---|---|
[KATA] Triangular Treasure(삼각수) (0) | 2017.01.26 |
자바의 정석 연습문제 6-23 (0) | 2016.08.17 |
자바의 정석 연습문제 6-4 (0) | 2016.08.16 |
자바의 정석 연습문제 6-2 (0) | 2016.08.16 |