객체지향에서 배운 개념과 문법을 이용해서 다음 요구조건을 만족하는 클래스를 작성하시요. 여러분이 게임을 만든다고 생각해보세요.
요구사항
위 요구사항을 만족하는 클래스들을 바탕으로, Main 함수를 다음 동작을 출력(System.out.println
)하며 실행하도록 작성하시오. 이동하는 동작은 각자 순서가 맞아야 합니다.
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 + " aged.");
}
}
public interface Walkable {
void walk(int x, int y);
}
public interface Runnable {
void run(int x, int y);
}
public interface Swimmable {
void swim(int x, int y);
}
public class GrandParent extends Human implements Walkable {
public GrandParent(String name, int age) {
super(name, age, 1);
}
@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());
}
}
public class Parent extends Human implements Walkable, Runnable{
public Parent(String name, int age) {
super(name, age, 3);
}
@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 walk(int x, int y) {
printWhoAmI();
System.out.println("walk speed: " + speed);
this.x = x;
this.y = y;
System.out.println("Walked to " + getLocation());
}
}
public class Child extends Human implements Walkable, Runnable, Swimmable{
public Child(String name, int age) {
super(name, age, 5);
}
@Override
public void swim(int x, int y) {
printWhoAmI();
System.out.println("swimming speed: " + (speed + 1));
this.x = x;
this.y = y;
System.out.println("Swum to " + getLocation());
}
@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 walk(int x, int y) {
printWhoAmI();
System.out.println("walk speed: " + speed);
this.x = x;
this.y = y;
System.out.println("Walked to " + getLocation());
}
}
public class Main {
public static void main(String[] args) {
Human grandParent = new GrandParent("할아버지", 70);
Human parent = new Parent("엄마", 50);
Human child = new Child("나", 20);
Human[] humans = { grandParent, parent, child };
for (Human human : humans) {
System.out.println(human.name + ", 나이: " + human.age + ", 속도: " + human.speed + ", 장소: " + human
.getLocation());
}
System.out.println("<활동 시작>");
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(" - - - - - - ");
}
}
}
}
💡 구현하는 방법은 여러가지 입니다. 꼭 예시가 정답은 아닙니다. 다만, 여러분이 짠 코드와 비교해보면서 어떤 코드가 어떤 점에서 더 좋은지 생각해보세요.