[this-2] 도형(사각형)

seratpfk·2022년 7월 28일
0

JAVA

목록 보기
49/96

Rectangle 클래스 생성 (메인메소드 없음)

// 필드
private int width;
private int height;
// 생성자
public Rectangle(int width, int height) {
	this.width = width;
	this.height = height;
}
public Rectangle(int n) {
	this(n, n);  // 인수가 2개인 다른 생성자를 호출한다.
}
// 메소드
public int getArea() {
	return width * height;
}
public int getCircumgerence() {
	return 2 * (width + height);
}

RectangleMain 클래스 생성 (메인메소드 설정)

// 직사각형
Rectangle rect1 = new Rectangle(3, 4);
// 정사각형
Rectangle rect2 = new Rectangle(5);
System.out.println("넓이 : " + rect1.getArea());
System.out.println("둘레 : " + rect1.getCircumgerence());
System.out.println("넓이 : " + rect2.getArea());
System.out.println("둘레 : " + rect2.getCircumgerence());

넓이 : 12
둘레 : 14
넓이 : 25
둘레 : 20

0개의 댓글