Notice
Recent Posts
Recent Comments
Link
«   2024/06   »
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
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter01 : rect / rectmain 본문

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

	}

}

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

chapter06 : Local / LocalMain  (0) 2023.05.26
chapter05 : Circle / CircleMain  (0) 2023.05.26
chapter04 : Book / BookMain  (0) 2023.05.26
chapter03 : person / personmain  (0) 2023.05.26
chapter02 : Student / StudentMain  (0) 2023.05.26