1.아래의 Main메소드가 돌아 가도록 프로그래밍 하시오.
interface Printable {
public abstract void print(String doc);
}
public class InterfaceTest {
public static void main(String[] args) {
String myDoc = "This is a report about...";
Printable prn = new SPrinterDriver();
prn.print(myDoc);
System.out.println();
prn = new LPrinterDriver();
prn.print(myDoc);
}
}
class StringBuilderPrint {
private static final StringBuilder print = new StringBuilder();
static void printAndReset(String str) {
print.append(str);
System.out.print(print);
print.setLength(0);
}
}
class SPrinterDriver implements Printable {
SPrinterDriver() {
}
@Override
public void print(String doc) {
StringBuilderPrint.printAndReset(doc);
}
}
class LPrinterDriver implements Printable {
LPrinterDriver() {
}
@Override
public void print(String doc) {
StringBuilderPrint.printAndReset(doc);
}
}
//
// print
//
This is a report about...
This is a report about...
2.아래의 인터페이스에 맞추어(상속하여) 아래를 프로그래밍 하시오.
Circle, Rectangle , Triangle
interface AreaGetable {
public double getArea();
}
public static void main(String[] args) {
AreaGetable area = new Circle(4);
System.out.println(area.getArea());
area = new Rectangle(4,5);
System.out.println(area.getArea());
area = new Triangle(4,5);
System.out.println(area.getArea());
}
class Circle implements AreaGetable {
private double radius;
Circle(double radius) {
this.radius = radius;
}
Circle(int radius) {
this.radius = (double)radius;
}
@Override
public double getArea() {
double area = Math.PI * Math.pow(this.radius, 2);
return area;
}
}
class Rectangle implements AreaGetable {
private double length;
private double height;
Rectangle(double length, double height) {
this.length = length;
this.height = height;
}
@Override
public double getArea() {
double area = this.length * this.height;
return area;
}
}
class Triangle implements AreaGetable {
private double base;
private double height;
Triangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double getArea() {
double area = this.base * this.height * 0.5;
return area;
}
}
//
// print
//
50.26548245743669
20.0
10.0