요구사항
1. 사람은 자식, 부모님, 조부모님이 있다.
2. 모든 사람은 이름, 나이, 현재 위치 정보(x, y 좌표)가 있다.
3. 모든 사람은 걸을 수 있다. 특정 위치(x, y 좌표)로 이동한다.
4. 자식과 부모님은 달랄 수 있다. 특정 위치(x, y 좌표)로 이동한다.
5. 조부모님의 기본속도는 1이다. 부모님의 기본속도는 3, 자식의 기본속도는 5이다.
6. 달릴 때의 속도는 기본속도 대비 +2 만큼 빠르다.
7.수영할 때의 속도는 기본속도 대비 +1만큼 빠르다.
8. 자식만 수영을 할 수 있다. 특정 위치(x, y 좌표)로 이동한다.
main 함수 동작 조건
1. 모든 종류의 사람 인스턴스는 각각 1개씩 생성한다.
2. 모든 사람의 처음 위치는 x=0, y=0 이다.
3. 모든 사람의 이름, 나이, 속도, 현재 위치를 확인한다.
4. 걸을 수 있는 모든 사람은 (1, 1) 위치로 이동한다.
5. 달릴 수 있는 모든 사람은 (2, 2) 위치로 이동한다.
6. 수영할 수 있는 모든 사람은 (3, -1) 위치로 이동한다.
Human.java - 사람(Human) 클래스 정의
public class Human {
String name;
int age;
int speed;
int x, y;
public Human(String name, int age, int speed, int x, int y) {
this.name = name;
this.age = age;
this.speed = speed;
this.x = x;
this.y = y;
}
public Human(String name, int age, int speed) {
this(name, age, speed, 0, 0);
}
public String getLocation() {
return "(" + x + ", " + y + ")";
}
protected void printWhoAmI () {
System.out.println("My name is " + name + ". " + age + " years old.");
}
}
Child.java - 사람(Human) 클래스를 상속받은 자식(Child) 클래스 정의
[요구사항에 따라 조부모(GrandParent), 부모(Parent) 클래스는 각각 달리기와 수영 메소드를 구현하지 않는다.]
public class Child extends Human implements Walkable, Runnable, Swimmable {
public Child(String name, int age) {
super(name, age, 5);
}
@Override
public void run(int x, int y) {
printWhoAmI();
System.out.println("run speed: " + (speed + 2));
this.x = x;
this.y = y;
System.out.println("Ran to " + getLocation());
}
@Override
public void swim(int x, int y) {
printWhoAmI();
System.out.println("swim speed: " + (speed + 1));
this.x = x;
this.y = y;
System.out.println("Swam to " + getLocation());
}
@Override
public void walk(int x, int y) {
printWhoAmI();
System.out.println("walk speed: " + speed);
this.x = x;
this.y = y;
System.out.println("Walked to " + getLocation());
}
}
Walkable.java - 걷기(Walkable) 인터페이스 정의
[달리기(Runnable), 수영(Swimmable) 인터페이스도 동일하게 정의한다.]
public interface Walkable {
void walk(int x, int y);
}
Main.java
public class Main {
public static void main(String[] args) {
Human grandParent = new GrandParent("grand-father", 70);
Human parent = new Parent("mother", 50);
Human child = new Child("me", 20);
Human[] humans = {grandParent, parent, child};
for (Human human : humans) {
System.out.println(human.name + ", age: " + human.age + ", speed: " + human.speed + ", current position: " + human.getLocation());
}
System.out.println("<< PROGRAM START >>");
for (Human human : humans) {
if (human instanceof Walkable) {
((Walkable) human).walk(1, 1);
System.out.println("- - - - - - - - - -");
}
if (human instanceof Runnable) {
((Runnable) human).run(2, 2);
System.out.println("- - - - - - - - - -");
}
if (human instanceof Swimmable) {
((Swimmable) human).swim(3, -1);
System.out.println("- - - - - - - - - -");
}
}
}
}
실행 결과
grand-father, age: 70, speed: 1, current position: (0, 0)
mother, age: 50, speed: 3, current position: (0, 0)
me, age: 20, speed: 5, current position: (0, 0)
<< PROGRAM START >>
My name is grand-father. 70 years old.
walk speed: 1
Walked to (1, 1)
- - - - - - - - - -
My name is mother. 50 years old.
walk speed: 3
Walked to (1, 1)
- - - - - - - - - -
My name is mother. 50 years old.
run speed: 5
Ran to (2, 2)
- - - - - - - - - -
My name is me. 20 years old.
walk speed: 5
Walked to (1, 1)
- - - - - - - - - -
My name is me. 20 years old.
run speed: 7
Ran to (2, 2)
- - - - - - - - - -
My name is me. 20 years old.
swim speed: 6
Swam to (3, -1)
- - - - - - - - - -
'Java' 카테고리의 다른 글
자바 - 2차원 배열 정렬 (오름차순, 내림차순, 다중 조건) (0) | 2023.03.15 |
---|---|
자바 - 날짜와 시간 (0) | 2023.02.20 |
자바 - 인터페이스 (0) | 2023.02.20 |
자바 - 추상 클래스 (0) | 2023.02.20 |
자바 - 접근 제어자 (0) | 2023.02.20 |
댓글