๋ฌธ์ ) ๋ค์์ ๊ฐ๋ก ์ธ๋ก๋ก ๊ตฌ์ฑ๋๋ ๋ฐ์ค๋ฅผ ํํํ๋ Box ํด๋์ค์ ์ด๋ฅผ ์ด์ฉํ๋ ์ฝ๋์ด๋ค. Box draw()๋ fill ํ๋์์ ์ง์ ๋ ๋ฌธ์๋ก ์์ ์ ๊ทธ๋ฆฐ๋ค. ์คํ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด์ ์ฝ๋๋ฅผ ์์ฑํ๋ผ
ํ์ด)
package ์ค์ต๋ฌธ์ 6;
public class Box {
private int width, height;
private char fillChar;
public Box() {
this(10,1);
}
public Box(int width, int height) {
this.width=width;
this.height=height;
}
public void draw() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print(fillChar);
}
System.out.println();
}
}
public void fill(char c) {
fillChar = c;
}
public static void main(String[] args) {
Box a = new Box();
Box b = new Box(20,3);
a.fill('*');
b.fill('%');
a.draw();
b.draw();
}
}
1. this() ๋ก ๋ค๋ฅธ ์์ฑ์ ํธ์ถํ ์ ์๋ค.
๊ฐ์ฒด ์์ฑ ์ ์๋ฌด๋ฐ ๋งค๊ฐ๋ณ์๋ฅผ ์ ๋ ฅํ์ง ์์์ ๊ฒฝ์ฐ ๊ฐ๋ก 10๊ฐ, ์ธ๋ก 1๊ฐ์ ๋ฐ์ค๋ฅผ ๊ทธ๋ฆฌ๋๋ก ๊ณ ์ ํด๋์
2. ์ด์ค์ผ๋ก ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํจ. ( ๊ตฌ๊ตฌ๋จ ๋ง๋ค๊ธฐ์ ๋์ผํ ์๋ฆฌ์)
์ถ๋ ฅํ๋ฉด)

'BACKEND > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์ค์ต ๋ฌธ์ ) p235 1๋ฒ ํ์ด -๋ช ํ ์๋ฐ ์์ผ์ (0) | 2023.10.03 |
|---|---|
| ์ค์ต ๋ฌธ์ ) p188 Bonus1 ํ์ด -๋ช ํ ์๋ฐ ์์ผ์ (2) | 2023.10.03 |
| ์ค์ต ๋ฌธ์ ) p185 4๋ฒ ํ์ด -๋ช ํ ์๋ฐ ์์ผ์ (0) | 2023.10.03 |
| ์ฟ ํค(Cookie) ๋? (0) | 2023.09.28 |
| Scope ) session ์์ญ (0) | 2023.09.26 |