
코드는 다음과 같습니다.
class Shape {
double getArea(){
return 0.0;
}
double getPerimeter(){
return 0.0;
}
public String toString(String a){
return "shape";
}
}
public class Point {
private int x,y;
public Point(){
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
@Override
public String toString() {
return null;
}
}
public class Rectangle extends Shape{
Point p1;
Point p2;
private int x1,y1,x2,y2;
private double area, perimeter;
public Rectangle(Point p1, Point p2) {
x1 = p1.getX();
x2 = p2.getX();
y1 = p1.getY();
y2 = p2.getY();
area = Math.abs(x1-x2) * Math.abs(y1-y2);
perimeter = (Math.abs(x1-x2) + Math.abs(y1-y2)) * 2;
}
public Rectangle(int x1, int y1, int x2, int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
area = Math.abs(x1-x2) * Math.abs(y1-y2);
perimeter = (Math.abs(x1-x2) + Math.abs(y1-y2)) * 2;
}
double getArea(){
System.out.print("p1:("+x1+","+y1+")\n");
System.out.print("p2:("+x2+","+y2+")\n");
return area;
}
double getPerimeter(){
return perimeter;
}
@Override
public String toString(){
return "Rectangle:";
}
}
public class Circle extends Shape{
Point center;
int radius;
private int x,y,r;
private double area, perimeter;
public Circle(Point p, int r){
x = p.getX();
y = p.getY();
area = Math.pow(r,2) * Math.PI;
perimeter = r*2*Math.PI;
}
public Circle(int x, int y, int r){
this.x = x;
this.y = y;
this.r = r;
area = Math.pow(r,2) * Math.PI;
perimeter = r*2*Math.PI;
}
double getArea(){
System.out.print("center:("+x+","+y+")\n");
System.out.print("radius:"+r+"\n");
return area;
}
double getPerimeter(){
return perimeter;
}
@Override
public String toString(){
return "Circle:";
}
}
public class main {
private static final int NUM = 4;
public static void main(String[] args) {
Shape[] shapes = new Shape[NUM];
shapes[0] = new Rectangle(20, 30, 50, 50);
shapes[1] = new Circle(30, 30, 20);
shapes[2] = new Rectangle(new Point(20, 30), new Point(50, 50));
shapes[3] = new Circle(new Point(30, 30), 20);
for (int i = 0; i < NUM; i++) {
System.out.println(shapes[i]);
System.out.printf("면적: %.2f\n", shapes[i].getArea());
System.out.printf("둘레: %.2f\n", shapes[i].getPerimeter());
}
}
}
interface ImageViewerInterface {
// ImageViewerInterface를 구현하는 클래스 모듈의
// 이름을 반환. 예) "JPG Module"
String getName();
// 해당되는 확장자를 가지고 있는 파일을 보이는 함수
// 본 문제에서는 그냥 로딩한다는 표시만 화면에
// 출력. 예) "JPG image loading: fileName"
void show(String fileName);
// 처리 가능한 이미지의 확장자를 문자열 형태로 반환
// 예) "JPG" 또는 "PNG"
String getExtension();
}
import java.util.ArrayList;
class ImageViewer {
// 필요한 멤버 변수 선언
// ImageViewerInterface를 구현한 클래스의 객체를
// 담을 수 있는 ArrayList 초기화
private ArrayList<ImageViewerInterface> arr = new ArrayList<ImageViewerInterface>();
ImageViewer(){
}
// ImageViewerInterface를 구현한 클래스의 객체를
// ArrayList에 추가
void addPlugIn(ImageViewerInterface ivi){
arr.add(ivi);
}
// 이미지 파일 이름과 확장자에 해당되는 인자를 받아서
// 배열에서 해당되는 확장자를 처리할 수 있는 객체를
// 찾아서 처리. 만약 플러그인을 찾지 못하면, "Image
// viewer plugin for the extension, ???, is not
// registered"를 출력할 것. ???는 확장자
void show(String fileName, String ext){
int count=0;
for(int i=0;i<arr.size();i++) {
if (ext.equals(arr.get(i).getExtension())) {
System.out.println("Using "+arr.get(i).getName());
arr.get(i).show(fileName);
count = 1;
}
}
if (count==0) {
System.out.println("Image viewer plugin for the extension," + ext + ", is not registered");
}
}
}
class JPGModule implements ImageViewerInterface{
@Override
public String getName() {
return "JPG Module";
}
@Override
public void show(String fileName){
System.out.println("JPG image loading: "+fileName);
}
@Override
public String getExtension() {
return "JPG";
}
}
class PNGModule implements ImageViewerInterface{
@Override
public String getName() {
return "PNG Module";
}
@Override
public void show(String fileName ){
System.out.print("PNG image loading:"+fileName+"\n");
}
@Override
public String getExtension() {
return "PNG";
}
}
public class TestImageViewer {
public static void main(String[] args) {
ImageViewer v = new ImageViewer();
v.addPlugIn(new PNGModule());
v.addPlugIn(new JPGModule());
v.show("a.jpg", "JPG");
v.show("b.png", "PNG");
v.show("c.tif", "TIF");
}
}