2022.11.18 JAVA 17일차 객체지향 응용문제(상수)
2022. 11. 22. 14:37ㆍJAVA
상수 : 변수 데이터타입 왼쪽에 final키워드를 넣으면 상수변수로 지정, 값을 변경할수 없다
상수의 작명법은 전체 대문자
예) final int ONE = 1;
문제1)
/*
* 부모클래스는 Animal 클래스에 맴버변수를 이름, 이동수단, 울음소리, 다리수로 정의
* 각 맴버변수를 제어하는 메서드도 포함.
* Animal 클래스를 상속받은 아래 클래스를 만들어서 각각의 이름, 이동수단, 울음소리, 다리수 를 출력하는 프로그램
* 클래스이름 Dog - 강아지
* Eagle - 독수리
* Lion - 사자
* Cat - 고양이
*/
class Animal
{
String name; //이름
String move; //이동수단
String cry; //울음소리
int leg; //다리수
Animal()
{
this(" ", " ", " ", 0);
}
Animal(String name, String move, String cry, int leg)
{
this.name = name;
this.move = move;
this.cry = cry;
this.leg = leg;
}
String getName()
{
return name;
}
void setName(String name)
{
this.name = name;
}
String getMove()
{
return move;
}
void setMove(String move)
{
this.move = move;
}
String getCry()
{
return cry;
}
void setCry(String cry)
{
this.cry = cry;
}
int getLeg()
{
return leg;
}
void setLeg(int leg)
{
this.leg = leg;
}
}
class Dog extends Animal
{
Dog()
{
this(" ", " ", " ", 0);
}
Dog(String name, String move, String cry, int leg)
{
super(name,move,cry,leg);
}
void cute()
{
System.out.println("커엽다");
}
void print(Dog d)
{
System.out.printf("동물 이름 : %s, 이동수단 : %s, 울음소리 : %s, 다리개수 : %d, ",d.getName(),d.getMove(),d.getCry(),d.getLeg());
d.cute();
}
}
class Eagle extends Animal
{
Eagle()
{
this(" ", " ", " ", 0);
}
Eagle(String name, String move, String cry, int leg)
{
super(name,move,cry,leg);
}
String bald()
{
return "너 대머리야";
}
void print(Eagle e)
{
System.out.printf("동물 이름 : %s, 이동수단 : %s, 울음소리 : %s, 다리개수 : %d, %s\n",e.getName(),e.getMove(),e.getCry(),e.getLeg(),e.bald());
}
}
class Lion extends Animal
{
Lion()
{
this(" ", " ", " ", 0);
}
Lion(String name, String move, String cry, int leg)
{
super(name,move,cry,leg);
}
String king()
{
return "육상동물의 왕";
}
void print(Lion l)
{
System.out.printf("동물 이름 : %s, 이동수단 : %s, 울음소리 : %s, 다리개수 : %d, %s\n",l.getName(),l.getMove(),l.getCry(),l.getLeg(),l.king());
}
}
class Cat extends Animal
{
Cat()
{
this(" ", " ", " ", 0);
}
Cat(String name, String move, String cry, int leg)
{
super(name,move,cry,leg);
}
void c()
{
System.out.println("고양이는 말 안드뤄");
}
void print(Cat c)
{
System.out.printf("동물 이름 : %s, 이동수단 : %s, 울음소리 : %s, 다리개수 : %d, ",c.getName(),c.getMove(),c.getCry(),c.getLeg());
c.c();
}
}
public class MySample1118 {
public static void main(String[] args) {
Dog d = new Dog("개" , "땅", "멍멍", 4);
Eagle e = new Eagle("독수리", "하늘", "몰라", 2);
Lion l = new Lion("사자", "땅", "어흥", 4);
Cat c = new Cat("고양이", "땅", "야옹", 4);
d.print(d);
e.print(e);
l.print(l);
c.print(c);
}
}
출력

문제2)
class Card2
{
static final int KIND_MAX = 4; //카드 무늬수
static final int NUM_MAX = 13; //무늬당 장수
static final int SPADE = 4;
static final int DIAMOND = 3;
static final int HEART = 2;
static final int CLOVER = 1;
int kind;
int number;
Card2()
{
this(SPADE,1);
}
Card2(int kind, int number)
{
this.kind = kind;
this.number = number;
}
public String toString()
{
String kinds[] = { "","CLOVER", "HEART", "DIAMOND", "SPADE"};
String numbers = "0123456789XJQK"; //X는 숫자10
return "kind : " + kinds[this.kind] + ", number : " + numbers.charAt(this.number);
}
}
class Deck
{
final int CARD_NUM = 52; //카드 전체 장수
Card2 cardArr[] = new Card2[CARD_NUM]; //카드 저장 배열
Deck()
{
int i = 0, k, n;
for(k = Card2.KIND_MAX; k >0; k--)
{
for(n = 0; n < Card2.NUM_MAX; n++)
{
cardArr[i++] = new Card2(k,n+1);
}
}
}
//지정된 위치(index) 에 있는 카드 하나를 꺼내 반환
Card2 pick(int index)
{
return cardArr[index];
}
//Deck에서 카드하나를 선택
Card2 pick()
{
int index = (int)(Math.random()*CARD_NUM);
return pick(index);
}
//카드순서 섞기
void shuffle()
{
int i,r;
Card2 tmp;
for(i = 0; i < cardArr.length; i++)
{
r = (int)(Math.random()*CARD_NUM);
tmp = cardArr[r];
cardArr[r] = cardArr[i];
cardArr[i] = tmp;
}
}
}
public class MySample1118_2 {
public static void main(String[] args) {
Deck d = new Deck();
Card2 c = d.pick(0); //섞기전 제일 위 카드 뽑기
System.out.println(c.toString());//뽑은카드 출력
d.shuffle();//카드섞기
c = d.pick();//카드 선택
System.out.println(c.toString());//선택한 카드 출력
}
}
출력

'JAVA' 카테고리의 다른 글
| 2022.11.22 JAVA 19일차 다형성 (0) | 2022.11.22 |
|---|---|
| 2022.11.21 JAVA 18일차 오버라이딩, 접근제어자 (0) | 2022.11.22 |
| 2022.11.17 JAVA 16일차 생성자2, 상속 (0) | 2022.11.22 |
| 2022.11.16 JAVA 15일차 생성자 (0) | 2022.11.22 |
| JAVA 응용문제 이중배열 정렬 (0) | 2022.11.22 |