public class Ex03_method {
public static void main(String[] args) {
Rect r = new Rect();
int a, b;
r.width = 10;
r.height = 5;
a = r.area();
b = r.length();
System.out.println("넓이:"+a+", 둘레:"+b);
}
}
/*
- 직사각형의 넓이와 둘레계산
- 데이터(상태) : 가로, 세로
- 메소드 : 넓이계산, 둘레계산
*/
class Rect {
int width;
int height;
// 필드 선언 시, 자료형이 같아도 따로따로 선언한다.
// 넓이
public int area() {
return width * height;
}
// 둘레
public int length() {
return (width + height) * 2;
}
}
'쌍용강북교육센터 > 7월' 카테고리의 다른 글
0714_Ex05_method (0) | 2021.07.14 |
---|---|
0714_Ex04_method (0) | 2021.07.14 |
0714_Ex02_method (0) | 2021.07.14 |
0714_Ex01_field (0) | 2021.07.14 |
0713_Ex07_method (0) | 2021.07.14 |