IT/솔루션) Power Java

Power JAVA 9장 ) 생성자와 접근 제어 - Programming_3

돔찌 2019. 5. 12. 17:43

<2016. 10. 3. 19:00>

 

Power JAVA 9장 ) 생성자와 접근 제어 - Programming_3

 

/* 
 * 상자를 나타내는 Box라는 이름의 클래스를 설계하라.
 * Box 클래스는 상자의 가로, 세로, 높이를 필드로 가지고 있다.
 * 박스가 비어 있는지 그렇지 않은지를 나타내는 empty라고 하는 필드도 추가한다.
 * Box 클래스의 생성자를 중복 정의하라. 생성자는 모든 데이터를 받을 수도 있고 아니면 하나도 받지 않을 수 있다.
 * 새로 생성된 Box는 비어 있다고 가정한다.
 */
class Box {
	private int width;
	private int length;
	private int height;
	private boolean empty = true;
	Box(int width, int length, int height ) {
		this.width = width;
		this.length = length;
		this.height = height;
	}
	Box() {
		this(1,1,1);
	}
	void openBox() {
		empty = !empty;
	}
	void emptyCheck() {
		if (empty == true)
		   System.out.println("이 박스는 비어있습니다."); else
		   System.out.println("이 박스는 비어있지 않습니다.");
	}
	public String toString() {
		return "가로 : " + width + "세로 : " + length + "높이 : " + height;
	}
}
public class Programming_3 {
	public static void main(String[] args) {
		Box mybox1 = new Box(5,6,7);
		System.out.println(mybox1);
		mybox1.emptyCheck();
		Box mybox2 = new Box();
		System.out.println(mybox2);
		mybox2.emptyCheck();
	}
}