Study 도형 중심좌표와 높이 너비, 다중 클래스 (상속) bslime 2008. 5. 31. 15:34 public class Shape{ private double x,y; public Shape(double x, double y){ setX(x); setY(y); } public double getX(){ return x; } public void setX(double x){ this.x = x; } public double getY(){ return y; } public void setY(double y){ this.y = y; } } public class Circle extends Shape{ private double radius; public Circle(){ super(0,0); } public Circle(double x, double y){ super(x,y); } public Circle(double radius){ this(0,0); this.setRadius(radius); } public void draw(){ System.out.println("이 다각형은 원입니다."); System.out.println("\t중심좌표 : ("+this.getX()+", "+this.getY()+")"); System.out.println("\t반지름 : "+this.getRadius()+"\n"); } public double getRadius(){ return radius; } public void setRadius(double radius){ this.radius = radius; } } public class Triangle extends Shape{ private double width; private double height; public Triangle(double x, double y){ super(x,y); } public Triangle(double x, double y, double width, double height){ super(x,y); this.setWidth(width); this.setHeight(height); } public void draw(){ System.out.println("이 다각형은 삼각형입니다."); System.out.println("\t중심좌표 : ("+this.getX()+", "+this.getY()+")"); System.out.println("\t가로 : "+this.getWidth()); System.out.println("\t세로 : "+this.getHeight()+"\n"); } public double getWidth(){ return width; } public void setWidth(double width){ this.width = width; } public double getHeight(){ return height; } public void setHeight(double height){ this.height = height; } } public class Rectangle extends Shape{ private double width; private double height; public Rectangle(double x, double y){ super(x,y); } public Rectangle(double x, double y, double width, double height){ super(x,y); this.setWidth(width); this.setHeight(height); } public void draw(){ System.out.println("이 다각형은 사각형입니다."); System.out.println("\t중심좌표 : ("+this.getX()+", "+this.getY()+")"); System.out.println("\t가로 : "+this.getWidth()); System.out.println("\t세로 : "+this.getHeight()+"\n"); } public double getWidth(){ return width; } public void setWidth(double width){ this.width = width; } public double getHeight(){ return height; } public void setHeight(double height){ this.height = height; } } public class Output{ public static void main(String args[]){ Circle c1 = new Circle(4.5, 7.9); c1.setRadius(3.5); c1.draw(); Rectangle r1 = new Rectangle(2.5, 7.9); r1.setWidth(4.3); r1.setHeight(5.9); r1.draw(); Rectangle r2 = new Rectangle(2.5, 7.9, 3.6, 6.1); r2.draw(); Triangle t1 = new Triangle(3.5, 6.9); t1.setWidth(5.7); t1.setHeight(4.6); t1.draw(); Triangle t2 = new Triangle(3.5, 6.9, 6.2, 9.1); t2.draw(); } }