다음 TV 클래스가 있다. class TV{ private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } **[1번] 다음 main() 메소드와 실행 결과를 참고하여 TV를 상속받은 ColorTV 클래스를 작성하라** public static void main(String[] args) { ColorTV myTV = new ColorTV(32, 1024); myTV.printProperty(); }
- 출력결과
32인치 1024컬러
package inhertest;
class TV {
private int size;
public TV(int size) {
this.size = size;
}
protected int getSize() {
return size;
}
}
class ColorTV extends TV {
private int color;
public ColorTV(int size, int color) {
super(size);
this.color = color;
}
public void printProperty() {
System.out.println(super.getSize() + "인치 " + color + "컬러"); // 부모 클래스에서 size를 가져오기 위해 super.함수 활용
}
}
class SuperSubTest {
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
}
}
class A{
public A(){ //공짜로 만들어줌
}
}
class SuperCLS {
public SuperCLS () {
System.out.println("I'm Super Class");
}
}
class SubCLS extends SuperCLS {
public SubCLS (){
super(); // 코드를 쓰지 않아도 자동으로 생성
}
public SubCLS () {
System.out.println("I'm Sub Class");
}
}
class SuperSubCon {
public static void main(String[] args) {
new SubCLS ();
}
}
class A {
// public A() { // 1) 코드를 넣지 않아도 컴파일러가 자동으로 생성하는 디폴트 생성자
//
// }
}
class B extends A {
// public B() { // 1) 코드를 넣지 않아도 컴파일러가 자동으로 생성하는 디폴트 생성자
// super(); // 2) 코드를 넣지 않아도 컴파일러가 자동으로 생성하는 super 함수
// }
}
class SuperSubCon {
public static void main(String[] args) {
A a = new A();
B b = new B();
System.out.println(b);
}
}
class Man {
. . .
}
class BusinessMan extends Man {
. . .
}
게임시작 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 >>
package inhertest;
import java.util.Scanner;
class GameOper {
private int cnum;
public int setNum() {
cnum = (int) (Math.random() * 100) + 1;
return cnum;
}
}
public class UpDownGame2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
GameOper go = new GameOper();
while (true) {
System.out.print("게임시작 : 1 ");
System.out.println("게임종료 : 2 ");
int input = sc.nextInt();
int cnum = go.setNum();
// System.out.println(cnum);
if (input == 1) {
for (int i = 1; i <= 10; i++) {
System.out.println("숫자를 입력해주세요 : ");
int num = sc.nextInt();
if (cnum == num) {
System.out.println("일치");
break;
} else if (cnum < num) {
System.out.println("Down");
} else {
System.out.println("Up");
}
System.out.println(10 - i + "번 남았습니다.");
if (i == 10 && cnum != num) {
System.out.println("정답: " + cnum);
}
}
} else {
System.out.println("종료");
break;
}
}
}
}