JAVA/chapter15_access_modifer
chapter01 : rect / rectmain
GAWON
2023. 5. 26. 18:43
package org.joonzis.ex;
/*
* 필드
* - width, height, isSquare
*
* 메소드
* - 생성자() : 가로, 세로 1로 고정
* - 생성자(int side) : 가로, 세로 같은 값으로 저장
* - 생성자(w, h) : 가로, 세로 값 전달 받아 저장
* - int calcArea() : 사각형 크기 리턴
* - output() : 너비, 높이, 크기, 형태 (정사각형or 직사각형) 출력
*
*/
public class Ex01_Rect {
private int width, height;
private boolean isSquare;
public Ex01_Rect() {
this(1, 1);
}
public Ex01_Rect(int side) {
this(side, side);
}
public Ex01_Rect(int width, int height) {
this.width = width;
this.height = height;
isSquare = (width == height) ? true : false;
}
private int calcArea() {
return width * height;
}
public void output() {
System.out.println("너비 : " + width);
System.out.println("높이 : " + height);
System.out.println("크기 : " + calcArea());
System.out.println("형태 : " + (isSquare ? "정사각형" : "직사각형"));
}
}
package org.joonzis.ex;
public class Ex01_RectMain {
public static void main(String[] args) {
// 1. 객체(인스턴스) 생성
// ==>>> 생성자가 호출된다!
Ex01_Rect nemo1 = new Ex01_Rect();
Ex01_Rect nemo2 = new Ex01_Rect(4);
Ex01_Rect nemo3 = new Ex01_Rect(3, 5);
// 2. 객체 메소드 호출
nemo1.output();
System.out.println("-------------");
nemo2.output();
System.out.println("-------------");
nemo3.output();
}
}