JAVA/chapter07_method
chapter01 : rect / rectmain
GAWON
2023. 5. 26. 17:45
package org.joonzis.ex;
public class Ex01_Rect {
// 필드
int width; // 너비
int height; // 높이
boolean isSquare; // 정사각형 유무 - 직사각형:false , 정사각형:true
// 메소드
// 1. 필드 초기화 메소드
void setFields(int w, int h) {
width = w;
height = h;
isSquare = (w==h) ? true : false;
}
void setFields(int side) {
width = side;
height = side;
isSquare = true;
}
// 2. 사각형 크기 계산 메소드
int calcArea() {
int result = width * height;
return result;
}
// 3. 정보 출력 메소드
void output() {
System.out.println("너비 : " + width);
System.out.println("높이 : " + height);
System.out.println(isSquare ? "정사각형" : "직사각형");
System.out.println("크기 : " + calcArea());
}
}
package org.joonzis.ex;
public class Ex01_RectMain {
public static void main(String[] args) {
// 1. 객체 생성
Ex01_Rect r1 = new Ex01_Rect();
r1.setFields(10, 10);
r1.output();
System.out.println("----------------");
Ex01_Rect r2 = new Ex01_Rect();
r2.setFields(15); // 메소드 오버로딩
r2.output();
}
}