원본 접은코드
더보기
abstract class Human {
private int x, y;
public String name;
public int age = 10;
public int speed = 1;
public double totalTime = 0.0;
boolean runnable;
boolean swimmable;
void walk(int x, int y) {
printLocation();
double time = (float) (Math.abs(this.x + x) + Math.abs(this.y + y)) / (speed + 2);
System.out.println(name + "이/가 뛰기 시작합니다.");
this.x = x;
this.y = y;
System.out.println(name + "이/가 도착했습니다.");
printLocation();
printTime(time);
System.out.println("====이동완료====");
totalTime += time;
}
void run(int x, int y) {
printLocation();
if (runnable) {
double time = (float) (Math.abs(this.x + x) + Math.abs(this.y + y)) / (speed + 2);
System.out.println(name + "이/가 뛰기 시작합니다.");
this.x = x;
this.y = y;
System.out.println(name + "이/가 도착했습니다.");
printLocation();
printTime(time);
System.out.println("====이동완료====");
totalTime += time;
}
else {
System.out.println("뛸 수 없습니다. 움직이지 않습니다.");
printLocation();
System.out.println("====이동완료====");
}
}
void swim(int x, int y) {
printLocation();
if (swimmable) {
double time = (float) (Math.abs(this.x + x) + Math.abs(this.y + y)) / (speed + 1);
System.out.println(name + "이/가 수영하기 시작합니다.");
this.x = x;
this.y = y;
System.out.println(name + "이/가 도착했습니다.");
printLocation();
printTime(time);
System.out.println("====이동완료====");
totalTime += time;
}
else {
System.out.println("수영이 불가능합니다. 움직이지 않습니다.");
printLocation();
System.out.println("====이동완료====");
}
}
public void printLocation(){
System.out.println(name + "의 현위치 : ( x좌표 :" + x + ", y좌표 :" + y + ")");
}
public void printTime(double time){
System.out.println(name + "의 걸린시간 : " + time + "시간 걸렸습니다.");
}
public void printAll(){
System.out.println("(이름 : " + name + ", 나이 : " + age + ", 위치 : " + x + ", " + y + ", 이동시간 : " + totalTime + ")");
}
}
class Child extends Human {
public Child() {
name = "자식";
speed = 5;
age = 15;
swimmable = true;
runnable = true;
}
// public String name = "자식";
// public int speed = 5;
// public int age = 15;
}
class Parent extends Human {
public Parent() {
name = "부모";
speed = 3;
age = 35;
runnable = true;
swimmable = false;
}
// @Override
// boolean runnable() {
// return true;
// }
// @Override
// boolean swimmable() {
// return false;
// }
// public String name = "부모";
// public int speed = 3;
// public int age = 35;
}
class GrandParent extends Human {
public GrandParent() {
name = "조부모";
speed = 1;
age = 85;
runnable = false;
swimmable = false;
}
// @Override
// boolean runnable() {
// return false;
// }
// @Override
// boolean swimmable() {
// return false;
// }
// public String name = "조부모";
// public int speed = 1;
// public int age = 85;
}
//public class Main {
// public static void main(String[] args) {
// Human child = new Child();
// Human parent = new Parent();
// Human grandParent = new GrandParent();
//
// child.printAll();
// parent.printAll();
// grandParent.printAll();
//
//
//
// }
//}
public class Main {
public static void main(String[] args) {
Human child = new Child();
Human parent = new Parent();
Human grandParent = new GrandParent();
child.printAll();
parent.printAll();
grandParent.printAll();
System.out.println("-------");
child.walk(1,1);
parent.walk(1,1);
grandParent.walk(1,1);
System.out.println("-------");
child.run(2,2);
parent.run(2,2);
grandParent.run(2,2);
System.out.println("-------");
child.swim(3,-1);
parent.swim(3,-1);
grandParent.swim(3,-1);
System.out.println("-------");
child.printAll();
parent.printAll();
grandParent.printAll();
System.out.println("-------");
}
}
주석 지운 원본 코드
abstract class Human {
private int x, y;
public String name;
public int age = 10;
public int speed = 1;
public double totalTime = 0.0;
boolean runnable;
boolean swimmable;
void walk(int x, int y) {
printLocation();
double time = (float) (Math.abs(this.x + x) + Math.abs(this.y + y)) / (speed + 2);
System.out.println(name + "이/가 뛰기 시작합니다.");
this.x = x;
this.y = y;
System.out.println(name + "이/가 도착했습니다.");
printLocation();
printTime(time);
System.out.println("====이동완료====");
totalTime += time;
}
void run(int x, int y) {
printLocation();
if (runnable) {
double time = (float) (Math.abs(this.x + x) + Math.abs(this.y + y)) / (speed + 2);
System.out.println(name + "이/가 뛰기 시작합니다.");
this.x = x;
this.y = y;
System.out.println(name + "이/가 도착했습니다.");
printLocation();
printTime(time);
System.out.println("====이동완료====");
totalTime += time;
}
else {
System.out.println("뛸 수 없습니다. 움직이지 않습니다.");
printLocation();
System.out.println("====이동완료====");
}
}
void swim(int x, int y) {
printLocation();
if (swimmable) {
double time = (float) (Math.abs(this.x + x) + Math.abs(this.y + y)) / (speed + 1);
System.out.println(name + "이/가 수영하기 시작합니다.");
this.x = x;
this.y = y;
System.out.println(name + "이/가 도착했습니다.");
printLocation();
printTime(time);
System.out.println("====이동완료====");
totalTime += time;
}
else {
System.out.println("수영이 불가능합니다. 움직이지 않습니다.");
printLocation();
System.out.println("====이동완료====");
}
}
public void printLocation(){
System.out.println(name + "의 현위치 : ( x좌표 :" + x + ", y좌표 :" + y + ")");
}
public void printTime(double time){
System.out.println(name + "의 걸린시간 : " + time + "시간 걸렸습니다.");
}
public void printAll(){
System.out.println("(이름 : " + name + ", 나이 : " + age + ", 위치 : " + x + ", " + y + ", 이동시간 : " + totalTime + ")");
}
}
class Child extends Human {
public Child() {
name = "자식";
speed = 5;
age = 15;
swimmable = true;
runnable = true;
}
}
class Parent extends Human {
public Parent() {
name = "부모";
speed = 3;
age = 35;
runnable = true;
swimmable = false;
}
}
class GrandParent extends Human {
public GrandParent() {
name = "조부모";
speed = 1;
age = 85;
runnable = false;
swimmable = false;
}
}
public class Main {
public static void main(String[] args) {
Human child = new Child();
Human parent = new Parent();
Human grandParent = new GrandParent();
child.printAll();
parent.printAll();
grandParent.printAll();
System.out.println("-------");
child.walk(1,1);
parent.walk(1,1);
grandParent.walk(1,1);
System.out.println("-------");
child.run(2,2);
parent.run(2,2);
grandParent.run(2,2);
System.out.println("-------");
child.swim(3,-1);
parent.swim(3,-1);
grandParent.swim(3,-1);
System.out.println("-------");
child.printAll();
parent.printAll();
grandParent.printAll();
System.out.println("-------");
}
}
(이름 : 자식, 나이 : 15, 위치 : 0, 0, 이동시간 : 0.0)
(이름 : 부모, 나이 : 35, 위치 : 0, 0, 이동시간 : 0.0)
(이름 : 조부모, 나이 : 85, 위치 : 0, 0, 이동시간 : 0.0)
-------
자식의 현위치 : ( x좌표 :0, y좌표 :0)
자식이/가 뛰기 시작합니다.
자식이/가 도착했습니다.
자식의 현위치 : ( x좌표 :1, y좌표 :1)
자식의 걸린시간 : 0.2857142984867096시간 걸렸습니다.
====이동완료====
부모의 현위치 : ( x좌표 :0, y좌표 :0)
부모이/가 뛰기 시작합니다.
부모이/가 도착했습니다.
부모의 현위치 : ( x좌표 :1, y좌표 :1)
부모의 걸린시간 : 0.4000000059604645시간 걸렸습니다.
====이동완료====
조부모의 현위치 : ( x좌표 :0, y좌표 :0)
조부모이/가 뛰기 시작합니다.
조부모이/가 도착했습니다.
조부모의 현위치 : ( x좌표 :1, y좌표 :1)
조부모의 걸린시간 : 0.6666666865348816시간 걸렸습니다.
====이동완료====
-------
자식의 현위치 : ( x좌표 :1, y좌표 :1)
자식이/가 뛰기 시작합니다.
자식이/가 도착했습니다.
자식의 현위치 : ( x좌표 :2, y좌표 :2)
자식의 걸린시간 : 0.8571428656578064시간 걸렸습니다.
====이동완료====
부모의 현위치 : ( x좌표 :1, y좌표 :1)
부모이/가 뛰기 시작합니다.
부모이/가 도착했습니다.
부모의 현위치 : ( x좌표 :2, y좌표 :2)
부모의 걸린시간 : 1.2000000476837158시간 걸렸습니다.
====이동완료====
조부모의 현위치 : ( x좌표 :1, y좌표 :1)
뛸 수 없습니다. 움직이지 않습니다.
조부모의 현위치 : ( x좌표 :1, y좌표 :1)
====이동완료====
-------
자식의 현위치 : ( x좌표 :2, y좌표 :2)
자식이/가 수영하기 시작합니다.
자식이/가 도착했습니다.
자식의 현위치 : ( x좌표 :3, y좌표 :-1)
자식의 걸린시간 : 1.0시간 걸렸습니다.
====이동완료====
부모의 현위치 : ( x좌표 :2, y좌표 :2)
수영이 불가능합니다. 움직이지 않습니다.
부모의 현위치 : ( x좌표 :2, y좌표 :2)
====이동완료====
조부모의 현위치 : ( x좌표 :1, y좌표 :1)
수영이 불가능합니다. 움직이지 않습니다.
조부모의 현위치 : ( x좌표 :1, y좌표 :1)
====이동완료====
-------
(이름 : 자식, 나이 : 15, 위치 : 3, -1, 이동시간 : 2.142857164144516)
(이름 : 부모, 나이 : 35, 위치 : 2, 2, 이동시간 : 1.6000000536441803)
(이름 : 조부모, 나이 : 85, 위치 : 1, 1, 이동시간 : 0.6666666865348816)