JAVA 프로그래밍

문제

점을 클래스로 표현하는 문제입니다 실행순서를 클릭하세요 
점의 x좌표 값을 입력하세요 : 3
점의 y좌표 값을 입력하세요 : 4
(0,0)과 (3,4) 사이의 거리는 5.0입니다 

프로그램 코드

	import java.util.Scanner;
	import java.lang.Math;

	class XYpoint
	{
		private int x, y;
		
P1b		public XYpoint( int x, int y ) {
			this.x = x;
			this.y = y;
P1e		}
		
P2b		public XYpoint() {
P21			this( 0, 0 );
P2e		}

P3b		public String toString() {
			return "(" + this.x + "," + this.y + ")";
P3e		}

P4b		public boolean equals( XYpoint that ) {
			return ( this.x == that.x ) && ( this.y == that.y );
P4e		}

P5b		public double compareTo( XYpoint that ) {
			return Math.sqrt( Math.pow( ( this.x - that.x ), 2 ) + Math.pow( ( this.y - that.y ), 2 ) );
P5e		}
	}
		
	public class XYpoints
	{
1		public static void main( String[] args ) {
			Scanner scan = new Scanner( System.in );
			
			System.out.print( "점의 x좌표 값을 입력하세요 : " );
			int x = scan.nextInt();
			System.out.print( "점의 y좌표 값을 입력하세요 : " );
			int y = scan.nextInt();

2			XYpoint origin =
3			               new XYpoint();
4			XYpoint point =
5			               new XYpoint( x, y );

6			if ( origin.equals( point ) ) {
7				System.out.println( "이 점은 원점입니다" );
			}
8			else {
				System.out.println(
9				                    origin + "과 "
10				                     + point + " 사이의 거리는 "
11				                     + point.compareTo( origin ) + "입니다" );
			}
			
			scan.close();
12		}
	}








 
실행 순서