SW공부 27일차

Guryena·2023년 2월 3일
0

CS

목록 보기
20/34

1. 다음 TV 클래스가 있다.

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

public static void main(String[] args) {
   ColorTV myTV = new ColorTV(32, 1024);
   myTV.printProperty();
}
//결과 32인치 1024컬러
public class TV {

	private int size;

	public TV(int size) {
		this.size = size;
	}

	protected int getSize() {
		return size;
	}

}
public class ColorTV extends TV {
	private int color;

	public ColorTV(int inch, int color) {
		super(inch);
		this.color = color;

	}

	public void printProperty() {
		System.out.println(this.getSize() + "인치 " + this.color + "컬러");
	}

}

2. 자바 컴파일러가 공짜로 해주는 거 2가지를 정리하시오.

  • 상속의 경우, 부모클래스를 칭하는 "this."와 부모클래스의 생성자를 칭하는 "show();"가 생략되어있다.

3. B b = new B(); 했을시 컴파일러가 넣는 코드를 완성하시오.

class A{
	public A(){
    
    }
}
class B extends A{
	public B(){
    show();
    }
}

4. 상속을 UML로 표기하시오.

5. 아래의 업다운 게임을 완성하시오.

게임시작 1
게임종료 2
1
숫자를 입력해주세요 : 50
Down ===> 9번 남아 있습니다.
숫자를 입력해주세요 : 30
Down ===> 8번 남아 있습니다.
숫자를 입력해주세요 : 15
Down ===> 7번 남아 있습니다.
숫자를 입력해주세요 : 8
Up ====> 6번 남아 있습니다.
숫자를 입력해주세요 : 9
Up ====> 5번 남아 있습니다.
숫자를 입력해주세요 : 10
Up ====> 4번 남아 있습니다.
숫자를 입력해주세요 : 11
Up ====> 3번 남아 있습니다.
숫자를 입력해주세요 : 12
Up ====> 2번 남아 있습니다.
숫자를 입력해주세요 : 13
Up ====> 1번 남아 있습니다.
숫자를 입력해주세요 : 14
일치
게임시작 1
게임종료 2

public class Game extends RandomNumber {
	private int num;
	private String x;
	public Game(int num) {
		this.num = num;
//		matching();
	}

	String matching() {
		if (num == this.value) {
//			System.out.println("일치");
			x = "일치";
		} else if (num > this.value) {
//			System.out.println("Down");
			x = "Down";
		} else if(num < this.value) {
//			System.out.println("Up");
			x = "Up";
		}return x;
	}

}
import java.util.Random;

public class RandomNumber {
	int value;
	public RandomNumber() {
		Random random = new Random();
		value = (int) (random.nextInt(100) + 1);
	}
}
import java.util.Scanner;

public class UpDownGame {
	public static void main(String[] args) {
		int count = 10;

		Scanner sc = new Scanner(System.in);
		System.out.println("1 게임 시작" + "\t" + "2 게임 종료");
		int play = sc.nextInt();
		if (play == 1) {
			while (count > 0) {
				int num = sc.nextInt();
				Game game = new Game(num);
				System.out.println(game.matching());
				count--;
				System.out.println("남은 횟수 = "+count);
			}
		} else if (play == 2) {
			System.out.println("게임을 종료합니다");
			System.exit(0);
		}
	}
}

0개의 댓글