문제
점을 클래스로 표현하는 문제입니다 이를 해결하는 프로그램의 다음 실행상태에 대해 빈칸을 채우세요
프로그램의 실행순서 및 실행상태
1 public static void main( String[] args ) {
점의 x좌표 값을 입력하세요 : 3
점의 y좌표 값을 입력하세요 : 4
3 new XYpoint();
| | main() | | | | | |
| x | 3 |
| y | 4 |
| origin | | | x | | y | | XYpoint(),toString(),equals(),compareTo() | (obj01) |
P2b public XYpoint() {
P21 this( 0, 0 );
P1b public XYpoint( int x, int y ) {
P1e }
P2e }
2 XYpoint origin =
5 new XYpoint( x, y );
| | main() | | | | | |
| x | 3 |
| y | 4 |
| origin | (obj01) | | x | 0 | y | 0 | XYpoint(),toString(),equals(),compareTo() | (obj01) |
| point | | | x | | y | | XYpoint(),toString(),equals(),compareTo() | (obj02) |
P1b public XYpoint( int x, int y ) {
P1e }
4 XYpoint point =
6 if ( origin.equals( point ) ) {
P4b public boolean equals( XYpoint that ) {
P4e }
8 else {
9 origin + "과 "
P3b public String toString() {
P3e }
(0,0)과
10 + point + " 사이의 거리는 "
P3b public String toString() {
P3e }
(3,4) 사이의 거리는
11 + point.compareTo( origin ) + "입니다" );
P5b public double compareTo( XYpoint that ) {
P5e }
5.0입니다
12 }
프로그램 코드
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 }
}