프로그래밍 언어/JAVA

<Java> 명품 Java programming 5장 실습문제 1 -2 해답

창조적생각 2021. 7. 7. 14:24

[1~2] 다음 TV 클래스가 있다.

 

1
2
3
4
5
6
7
8
9
class TV{
    private int size;
    public TV(int size) {
        this.size = size;
    }
    protected int getSize() {
        return size;
    }
}
cs

 

1. 다음 main() 메소드의 실행 결과를 참고하여 TV를 상속받는 ColorTV 클래스를 작성하라.

 

1
2
3
4
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ColorTV myTV = new ColorTV(32,1024);
        myTV.printProperty();
cs

[실행결과]

 

1
32인치1024컬러
cs

 

[풀이]

 

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
class TV{
    private int size;
    public TV(int size) {
        this.size = size;
    }
    protected int getSize() {
        return size;
    }
}
class ColorTV extends TV{
    int color;
    public ColorTV(int size, int color) {
        super(size);
        this.color = color;
    }
    protected int getColor() {
        return color;
        }
    void printProperty(){
        System.out.print(getSize()+"인치"+getColor()+"컬러");
    }
    
}
    
public class pactice5_1 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ColorTV myTV = new ColorTV(32,1024);
        myTV.printProperty();
    }
 
}
 
cs

 

2. 다음 main()메소드와 실행 결과를 참고하여 문제 1의 ColorTV를 상속받는 IPTV 클래스를 작성하라.

 

1
2
3
4
5
6
public static void main(String[] args) {
        // TODO Auto-generated method stub
        IPTV iptv = new IPTV("192.1.1.2 ",32,2048);
        
        iptv.printProperty();
    }
cs

 

[실행결과]

 

1
나의 IPTV는192.1.1.2 32인치2048컬러
cs

 

[풀이]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class IPTV extends ColorTV{
    String address;
    public IPTV(String address,int size,int color) {
        super(size,color);
        this.address = address;
        System.out.print("나의 IPTV는"+address);
    }
    
}
public class practice5_2 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        IPTV iptv = new IPTV("192.1.1.2 ",32,2048);
        
        iptv.printProperty();
    }
 
}
 
cs

 

728x90