[LIKELION] 220923 과제

고관운·2022년 9월 25일
0

1. 좌표값에 X표시하기

import java.util.Scanner;

public class test1 {

	public static void main(String[] args) {
		String[][] arr = {{" "," "," "," "," "}, {" "," "," "," "," "}, {" "," "," "," "," "}, {" "," "," "," "," "}, {" "," "," "," "," "}};
		int row;
		int col;
		
		Scanner sc = new Scanner(System.in);
		
		while(true) {
			System.out.print("행 인덱스 입력 >> ");
			row = sc.nextInt();
			if (row < 0 || row >= 5) {
				break;
			}
			System.out.print("열 인덱스 입력 >> ");
			col = sc.nextInt();
			if (col < 0 || col >= 5) {
				break;
			}
			
			arr[row][col] = "X";
			
			System.out.println("  0 1 2 3 4");
			for(int i=0; i < 5; i++) {
				System.out.print(i);
				for(int j=0; j < 5; j++) {
					System.out.print(" " + arr[i][j]);
				}
				System.out.println();
			}
			
		}
		System.out.println("프로그램이 종료되었습니다.");

	}
}

2. Rectangle클래스를 만들고 정보 출력

class Rectangle {
	int x;
	int y;
	int width;
	int height;
	
	public Rectangle() {
		x = 0;
		y = 0;
		width = 0;
		height = 0;
	}
	
	public Rectangle(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	public int square() {
		return width * height;
	}
	
	public void show() {
		System.out.println("(" + x + "," + y + ")에서 크기가 " + width + "x" + height + "인 사각형");
	}
	
	public boolean contains(Rectangle r) {
		if (x < r.x && y < r.y && (x+width) > (r.x+r.width) && (y+height) > (r.y+r.height)) {
			return true;
		}
		else {
			return false;
		}
	}
}

public class test2 {

	public static void main(String[] args) {
		Rectangle r = new Rectangle(2,2,8,7);
		Rectangle s = new Rectangle(5,5,6,6);
		Rectangle t = new Rectangle(1,1,10,10);

		r.show();
		System.out.println("s의 면적은 " + s.square());
		if(t.contains(r)) System.out.println("t는 r을 포함합니다.");
		if(t.contains(s)) System.out.println("t는 s을 포함합니다.");
	}
}

🟢 (x < r.x && y < r.y && (x+width) > (r.x+r.width) && (y+height) > (r.y+r.height)) : 원 내부에 포함되었는지 확인하는 조건

0개의 댓글