반응형

6.4> int 타입의 x, y, radius 필드를 가지는 Circle 클래스를 작성하라. Equals() 메소드를 재정의하여 두 개의 Circle 객체의 반지름이 같으면 두 Circle 객체가 동일한 것으로 판별하도록 하라. Circle 클래스의 생성자는 3개의 인자를 가지며 x, y, radius 필드를 인자로 받아 초기화한다


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
package SecondHW;
class Circle{ //default 접근의 Circle 클래스
    int x; //int 형 변수 x
    int y;//int 형 변수 y
    int radius;//int 형 변수 radius
    public Circle(int x,int y,int radius){ //Circle 클래스의 생성자
        this.x=x; //객체 의 x에 인자 x 대입
        this.y=y; //객체 의 y에 인자 y 대입
        this.radius=radius; //객체 의 radius에 인자 radius 대입
    }
     public boolean equals(Circle c2){ //java.lang 패키지에 속한 Object 클래스(자바클래스구조의 최상위)의 equals 메소드 오버라이딩
        if(this.radius==c2.radius) //객체의 radius와 인자 c2의 radius 비교
            return true//맞으면 true 반환
        else
            return false//다르면 false 반환}}
public class CircleDicision {
 
    public static void main(String[] args) {
        Circle cir1 = new Circle(1,2,3); //cir1 의 x=1,y=2,radius=3으로 초기화
        Circle cir2 = new Circle(2,4,3); //cir2 의 x=2,y=4,radius=3으로 초기화
        System.out.printf("cir1의  x :  %d y : %d  rad : %d \n",cir1.x,cir1.y,cir1.radius); //객체의 인트형 변수들 출력
        System.out.printf("cir2의  x :  %d y : %d  rad : %d \n",cir2.x,cir2.y,cir2.radius); //객체의 인트형 변수들 출력
        if(cir1.equals(cir2)) //cir1.equals(cir2)가 참이면
            System.out.println("같은 원입니다."); //같은 원 출력
        else //아니면
            System.out.println("다른 원입니다."); //다른 원 출력
    }
}
 
cs




반응형

'컴퓨터 & 프로그래밍 & 전자공학 > JAVA' 카테고리의 다른 글

가위바위보  (0) 2017.06.19
대문자 개수 세기  (0) 2017.06.19
추상 클래스  (0) 2017.06.18
상속 클래스  (0) 2017.06.18
ArrayUtility class  (0) 2017.06.18

+ Recent posts