더 많이 실패하기

자바의정석 연습문제 풀이 6장 6-21 본문

자바/자바의정석-연습문제

자바의정석 연습문제 풀이 6장 6-21

김발자~ 2022. 9. 29. 20:55
반응형

자바의 정석 3판 연습문제 풀이

 

연습문제 파일은 아래 링크에서 다운받을 수 있다

https://github.com/castello/javajungsuk3/tree/master/%EC%97%B0%EC%8A%B5%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4

 

GitHub - castello/javajungsuk3: soure codes and ppt files of javajungsuk 3rd edition

soure codes and ppt files of javajungsuk 3rd edition - GitHub - castello/javajungsuk3: soure codes and ppt files of javajungsuk 3rd edition

github.com

 

 

http://www.yes24.com/Product/Goods/24259565

 

Java의 정석 - YES24

최근 7년동안 자바 분야의 베스트 셀러 1위를 지켜온 `자바의 정석`의 최신판. 저자가 카페에서 12년간 직접 독자들에게 답변을 해오면서 초보자가 어려워하는 부분을 잘 파악하고 쓴 책. 뿐만 아

www.yes24.com

자바의 정석 저자 남궁성 님이 직접 올려주신 문제다

 


챕터6은 총 24문제로 구성되어 있다


6-21


 

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class MyTv {
    boolean isPowerOn;
    int channel;
    int volume;
    
    final int MAX_VOLUME = 100;
    final int MIN_VOLUME = 0;
    final int MAX_CHANNEL = 100;
    final int MIN_CHANNEL = 1;
    
    void turnOnOff() {    //(1) ★더 쉬운 방법 있음 isPowerOn != isPowerOn;
        if(isPowerOn)
            isPowerOn= false;
        else
            isPowerOn = true;
    }
    
    void volumeUp() {
        if(volume < MAX_VOLUME) {    //(2)
            volume++;
        }
    }
    
    void volumeDown() {        // (3)
        if(volume > MIN_VOLUME) {
            volume--;
        }
    }
    
    void channelUp() {        // (4)★문제 잘 보기★
        if (channel == MAX_CHANNEL) {
            channel = MIN_CHANNEL;
        }
        else {
            channel++;
        }
    }
    
    void channelDown() {    // (5)★문제 잘 보기★
        if (channel == MIN_CHANNEL) {
            channel = MAX_CHANNEL;
        }
        else {
        channel--;
        }
    }
}
 
class Excercise6_21 {
    
    public static void main(String[] args) {
        MyTv t = new MyTv();
        
        t.channel = 100;
        t.volume = 0;
        System.out.println("CH: "+t.channel+", VOL:"+t.volume);
        
        t.channelDown();
        t.volumeDown();
        System.out.println("CH: "+t.channel+", VOL:"+t.volume);
        
        t.volume = 100;
        t.channelUp();
        t.volumeUp();
        System.out.println("CH: "+t.channel+", VOL:"+t.volume);
    }
}
cs

 

꽤 긴 코드긴 하지만 어려울 건 없다

(1)번의 경우 isPowerOn != isPowerOn;으로 아주 짧게 표현할 수 있다

 

 

 


 

반응형
Comments