JAVA를 잡아라!...
JAVA_인터페이스_#1_TV (+ 싱글톤 패턴) 본문
문제
SamsungTV
String userName; // 사용자명
int channel; // 현재 채널 위치
void channelUp(); // channel++;
void channelDown(); // channel--;
void channelRandom(); // 1~999로 채널 랜덤 변경됨(현재 위치 제외)
void printInfo(); // ㅁㅁ님의 현재 시청 채널은 ㅇㅇ입니다.
SamsungMiniTV
int battery;
void channelRandom(); // 1~999로 채널 랜덤 변경됨(현재 위치 제외)
//단, 배터리 10씩 소모
//0이하가 되면 채널 랜덤 변경 불가능
LgTV
boolean power;
int channel;
void channelUp(); // ON 상태일때 : channel++;
void channelDown(); // ON 상태일때 : channel--;
void printInfo(); // ON 상태일때 : 현재 시청 채널은 ㅇㅇ입니다.
// OFF 상태일때 : 전원이 꺼져있습니다...
main()에서
// 처음 채널 위치는 1로 고정
SamsungTV tv1=new SamsungTV("홍길동");
// 처음 채널 위치는 1로 고정. 처음 배터리는 50~70 랜덤 설정됨
SamsungMiniTV tv2=new SamsungMiniTV("홍길동");
// 처음 채널 위치는 1로 고정. 처음 전원은 OFF 상태를 구현해주세요.
LgTV tv3=new LgTV();
한글코딩_ver1
▶ 인터페이스
public interface TVimp {
void channelUp();
void channelDown();
void printInfo();
}
▶ 추상 클래스
abstract class TV emplements TVimp{
int channel;
TV() {
this.channel = 1;
}
@오버라이딩
public void channelup() {
if(this.channel < 999) {
this.channel ++;
}
else if(this.channel == 999) {
this.channel = 1;
}
}
@오버라이딩
public void channelDown() {
if(채널 > 1) {
this.channel --;
}
else if(this.channel == 1) {
this.channel = 999;
}
}
}
▶ 클래스
class SamsungTV extends TV {
String userName;
SamsungTV(String userName) {
super();
this.userName = userName;
}
@오버라이딩
void printInfo() {
syso(ㅇㅇ님의 현재 시청 채널은 ㅇㅇ입니다.)
}
void channelRandom() {
while(true) {
int channel = new Random().nextInt(999) + 1;
if(channel != this.channel) {
this.chennel = channel;
break;
}
}
}
}
class SamsungMiniTV extends SamsungTV {
int battery;
SamsungMiniTV(String userName) {
super(userName);
this.battery = new Random().nextInt(21) + 50;
}
@오버라이딩
void channelRandom() {
if(this.battery > 0) {
super.channelRandom();
this.battery -= 10;
if(this.battery < 0) {
this.battery = 0;
}
}
}
}
class LgTV extends TV {
boolean power;
LgTV() {
this.power = false;
}
void tvOn() {
this.power = true;
}
void tvOff() {
this.power = false;
}
@오버라이딩
void channelUp() {
if(this.power) {
super.channelUp();
}
}
@오버라이딩
void channelDown() {
if(this.power) {
super.channelDown();
}
}
@오버라이딩
void printInfo() {
if(this.power) {
syso(현재 시청 채널은 ㅇㅇ입니다.)
}
syso(전원이 꺼져있습니다.)
}
}
코드_ver1
TV는 추상클래스 불가..! --> 삼성과 LG는 하나가 될수없어.....
Interface
package practice01;
public interface TVimp {
void channelUp();
void channelDown();
void printInfo();
}
class
package practice01;
import java.util.Random;
abstract class TV implements TVimp {
int channel;
TV() {
this.channel = 1;
}
@Override
public void channelUp() {
if(this.channel < 999) {
this.channel ++;
}
else if(this.channel == 999) {
this.channel = 1;
}
}
@Override
public void channelDown() {
if(this.channel > 1) {
this.channel --;
}
else if(this.channel == 1) {
this.channel = 999;
}
}
}
class SamsungTV extends TV {
String userName;
SamsungTV(String userName) {
super();
this.userName = userName;
}
@Override
public void printInfo() {
System.out.println(this.userName + "님의 현재 시청 채널은 " + this.channel + "입니다.");
}
void channelRandom() {
while(true) {
int channel = new Random().nextInt(999) + 1;
if(channel != this.channel) {
this.channel = channel;
break;
}
}
}
}
class SamsungMiniTV extends SamsungTV {
int battery;
SamsungMiniTV(String userName) {
super(userName);
this.battery = new Random().nextInt(21) + 50;
}
@Override
void channelRandom() {
if(this.battery > 0) {
super.channelRandom();
this.battery -= 10;
if(this.battery < 0) {
this.battery = 0;
}
}
}
}
class LgTV extends TV {
boolean power;
LgTV() {
this.power = false;
}
void tvOn() {
this.power = true;
}
void tvOff() {
this.power = false;
}
@Override
public void channelUp() {
if(this.power) {
super.channelUp();
}
}
@Override
public void channelDown() {
if(this.power) {
super.channelDown();
}
}
@Override
public void printInfo() {
if(this.power) {
System.out.println("현재 시청 채널은 " + this.channel + "입니다.");
}
System.out.println("전원이 꺼져있습니다.");
}
}
public class Practice01_TV {
public static void main(String[] args) {
SamsungTV tv1 = new SamsungTV("홍길동1");
SamsungMiniTV tv2 = new SamsungMiniTV("홍길동2");
LgTV tv3 = new LgTV();
tv1.channelRandom();
tv1.printInfo();
tv1.channelDown();
tv1.printInfo();
tv1.channelUp();
tv1.printInfo();
tv2.channelRandom();
tv2.printInfo();
tv3.tvOn();
tv3.channelUp();
tv3.printInfo();
tv3.tvOff();
}
}
-------------------------------------------------------------------------------------
한글코딩_ver2
대문자 -> 클래스 Class
SamsungTV LgTV
SamsungMiniTV
TV라면 가져야하는 것들?
멤버변수(속성)
TV 최상위 클래스 channel
메소드 Samsung Lg
channelUp() O
channelDown() O
channelRandom() Mini
printInfo() O
TVimpl 인터페이스
channelUp()
channelDown()
printInfo()
코드_ver2
Interface
package practice01;
public interface TVimp02 {
public static final int CMIN = 1;
int CMAX = 999;
void channelUp();
void channelDown();
void printInfo();
}
class
package practice01;
import java.util.Random;
class SamsungTV2 implements TVimp02 {
String userName;
int channel;
static Random rand = new Random();
// 반복문 횟수 --> 메소드 호출 횟수 --> 객체 생성 횟수 --> 1번
// "싱글톤 패턴" 유지 ★
SamsungTV2(String userName) {
this.userName = userName;
this.channel = 1;
}
@Override
public void channelUp() {
this.channel ++;
if(this.channel > TVimp02.CMAX) {
this.channel = TVimp02.CMIN;
}
}
@Override
public void channelDown() {
this.channel --;
if(this.channel < TVimp02.CMIN) {
this.channel = TVimp02.CMAX;
}
}
public void channelRandom() {
int randNum; //scope 이슈 막기위해 while밖에 선언
while(true) {
randNum = SamsungTV2.rand.nextInt(TVimp02.CMAX - TVimp02.CMIN + 1) + TVimp02.CMIN;
if(this.channel != randNum) {
break;
}
}
this.channel = randNum;
//필드값은 if문 안에두지말고 밖으로 빼주자! 가독성 ↑
}
@Override
public void printInfo() {
System.out.println(this.userName + "님의 현재 시청 채널은 " + this.channel + "입니다.");
}
}
class SamsungMiniTV2 extends SamsungTV2 {
int battery;
final static int BM = 10;
public SamsungMiniTV2(String userName) {
super(userName);
this.battery = SamsungTV2.rand.nextInt(21) + 50;
}
@Override
public void channelRandom() {
if(this.battery < SamsungMiniTV2.BM) { // 하드코딩
System.out.println("로그: 배터리 부족으로 채널 랜덤 변경 불가능");
return;
}
super.channelRandom();
this.battery -= SamsungMiniTV2.BM;
}
}
class LgTV2 implements TVimp02 {
boolean power;
int channel;
LgTV2() {
this.power = false;
this.channel = 1;
}
void TVOn() {
this.power = true;
}
void TVOff() {
this.power = false;
}
@Override
public void channelUp() {
if(this.power) {
this.channel ++;
if(this.channel > TVimp02.CMAX) {
this.channel = TVimp02.CMIN;
}
}
else {
System.out.println("로그: 전원 off 상태");
}
}
@Override
public void channelDown() {
if(!this.power) {
System.out.println("로그: 전원 off 상태");
return;
}
this.channel --;
if(this.channel < TVimp02.CMIN) {
this.channel = TVimp02.CMAX;
}
}
@Override
public void printInfo() {
System.out.println("현재 시청 채널은 " + this.channel + "입니다.");
}
}
'JAVA > JAVA_복습문제' 카테고리의 다른 글
JAVA_예외처리(Exception)_#1 (0) | 2023.12.08 |
---|---|
JAVA_배열리스트_#1_Student (0) | 2023.12.07 |
JAVA_추상클래스_#2_Pokemon (2) | 2023.12.06 |
JAVA_추상클래스_#1_Shape (1) | 2023.12.06 |
JAVA_리턴타입 boolean인 메소드 연습_#1 (2) | 2023.12.05 |