Notice
Recent Posts
Recent Comments
Link
250x250
develope_kkyu
[JAVA] 두 점 사이의 거리 구하기 본문
728x90
MyPoint.java
public class MyPoint {
public int x;
public int y;
MyPoint(int x, int y){
this.x = x;
this.y = y;
}
public double getDistance(int i, int j) {
double dis;
dis = Math.sqrt((this.x-i)*(this.x-i) + (this.y-j)*(this.y-j));
return dis;
}
}
MyPointExam.java
public class MyPointExam {
public static void main(String[] args) {
// System.out.println(Math.sqrt(4)); // 2.0*2.0 == 4
// System.out.println(Math.sqrt(9)); // 3.0*3.0 ==9
MyPoint p = new MyPoint(1,1);
double dis = p.getDistance(1, 5);
System.out.printf("두 점 사이의 거리는 = %4.0f\n", dis);
}
}
--
두 점 사이의 거리는 = 4
728x90
'JAVA > Java SE' 카테고리의 다른 글
[JAVA] 증감 연산자 (0) | 2022.12.21 |
---|---|
[JAVA] 카페 메뉴 작업 (0) | 2022.12.21 |
[JAVA] 최대, 최소, 합계 구하기(함수 이용) (0) | 2022.12.21 |
[JAVA] 홀수단 출력 (1) | 2022.12.21 |
[JAVA] 구구단 출력 (1) | 2022.12.21 |