1-17. 객체지향 퀴즈

백종석·2022년 5월 19일
0

자바 문법 뽀개기

목록 보기
17/17
post-thumbnail

퀴즈 : 객체지향

객체지향에서 배운 개념과 문법을 이용해서 다음 요구조건을 만족하는 클래스를 작성하시요. 여러분이 게임을 만든다고 생각해보세요.

요구사항

  1. 사람은 자식, 부모님, 조부모님이 있다.
  2. 모든 사람은 이름, 나이, 현재 장소정보(x,y좌표)가 있다.
  3. 모든 사람은 걸을 수 있다. 장소(x, y좌표)로 이동한다.
  4. 자식과 부모님은 뛸 수 있다. 장소(x, y좌표)로 이동한다.
  5. 조부모님의 기본속도는 1이다. 부모의 기본속도는 3, 자식의 기본속도는 5이다.
  6. 뛸때는 속도가 기본속도대비 +2 빠르다.
  7. 수영할때는 속도가 기본속도대비 +1 빠르다.
  8. 자식만 수영을 할 수 있다. 장소(x, y좌표)로 이동한다.

위 요구사항을 만족하는 클래스들을 바탕으로, Main 함수를 다음 동작을 출력(System.out.println)하며 실행하도록 작성하시오. 이동하는 동작은 각자 순서가 맞아야 합니다.

  1. 모든 종류의 사람의 인스턴스는 1개씩 생성한다.
  2. 모든 사람의 처음 위치는 x,y 좌표가 (0,0)이다.
  3. 모든 사람의 이름, 나이, 속도, 현재위치를 확인한다.
  4. 걸을 수 있는 모든 사람이 (1, 1) 위치로 걷는다.
  5. 뛸 수 있는 모든 사람은 (2,2) 위치로 뛰어간다.
  6. 수영할 수 있는 모든 사람은 (3, -1)위치로 수영해서 간다.














































퀴즈: 객체지향 - 정답 예시

Human.java

  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.");
      }
  }

Walkable.java

  public interface Walkable {
      void walk(int x, int y);
  }

Runnable.java

  public interface Runnable {
      void run(int x, int y);
  }

Swimmable.java

  public interface Swimmable {
      void swim(int x, int y);
  }

GrandParent.java

  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());
      }
  }

Parent.java

  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());
      }
  }

Child.java

  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());
      }
  }

Main.java

  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(" - - - - - - ");
              }
          }
      }
  }

💡 구현하는 방법은 여러가지 입니다. 꼭 예시가 정답은 아닙니다. 다만, 여러분이 짠 코드와 비교해보면서 어떤 코드가 어떤 점에서 더 좋은지 생각해보세요.

profile
항해중인 우당탕탕 코린이

0개의 댓글