Backend/SpringBoot
자바 객체지향 퀴즈 문제풀이
Mo'Greene
2022. 6. 21. 01:06
- 퀴즈: 객체지향요구사항
- 사람은 자식, 부모님, 조부모님이 있다.
- 모든 사람은 이름, 나이, 현재 장소정보(x,y좌표)가 있다.
- 모든 사람은 걸을 수 있다. 장소(x, y좌표)로 이동한다.
- 자식과 부모님은 뛸 수 있다. 장소(x, y좌표)로 이동한다.
- 조부모님의 기본속도는 1이다. 부모의 기본속도는 3, 자식의 기본속도는 5이다.
- 뛸때는 속도가 기본속도대비 +2 빠르다.
- 수영할때는 속도가 기본속도대비 +1 빠르다.
- 자식만 수영을 할 수 있다. 장소(x, y좌표)로 이동한다.
- 모든 종류의 사람의 인스턴스는 1개씩 생성한다.
- 모든 사람의 처음 위치는 x,y 좌표가 (0,0)이다.
- 모든 사람의 이름, 나이, 속도, 현재위치를 확인한다.
- 걸을 수 있는 모든 사람이 (1, 1) 위치로 걷는다.
- 뛸 수 있는 모든 사람은 (2,2) 위치로 뛰어간다.
- 수영할 수 있는 모든 사람은 (3, -1)위치로 수영해서 간다.
Human 클래스
package sub17;
abstract class Human {
String name;
int age;
int x;
int y;
public Human(String name, int age, int x, int y) {
this.name = name;
this.age = age;
this.x = x;
this.y = y;
}
void printLocation(int x, int y){
System.out.println("현재 위치 " + x + ", " + y + " 입니다." );
}
}
abstract 으로 추상클래스로 만듬
인터페이스
public interface Walkable {
void walk(int x, int y);
}
public interface Swimmable {
void swim (int x, int y);
}
public interface Runnable {
void run(int x, int y);
}
Child 클래스
package sub17;
public class Child extends Human implements Walkable, Runnable, Swimmable{
public Child(String name, int age) {
super(name, age, 0,0);
}
@Override
public void walk(int x, int y) {
System.out.println("걸어서 이동합니다");
printLocation(x, y);
}
@Override
public void run(int x, int y) {
System.out.println("뛰어서 이동합니다");
printLocation(x, y);
}
@Override
public void swim(int x, int y) {
System.out.println("수영해서 이동합니다");
printLocation(x, y);
}
}
Parent 클래스
package sub17;
public class Parent extends Human implements Walkable, Runnable{
public Parent(String name, int age) {
super(name, age,0,0);
}
@Override
public void run(int x, int y) {
System.out.println("뛰어서 이동합니다");
printLocation(x, y);
}
@Override
public void walk(int x, int y) {
System.out.println("걸어서 이동합니다");
printLocation(x, y);
}
}
GrandParent 클래스
package sub17;
public class GrandParent extends Human implements Walkable{
public GrandParent(String name, int age) {
super(name, age,0,0);
}
@Override
public void walk(int x, int y) {
System.out.println("걸어서 이동합니다");
printLocation(x, y);
}
}
Main
package sub17;
public class Main {
public static void main(String[] args) {
Child child = new Child("철수", 12);
Parent parent = new Parent("빠덜", 43);
GrandParent grandParent = new GrandParent("그랜빠덜", 72);
System.out.println("나이 " + child.age + "살 인 " + child.name + " 가 좌표 "+ child.x + ", " + child.y + " 에서 이동 합니다.");
child.walk(1,1);
child.run(2,2);
child.swim(3,-1);
System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
System.out.println("나이 " + parent.age + "살 인 " + parent.name + " 가 좌표 "+ parent.x + ", " + parent.y + " 에서 이동 합니다.");
parent.walk(1,1);
parent.run(2,2);
System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
System.out.println("나이 " + grandParent.age + "살 인 " + grandParent.name + " 가 좌표 "+ grandParent.x + ", " + grandParent.y + " 에서 이동 합니다.");
grandParent.walk(1,1);
}
}
콘솔
나이 12살 인 철수 가 좌표 0, 0 에서 이동 합니다.
걸어서 이동합니다
현재 위치 1, 1 입니다.
뛰어서 이동합니다
현재 위치 2, 2 입니다.
수영해서 이동합니다
현재 위치 3, -1 입니다.
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
나이 43살 인 빠덜 가 좌표 0, 0 에서 이동 합니다.
걸어서 이동합니다
현재 위치 1, 1 입니다.
뛰어서 이동합니다
현재 위치 2, 2 입니다.
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
나이 72살 인 그랜빠덜 가 좌표 0, 0 에서 이동 합니다.
걸어서 이동합니다
현재 위치 1, 1 입니다.