반응형

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
반응형

5.5>추상 클래스의 서브 클래스 만들기에 필요한 추상 메소드 오버라이딩과 super()의 사용에 관한 문제이다. 다음과 같은 MyPoint 추상 클래스가 있다.




MyPoint를 상속받는 MyColorPoint 클래스를 작성하라. MyColorPoint의 생성자는 MyColorPoint(int x, int y, String color)로 하라. 그리고 다음과 같은 main() 메소드를 삽입하여 실행되도록 하라.





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
package HW1_JAVA;
abstract class MyPoint{ // 추상 클래스 MyPoint
    int x; int y;  //int형 변수 x,y 선언
    public MyPoint(int x,int y){  //생성자 MyPoint int형 변수인 인자 x,y
        this.x=x; this.y=y; //객체의 x에 인자 x 대입, 객체의 y에 인자 y 대입
    }
    protected abstract void move(int x,int y); //추상 메소드 move,int형 인자 x,y
    protected abstract void reverse(); //추상 메소드 reverse
    protected void show(){ // 객체의 x와 y를 출력하는 메소드 show
        System.out.println(x+","+y);
    }
}
class MyColorPoint extends MyPoint{  //MyPoint를 상속하는 MyColorPoint 클래스
    String s; //스트링형 오브젝트 s
    public MyColorPoint(int x, int y, String Color){ //생성자 인자는 int형 x,y와 string Color
        super(x,y); //슈퍼클래스의 생성자 호출
        this.s=Color; //String 레퍼런스 변수 Color가 가리키는 문자열을 this.s가 가리키게함
    }
    protected void move(int x,int y){ //상속받은 추상 메소드 오버라이딩
        this.x=x; this.y=y; //객체의 x에 인자 x 대입, 객체의 y에 인자 y 대입
    }
    protected void reverse(){ //상속받은 추상 메소드 오버라이딩
        int temp; // int형 변수 temp, 빈 물컵 역할
        temp=this.x;  //x와 y를 스왑하는 과정
        this.x=this.y;
        this.y=temp;
    }
    protected void show(){ //슈퍼클래스의 메소드 show 오버라이딩
        System.out.println(x+","+y+","+s);  //x와 y와 s를 출력하는 메소드 show
    }
}
public class Abstract {
    public static void main(String[] args) {//MyPoint형 객체를 가리키는 레퍼런스 변수 p
        MyPoint p=new MyColorPoint(2,3,"blue"); //MyColorPoint클래스의 객체 생성, 업캐스팅
        p.move(34); // 동적 바인딩에 의해 MyColorPoint에서 오버라이딩한 메소드move가 호출됨
        p.reverse(); //동적 바인딩에 의해 MyColorPoint에서 오버라이딩한 메소드reverse가 호출됨
        p.show(); //동적 바인딩에 의해 MyColorPoint에서 오버라이딩한 메소드 show가 호출됨
    }
}
 
cs


반응형

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

대문자 개수 세기  (0) 2017.06.19
메소드 오버라이딩  (0) 2017.06.18
상속 클래스  (0) 2017.06.18
ArrayUtility class  (0) 2017.06.18
직사각형 클래스  (0) 2017.06.18
반응형

5.4> main() 함수를 다음과 같이 수행할 수 있도록 하기 위한 CPoint 클래스와 CColorPoint 클래스를 작성하고 전체 프로그램을 완성하라. CColorPoint 클래스의 어떤 메소드에서도 System.out.println()을 호출해서는 안 된다. CPoint 클래스는 생성자가 오직 하나뿐이다.


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
package HW1_JAVA;
class CPoint{ //class CPoint 선언
    int a; int b; String s;  //int형 변수 a,b 와 String s 선언
    protected CPoint(int a, int b){ //int a와 int b를 argument 로 갖는 생성자
        this.a=a; this.b=b; this.s=""//this.a에 a대입 this.b에 b 대입 s에 "" 대입(색깔들어갈자리)
    }
    void show(){
        System.out.println("("+a+","+b+")"+s); // a와 b와 s를 출력하는 메소드 show
    } //toString 메소드는 Object 클래스에 속한다. 
    public String toString(){ //메소드 오버라이딩 시 슈퍼 클래스 메소드의 접근지정자보다 접근 범위가 좁아질 수 없다.
        return "("+a+","+b+")"+" 입니다";
    } //toString : println의 인자로 객체의 레퍼런스 변수가 전달되면 해당 인스턴스의 toString 메소드가 호출되면서
    } // 리턴값으로 반환되는 문자열이 나온다.
class CColorPoint extends CPoint{ //CPoint를 상속하는 클래스 CColorPoint
        CColorPoint(int a, int b,String s)
        {
            super(a,b); //CColorPoint의 슈퍼클래스인 CPoint의 생성자 호출
            this.s=s; // this.s에 인자 s 대입
        }
    }
public class PointCl {
    public static void main(String[] args) {
        CPoint a,b; //CPoint 형 클래스를 가리키는 레퍼런스 변수 a,b
        a=new CPoint(2,3); // CPoint 클래스의 객체 생성(인자는 2,3) 레퍼런스 변수는 a
        b=new CColorPoint(3,4,"red"); //업캐스팅된 CColorPoint 클래스의 객체를 가리키는 레퍼런스변수 b
        a.show(); //a.show 메소드 호출
        b.show(); //b.show 메소드 호출
        System.out.println(a); //a 출력인데 a.toString의 리턴값이 출력됨
        System.out.println(b); //b 출력인데 b.toString의 리턴값이 출력됨
    }
}
 
cs




반응형

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

메소드 오버라이딩  (0) 2017.06.18
추상 클래스  (0) 2017.06.18
ArrayUtility class  (0) 2017.06.18
직사각형 클래스  (0) 2017.06.18
돈 단위 나누기  (0) 2017.06.18

+ Recent posts