원 a : Circle(2,3)반지름5
원 b : Circle(2,3)반지름30
같은 원
*두 객체의 중심이 같으면 같은 것으로 판별*
public class Circle {
private int x,y,radius;
public Circle(int x, int y, int radius) {
// TODO Auto-generated constructor stub
this.x = x;
this.y = y;
this.radius = radius;
}
public String toString() {
return "Circle("+x+","+y+")반지름"+radius;
}
public boolean equals(Object obj) {
Circle c = (Circle)obj;
if(x==c.x && y==c.y) return true;
else return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle a = new Circle(2,3,5);
Circle b = new Circle(2,3,30);
System.out.println("원 a : "+a);
System.out.println("원 b : "+b);
if(a.equals(b)) {
System.out.println("같은 원");
}
else {
System.out.println("서로 다른 원");
}
}
}
결과
'명품JAVA프로그래밍 > 6장 모듈과 패키지 개념' 카테고리의 다른 글
| [명품JAVA프로그래밍] 6장 실습문제 5번 (0) | 2022.01.23 |
|---|---|
| [명품JAVA프로그래밍] 6장 실습문제 4번 (0) | 2022.01.23 |
| [명품JAVA프로그래밍] 6장 실습문제 3번 (0) | 2022.01.23 |
| [명품JAVA프로그래밍] 6장 실습문제 1번 (0) | 2022.01.23 |
| [명품JAVA프로그래밍] 6장 Open Challenge (0) | 2022.01.23 |