자바의정석 연습문제 풀이 6장 6-7
자바의 정석 3판 연습문제 풀이
연습문제 파일은 아래 링크에서 다운받을 수 있다
GitHub - castello/javajungsuk3: soure codes and ppt files of javajungsuk 3rd edition
soure codes and ppt files of javajungsuk 3rd edition - GitHub - castello/javajungsuk3: soure codes and ppt files of javajungsuk 3rd edition
github.com
http://www.yes24.com/Product/Goods/24259565
Java의 정석 - YES24
최근 7년동안 자바 분야의 베스트 셀러 1위를 지켜온 `자바의 정석`의 최신판. 저자가 카페에서 12년간 직접 독자들에게 답변을 해오면서 초보자가 어려워하는 부분을 잘 파악하고 쓴 책. 뿐만 아
www.yes24.com
자바의 정석 저자 남궁성 님이 직접 올려주신 문제다
챕터6은 총 24문제로 구성되어 있다
6-7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class MyPoint {
int x;
int y;
MyPoint (int x, int y) {
this.x = x;
this.y = y;
}
double getDistance(int x, int y) {
return Math.sqrt((this.x-x)*(this.x-x)+(this.y-y)*(this.y-y));
}
}
public class Practice_6_6_7 {
public static void main(String[] args) {
MyPoint p = new MyPoint(1, 1);
System.out.println(p.getDistance(2, 2));
}
}
|
cs |
6-6의 static 메서드, 즉 클래스 메서드는 iv인 this를 사용하지 못했지만,
지금 6-7 문제에서는 사용할 수 있다
iv를 사용할 수 있으므로 매개변수로 2개만 받으면 된다
getDistance 메서드 안 매개변수를 x, y로 두지 않고 x1, y1 등으로 두면
this 를 입력할 필요 없이
double getDistance(int x, int y) {
return Math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));
}
라고 쓸 수도 있다(iv 사용 가능하니까)