9월 16일 기초 자바 tv 만들기 샘플 선생님 코드

 package e_oop;


public class TV {

/*

* 과제. 임의의 사물을 대상으로 클래스를 만들어주세요.

* 사물이 가지고 있는 속성을 변수로 만들고,

* 변수와 관계된 동작들을 메서드로 만들어주시면 됩니다.

*/

boolean power; //전원

int channel; //채널

int volume; //음량

final int MIN_CHANNEL = 1;

final int MAX_CHANNEL = 100;

final int MIN_VOLUME = 0;

final int MAX_VOLUME = 10;

TV(){

power = false;

channel = 1;

volume = 5;

}

void power(){

power = !power;

System.out.println(power ? "TV 켜짐" : "TV 꺼짐");

}

void changeChannel(int channel){

if(power){

if(MIN_CHANNEL <= channel && channel <= MAX_CHANNEL){

this.channel = channel;

}

System.out.println("채널. " + this.channel);

}

}

void channelUp(){

changeChannel(channel + 1);

}

void channelDown(){

changeChannel(channel - 1);

}

void volumeUp(){

if(power){

if(volume < MAX_VOLUME){

volume++;

}

showVolume();

}

}

void volumeDown(){

if(power){

if(MIN_VOLUME < volume){

volume--;

}

showVolume();

}

}

void showVolume(){

System.out.print("음량. ");

for(int i = MIN_VOLUME + 1; i <= MAX_VOLUME; i++){

if(i <= volume){

System.out.print("■");

}else{

System.out.print("□");

}

}

System.out.println();

}

public static void main(String[] args) {

TV tv = new TV();

while(true){

System.out.println("-------------------------------");

System.out.println("1.전원  2.채널변경  3.채널업  4.채널다운");

System.out.println("5.볼륨업  6.볼륨다운  0.종료");

System.out.println("-------------------------------");

System.out.print("번호 입력>");

int input = ScanUtil.nextInt();

switch (input) {

case 1: tv.power(); break;

case 2:

System.out.print("변경할 채널 입력>");

int ch = ScanUtil.nextInt();

tv.changeChannel(ch);

break;

case 3: tv.channelUp(); break;

case 4: tv.channelDown(); break;

case 5: tv.volumeUp(); break;

case 6: tv.volumeDown(); break;

default:

System.out.println("종료되었습니다.");

System.exit(0);

break;

}

}

}

}


댓글