문제) 모든 점 객체들을 저장할 수 있는 리스트에 임의로 10개의 점 객체들을 저장해주세요
점들은 좌표가 같다면 같은 점으로 판단합니다
[0]에 저장된 점과 같은 점은 몇개인지 출력해주세요
색을사용자에게 입력받아서 같은 색의 점이 몇개인지 출력해주세요
package practice;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class Point {
private int x;
private int y;
Point(){
this(0,0);
}
Point(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
@Override
public boolean equals(Object obj) {//같은 객체인지 확인(다를 때, 같을 떄)
if(!(obj instanceof Point)) {//만약 포인트클래스 있는 객체가 객체가 아니라면
return false;//반환x
}
Point point=(Point)obj; //포인트를 다운캐스팅해서 객체(상위)에서 포인트로 낮춰줌.
return x==point.getX()&& y==point.getY();//x는 get.x이고 y=get.y로 반환
}
}
class ColorPoint extends Point {
private String color;
ColorPoint(){
this(0,0,"검정");
}
ColorPoint(String color){
this(0,0,color);
}
ColorPoint(int x,int y){
this(x,y,"검정");
}
ColorPoint(int x,int y,String color){
super(x,y);
this.color=color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "ColorPoint [color=" + color + "]";
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof ColorPoint)) {//컬러포인트 클래스의 오브제가 객체가 아니라면
return false;//반환하지말고
}
ColorPoint colorpoint=(ColorPoint)obj; //다운캐스팅 시켜서 결과를 반환
return super.equals(colorpoint) && color.equals(colorpoint.getColor());
//부모클래스에서 받아온 인자와 컬러포인트와 다운캐스팅된 컬러를 비교해 반환
}
}
public class Test01 {
public static void main(String[] args) {
ArrayList<Point> al=new ArrayList<Point>();
al.add(new Point());
al.add(new Point(1,2));
al.add(new ColorPoint());
al.add(new ColorPoint(1,2));
al.add(new Point(3,3));
al.add(new Point(5,5));
al.add(new ColorPoint("분홍"));
al.add(new ColorPoint(1,2,"파랑"));
al.add(new ColorPoint(2,5,"분홍"));
al.add(new ColorPoint(3,7,"파랑"));
//한글코딩
//중복을 찾는 코드
//0번부터 크기만큼(범위가 있는) 반복문을 시행한다
//i인덱스가 0이면 j는 1부터 9까지 찾음
//i가 1이면 j는 2부터 9까지
//i가 2면 j는 3부터 9까지...
//i가 3이면 j는 4부터 9까지
//각 인덱스를 한 번씩 비교하면서
//i와 j가 같다면 하나만 출력
//i와 j를 비교하려면, 오버라이딩이 필요
//포인트 객체와 컬러 포인트 객체를 대등하게 비교
/*for (int i = 0; i < al.size(); i++) {
for (int j = i + 1; j < al.size(); j++) {
if (al.get(i).equals(al.get(j))) {
System.out.println("중복되는 점>> " + al.get(i));
}
}
}
*/
//중복된점은 HashSet을 사용해서 중복제거
//범위가 정해져 있는 포인트리스트를 반복해 만약 점이 중복되면
//하나만 출력하고 새로운 점을 찾았다면 point에 추가하고 출력.
Set<Point> SamePoint =new HashSet<>();
for (Point point : al) {
if(!SamePoint.contains(point)) {
SamePoint.add(point);
}
System.out.println("점은>>" +point);
}
//0에 저장된 점과 같은 점은 몇개??
//일단 기준으로 0번에 제로포인트를 두고
//반복해서 사용하고 같은 것인 있는지 비교
Point zeroPoint=al.get(0);
int count=0;//스코프이슈
for(Point point:al) {//범위가 정해져 있는 리스트
if(zeroPoint.equals(point)) {//만약 0번쨰좌표의 값과 포인트배열에 있는 값이 같다면
count++;
}
}
System.out.println("0번째와 같은 점의 개수는>>"+count+"개입니다.");
//사용자로부터 색 입력
// 스캐너 이용, 문자로 받음
Scanner sc=new Scanner(System.in);
System.out.println("색을 입력해주세요>>");
String findColor=sc.next();
int colorCount=0;
for(Point point :al) {
if(point instanceof ColorPoint) {
ColorPoint colorpoint=(ColorPoint) point;
if (colorpoint.getColor().equals(findColor)) {
colorCount++;
}
}
}
System.out.println(findColor + " 색의 점의 개수는 >> " + colorCount + "개입니다.");
}
}
//입력 시 유효성 검사 생략...
심화) 이 문제를 map을 활용해서 해결해주세요
점들에게 번호를 1001번부터 순차적으로 부여합니다.
Hash Map을 사용할건제 번호 pk값과 맵의 좌표를 이용한다(두개가 한쌍;;)
중복이 없는 특징이 있기 때문에 위 문제를 해결하는데 적합.
한글코딩
'JAVA > 코드' 카테고리의 다른 글
과제 1번 - 입출력(IO)을 이용한 업다운 게임 (0) | 2025.01.21 |
---|---|
컬렉션 프레임워크 문제풀기 (0) | 2025.01.21 |
TEAM과제 -삼중상속 (0) | 2025.01.18 |
포켓몬을 잡아라- 객체배열 (0) | 2025.01.16 |
학생부 프로그램 마무리와 코드리뷰 - 객체배열 (0) | 2025.01.14 |