Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter01 : rect / rectmain 본문

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();
		
		
		
	}
}

'JAVA > chapter07_method' 카테고리의 다른 글

Test . method  (0) 2023.05.26
Test . oper  (0) 2023.05.26
chapter04 : Triangle  (0) 2023.05.26
chapter03 : Recursiveclass  (0) 2023.05.26
chapter02 : local / localmain  (0) 2023.05.26