import java.util.*; abstract class Shape{ private String next; public Shape() {next = null; } public void setNext(String string) {next=string;} public String getNext() {return next;} public abstract void draw(); } class Line extends Shape{ public void draw() { setNext("Line"); } } class Rect extends Shape{ public void draw() { setNext("Rect"); } } class Circle extends Shape{ public void draw() { setNext("Circle"); } } public class Main{ public static void main(String[] args) { System.out.println("그래픽 에디터 서준혁을 실행합니다."); Scanner sc=new Scanner(System.in); int a = 1,e=0,b,c; String arr[]=new String[100]; while(a!=4) { System.out.println("삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>"); a=sc.nextInt(); if(a==1) { System.out.println("Line(1), Rect(2), Circle(3)"); b=sc.nextInt(); if(b==1) { Line L=new Line(); arr[e++]=getNext(); } else if(b==2) { Rect L=new Rect(); arr[e++]=getNext(); } else if(b==3) { Circle L=new Circle(); arr[e++]=getNext(); } } else if(a==2) { System.out.println("삭제할 도형의 위치>>"); b=sc.nextInt(); if(arr[b]==null) { System.out.println("삭제할 수 없습니다."); continue; } else { arr[b]=null; for(int i=0;i<e;i++) { if(arr[i-1]==null) { arr[i-1]=arr[i]; arr[i]=null; } } e--; } } else if(a==3) { for(int i=0;i<e;i++) { System.out.println(arr[i]); } } else if(a==4) { break; } } System.out.println("서준혁을 종료합니다."); } }
-------------------------
import java.util.*; interface Shape{ final double PI=3.14; void draw(); double getArea(); default public void redraw() { System.out.println("--- 다시 그립니다. 반지름이 "+getradius()+"인 원입니다."); draw(); } int getradius(); } class Circle implements Shape { int b; Circle(int b) { this.b=b; } public int getradius() { return b; } public void draw() { } public double getArea() { return PI*b*b; } } public class Main{ public static void main(String[] args) { Shape donut=new Circle(10); donut.redraw(); System.out.println("면적은 "+donut.getArea()); } }
---------------------------
import java.util.*; interface Shape{ final double PI=3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. 반지름이 "); getradius(); System.out.print("인 원입니다."); draw(); } void getradius(); } class Circle implements Shape { int b; Circle(int b) { this.b=b; } public void getradius() { System.out.println(b); } public void draw() { } public double getArea() { return PI*b*b; } } class Oval implements Shape { int d,c; Oval(int c,int d) { this.c=c; this.d=d; } public void getradius() { System.out.println(c+"*"+d); } public void draw() { } public double getArea() { return PI*d*c; } } class Rect implements Shape { int e,f; Rect(int e,int f) { this.e=e; this.f=f; } public void draw() { } public double getArea() { return e*f; } public void getradius() { System.out.println(e+"*"+f); } } public class Main{ public static void main(String[] args) { Shape[] list=new Shape[3]; list[0] =new Circle(10); list[1] =new Oval(20,30); list[2] =new Rect(10,40); for(int i=0;i<list.length;i++) { list[i].redraw(); } for(int i=0;i<list.length;i++) { System.out.println("면적은 "+list[i].getArea()); } } }