Java/JAVA의 정석_문제풀이

[7-10~11]연습문제 - 메서드/제어자

Jenny_yoon 2022. 11. 6. 22:28
728x90
반응형

[7-10] MyTv2클래스의 멤버변수 isPowerOn, channel, volume을 클래스 외부에서 접근할 수 없도록 제어자를 붙이고 대신 이 멤버변수들의 값을 어디서나 읽고 변경할 수 있도록 getter와 setter메서드를 추가하라.


풀이

1) isPowerOn, channel, volume 앞에 제어자 private 붙여주기

class MyTv2{
	private boolean isPowerOn;
	private int channel;
	private int volume;

 

2) 호출 부분을 보면 알 수 있듯, 생성해야 하는 메서드는 총 4개.

setVolume, getVolume, setChannel, setChannel

2-1) setVolume() 메서드 생성 (volume의 조건 입력)

	public void setVolume(int volume){ //매개변수 int volume 사용 (void)
    		if(volume>100 || volume<10) //volume 조건 설정
            	   return;
    this.volume = volume;

2-2) getVolume() 메서드 생성

	public int getVolume() { //매개변수 사용x (voidx)
		return volume;
	}

2-3) setChannel() 메서드 생성

	public static void setChannel(int channel){ //매개변수 int channel 사용(void)
    		if(channel>100 || channel<1) //channel 조건설정
        	    return;
    this.channel = channel;

2-4) setChannel() 메서드 생성

	public int getChannel() { //매개변수 사용x (voidx)
		return channel;
	}

 

정답
package pkg1;

class MyTv2{
	private boolean isPowerOn;
	private int channel;
	private int volume;
	
	final int MAX_VOLUME = 100;
	final int MIN_VOLUME = 10;
	final int MAX_CHANNEL = 100;
	final int MIN_CHANNEL = 1;
	
	public void setVolume(int volume) {
		if(volume>100 || volume<10)
			return;
		this.volume = volume;
	}
	
	public int getVolume() {
		return volume;
	}
	
	public void setChannel(int channel) {
		if(channel>100 || channel<1)
			return;
		this.channel = channel;
	}
	public int getChannel() {
		return channel;
	}
}
public class Ex7_10 {
	public static void main(String[] args) {
		MyTv2 t = new MyTv2();
		
		t.setChannel(10);
		System.out.println("CH:"+t.getChannel());
		t.setVolume(20);
		System.out.println("VOL:"+t.getVolume());
	}
}

[7-11] 문제7-10에서 작성한 MyTv2클래스에 이전 채널(previous channel)로 이동하는 기능의 메서드를 추가해서 실행결과와 같은 결과를 얻도록 하시오.
[Hint] 이전 채널의 값을 저장할 멤버변수를 정의하라.


풀이

1) 이전 채널의 값을 저장할 멤버변수 정의

	private int PrevChannel;

2) 현재 채널을 이전 채널 변수에 저장

public void setChannel(int channel) {
		if(channel>100 || channel<1)
			return;
		PrevChannel = this.channel; //이부분만 추가
		this.channel = channel;

3) gotoPrevChannel() 메서드 생성 & setChannel(PrevChannel) 호출

public void gotoPrevChannel() {
	setChannel(PrevChannel);
}

 

정답
package pkg1;

class MyTv3{
	private boolean isPowerOn;
	private int channel;
	private int volume;
	private int PrevChannel; //추가

	final int MAX_VOLUME = 100;
	final int MIN_VOLUME = 10;
	final int MAX_CHANNEL = 100;
	final int MIN_CHANNEL = 1;
	
	public void setVolume(int volume) {
		if(volume>100 || volume<10)
			return;
		this.volume = volume;
	}
	
	public int getVolume() {
		return volume;
	}
	
	public void setChannel(int channel) {
		if(channel>100 || channel<1)
			return;
		PrevChannel = this.channel; //추가
		this.channel = channel;
	}
	public int getChannel() {
		return channel;
	}


	public void gotoPrevChannel() { //추가
		setChannel(PrevChannel);
	}
}

public class Ex7_11 {
	public static void main(String[] args) {
		MyTv3 t = new MyTv3();
		
		t.setChannel(10);
		System.out.println("CH:"+t.getChannel());
		t.setChannel(20);
		System.out.println("CH:"+t.getChannel());
		t.gotoPrevChannel();
		System.out.println("CH:"+t.getChannel());
		t.gotoPrevChannel();
		System.out.println("CH:"+t.getChannel());
	}
}
728x90
반응형