package homework;

import java.util.Scanner;

//2. Rectangle클래스를 작성하세요

//int타입의 x1, y1, x2, y2 : 사각형을 구성하는 두 점의 좌표
//void set(int x1, int y1, int x2, int y2) : 좌표 설정
//int square() : 사각형 넓이 리턴
//void show() : 좌표와 넓이 등 직사각형 정보의 화면 출력
//boolean equals(Rectangle r) : 인자로 전달된 객체 r과 현 객체가 동일한 좌표의 직사각형이면 true 리턴
//

public class RectangleMain {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		Rectangle re = new Rectangle();		
		
		System.out.print("좌표 x1을 입력해주세요 (x1 < x2) >>> ");
		int num0 = scan.nextInt();
		System.out.print("좌표 y1을 입력해주세요 (y1 < y2) >>> ");
		int num1 = scan.nextInt();
		System.out.print("좌표 x2을 입력해주세요 (x1 < x2) >>> ");
		int num2 = scan.nextInt();
		System.out.print("좌표 y2을 입력해주세요 (y1 < y2) >>> ");
		int num3 = scan.nextInt();
		

		re.set(num0, num1, num2, num3);

		re.square();
		re.show();

		Rectangle r = new Rectangle();
		
		r.set(10, 26, 30, 45);
		System.out.println(re.equals(r));
		
		r.set(3, 9, 10, 46);
		System.out.println(re.equals(r));
		
		scan.close();
	}
}

 

package homework;

//int타입의 x1, y1, x2, y2 : 사각형을 구성하는 두 점의 좌표
//void set(int x1, int y1, int x2, int y2) : 좌표 설정
//int square() : 사각형 넓이 리턴
//void show() : 좌표와 넓이 등 직사각형 정보의 화면 출력
//boolean equals(Rectangle r) : 인자로 전달된 객체 r과 현 객체가 동일한 좌표의 직사각형이면 true 리턴
//
public class Rectangle {
	int x1;
	int x2;
	int y1;
	int y2;
	int square;

	void set(int x1, int y1, int x2, int y2) {
		this.x1 = x1;
		this.x2 = x2;
		this.y1 = y1;
		this.y2 = y2;
	}

	int square() {
		this.square = (this.x2 - this.x1) * (this.y2 - this.y1);
		return this.square;
	}

	void show() {
		System.out.printf("좌표는 (%d, %d)와 (%d, %d)이고,", x1, y1, x2, y2);
		System.out.println("이 좌표값이 이루는 직사각형의 넓이는 " + square + "입니다.");
	}

	boolean equals(Rectangle r) {
		if (r.x1 == this.x1 && r.x2 == this.x2 && r.y1 == this.y1 && r.y2 == this.y2) {
			return true;
		} else {
			return false;
		}
	}
}

+ Recent posts