๐ย ์ค๋ ์ฝ์ ๋ฒ์ : 6. ๊ฐ์ฒด์ ์๋ฃ๊ตฌ์กฐ(118~128p)
public class Point {
public double x;
public double y;
}
์ง๊ต์ขํ๊ณ ์์ ๋ฐ๋ก ์๊ฒ ๋๋ฒ๋ฆฐ๋ค
๊ฐ๋ณ์ ์ขํ๊ฐ์ read, write ํ๊ฒ ๊ฐ์ ํ๋ค
๊ตฌํ์ ๋ ธ์ถํ๋ค โ ๋ณ์๋ฅผ private ์ผ๋ก ํ ์ง๋ผ๋ ๊ฐ ๊ฐ๋ง๋ค get, set์ ์ ๊ณตํ๋ฉด ๊ตฌํ์ ์ธ๋ถ๋ก ๋ ธ์ถํ๋ ์
๋ณ์ ์ฌ์ด์ ํจ์๋ผ๋ ๊ณ์ธต์ ๋ฃ๋๋ค๊ณ ๊ตฌํ์ด ๊ฐ์ถฐ์ง๋๊ฒ ์๋๋ผ ์ถ์ํ๊ฐ ํ์
public interface Point {
double getX();
double getY();
void setCartesian(double x, double y);
double getR();
double getTheta();
void setPolar(double r, double theta);
}
์ง๊ต, ๊ทน ์ขํ๊ณ ๋ฑ์ ๋ชฐ๋ผ๋ ์ธํฐํ์ด์ค๊ฐ ์๋ฃ ๊ตฌ์กฐ๋ฅผ ๋ช ๋ฐฑํ๊ฒ ํํ
ํด๋์ค ๋ฉ๋์ค๊ฐ ์ ๊ทผ ์ ์ฑ ์ ๊ฐ์
์ฝ์ ๋๋ ๊ฐ ์ขํ ๊ฐ๋ณ๋ก
์ค์ ํ ๋๋ ๋ ๊ฐ์ ํ๋ฒ์ ์ค์
public class Square {
public Point topLeft;
public double side;
}
public class Rectangle {
public Point topLeft;
public double height;
public double width;
}
public class Circle {
public Point center;
public double radius;
}
public class Geometry {
public final double PI = 3.141592653589793;
public double area(Object shape) throws NoSuchShapeException {
if (shape instanceof Square) {
Square s = (Square) shape;
return s.side * s.side;
} else if (shape instanceof Rectangle) {
Rectangle r = (Rectangle) shape;
return r.height * r.width;
} else if (shape instanceof Circle) {
Circle c = (Circle) shape;
return PI * c.radius * c.radius;
}
throw new NoSuchShapeException();
}
}
public class Square implements Shape {
private Point topLeft;
private double side;
public double area() {
return side * side;
}
}
public class Rectangle implements Shape {
private Point topLeft;
private double height;
private double width;
public double area() {
return height * width;
}
}
public class Circle implements Shape {
private Point center;
private double radius;
public final double PI = 3.141592653589793;
public double area() {
return PI * radius * radius;
}
}
์ ์ฐจ์ ์ธ ์ฝ๋
์ ํจ์ ์ถ๊ฐ ์ฌ์
์ ์๋ฃ ๊ตฌ์กฐ ์ถ๊ฐ ์ด๋ ค์
๊ฐ์ฒด ์งํฅ ์ฝ๋
์ ํด๋์ค ์ถ๊ฐ ์ฌ์
์ ํจ์ ์ถ๊ฐ ์ด๋ ค์
ํด๋์ค C
f๊ฐ ์์ฑํ ๊ฐ์ฒด
f์ ์ธ์๋ก ๋์ด์จ ๊ฐ์ฒด
C ์ธ์คํด์ค ๋ณ์์ ์ ์ฅ๋ ๊ฐ์ฒด
๋จ ์ ๊ฐ์ฒด์์ ํ์ฉ๋ ๋ฉ์๋๊ฐ ๋ฐํํ๋ ๊ฐ์ฒด์ ๋ฉ์๋๋ ํธ์ถํ๋ฉด ์ ๋๋ค
final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();
๋๋ฏธํฐ ๋ฒ์น ์๋ฐ์ฌ๋ก
Options opts = ctxt.getOptions();
File scratchDir = opts.getScratchDir();
final String outputDir = scratchDir.getAbsolutePath();
ctxt, Options, ScratchDir ์ด ๊ฐ์ฒด๋ฉด ๋๋ฏธํฐ ๋ฒ์น ์๋ฐ ์๋ฃ ๊ตฌ์กฐ์ด๋ฉด ๋๋ฏธํฐ ๋ฒ์น ์ ์ฉ ์๋final String outputDir = ctxt.options.scratchDir.absolutePath;
ctxt.getAbsolutePathOfScratchDirectoryOption();
ctxt ๊ฐ์ฒด์ ๊ณต๊ฐํด์ผ ํ๋ ๋ฉ์๋๊ฐ ๋๋ฌด ๋ง์์ง๋คctx.getScratchDirectoryOption().getAbsolutePath()
getScratchDirectoryOption๊ฐ ์๋ฃ ๊ตฌ์กฐ๋ง ๋ฐํํ๋ค๋ ๊ฐ์ ์ด๋คBufferedOutputStream bos = ctxt.createScratchFileStream(classFileName);
ctxt๋ ๋ด๋ถ ๊ตฌ์กฐ๋ฅผ ๋๋ฌ๋ด์ง ์์ผ๋ฉฐ, ๋ชจ๋์์ ํด๋น ํจ์๋ ์์ ์ด ๋ชฐ๋ผ์ผํ๋ ์ฌ๋ฌ ๊ฐ์ฒด๋ฅผ ํ์ํ ํ์๊ฐ ์๋ค. โ ๋๋ฏธํฐ ๋ฒ์น์ ์๋ฐํ์ง ์์public class Address {
private String street;
private String streetExtra;
private String city;
private String state;
private String zip;
public Address(String street, String streetExtra,
String city, String state, String zip) {
this.street = street;
this.streetExtra = streetExtra;
this.city = city;
this.state = state;
this.zip = zip;
}
public String getStreet() {
return street;
}
public String getStreetExtra() {
return streetExtra;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public String getZip() {
return zip;
}
}
BufferedOutputStream bos = ctxt.createScratchFileStream(classFileName);
๊ฐ ์ ๋๋ฏธํฐ ๋ฒ์น์ ์๋ฐฐํ์ง ์๊ฒ ๋ฐ๊พผ ๊ฒ์ธ๊ฐ? dir ์ป์ด์ ๊บผ๋ด๋๊ฒ ์๋๋ผ ์์์ ์์ํ์ผ ์์ฑ ์์ ํด๋ฒ๋ฆฌ๋๊น ๊ด์ฐฎ๋ค๋๊ฑด๊ฐ ๊ทธ๋๋ ctxt ํ๋์ ๋ชจ๋ ๋ฉ์๋๊ฐ ๊ณต๊ฐ๋๋ฏ๋ก ์์ข์๊ฑด ์ฌ์ ํ์ง ์๋?