-
반응형
public class Box {
private int x;
private int y;
private int z;
private boolean empty;
public String toString() {
String rslt = "가로: " + x + "cm\n세로: " + y + "cm\n높이: " + z + "cm\n";
if (empty)
return rslt + "지금 박스는 비어있습니다.\n";
else
return rslt + "지금 박스에는 물건이 들어있습니다.\n";
}
public void setX(int x) {this.x = x;}
public void setY(int y) {this.y = y;}
public void setZ(int z) {this.z = z;}
public int getX() {return x;}
public int getY() {return y;}
public int getZ() {return z;}
public void fillBox() {
this.empty = false;
}
public void emptyBox() {
this.empty = true;
}
public Box() {
this(0, 0, 0);
}
public Box(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
emptyBox();
}
}
public class Practice_51 {
public static void main(String[] args) {
Box b0 = new Box();
Box b1 = new Box();
Box b2 = new Box(100, 60, 20);
b1.setX(30);
b1.setY(45);
b1.setZ(53);
b1.fillBox();
System.out.println("디폴트 상자의 정보입니다.");
System.out.print(b0.toString());
System.out.println("첫 번째 상자의 정보입니다.");
System.out.print(b1.toString());
System.out.println("두 번째 상자의 정보입니다.");
System.out.print(b2.toString());
}
}
결과:
반응형'코딩 > java' 카테고리의 다른 글
영화 정보 출력(생성자) (0) 2019.01.07 랜덤 숫자 200개 출력 (0) 2019.01.07 비행기 정보(생성자) (0) 2019.01.06 생일, 성년일 계산(설정자, 접근자) (1) 2019.01.06 은행 계좌(toString, 설정자, 접근자) (0) 2019.01.06