코딩/java

상자의 정보

런던전통손만두 2019. 1. 6. 23:44
반응형

 

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

 

 }

}

 

 

결과:

 

반응형