//Shape3D.java //demo of inheritance and polymorphism import java.awt.*; // public abstract class Shape3D { private Point3D center; private Color color; private String id; public Shape3D() { this ( 800, 600 ); } public Shape3D( int width, int height ) { center = new Point3D(); center.x = (int)(Math.random()*width); center.y = center.z = (int)(Math.random()*height); // color = new Color( Color.WHITE ); color = new Color( (int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256) ); } public Shape3D( Point3D p, Color c ) { center = p; //fix color = c; //fix } public Point3D getCenter() { return center; } public void setID( String newID ) { id = newID; } public String getID() { return id; } public int getX() { return center.x; } public int getY() { return center.y; } public int getZ() { return center.z; } public void setX( int x ) { //some kind of bounds check... center.x = x; } public void setY( int y ) { center.y = y; } public void setColor( Color newColor ) { color = newColor; } public Color getColor() { return color; } public void move( int dx, int dy, int dz ) { center.x += dx; center.y += dy; center.z += dz; } public void moveTo( Point3D newCenter ) { center.x = newCenter.x; center.y = newCenter.y; center.z = newCenter.z; } //abstract methods must be implmented in every (non-abstract) subclass public abstract double getArea(); public abstract double getVolume(); public abstract void draw( Graphics g ); public abstract double dim1(); //x width public abstract double dim2(); //y height } class Point3D extends Point { public int z; public Point3D() { super(0,0); z = 0; } public Point3D( Point3D p ) { super( p.x, p.y ); z = p.y; } } class Cone extends Shape3D { private double radius; private double height; public Cone() { this( 1.0, 1.0 ); } public Cone( double r, double h ) { radius = r; height = h; setID( "cone " + (int)(Math.random()*1000) ); } //others... public double dim1() { return 2*radius; } public double dim2() { return height; } public double getArea() { return Math.PI * radius * Math.sqrt(radius * radius + height * height) + Math.PI * radius * radius; } public double getVolume() { return 1.0/3.0 * Math.PI * radius * radius * height; } public void draw( Graphics g ){ // g.drawString( getID(), getX(), getY()); double ovalHeight; if ( height > radius) ovalHeight = radius; else ovalHeight = height; g.drawOval( getX(), getY()+(int)height-(int)ovalHeight, 2*(int)radius, (int)ovalHeight ); g.drawLine( getX()+(int)radius, getY(), getX(), getY()+(int)height-(int)(.5*ovalHeight) ); g.drawLine( getX()+(int)radius, getY(), getX()+2*(int)radius, getY()+(int)height-(int)(.5*ovalHeight) ); } } class Sphere extends Shape3D { private double radius; public Sphere() { this ( 1.0 ); } public Sphere( double r ) { radius = r; setID( "sphere " + (int)(Math.random()*1000) ); } //others... public double dim1() { return 2*radius; } public double dim2() { return 2*radius; } public double getArea() { return 4 * Math.PI * radius * radius; } public double getVolume() { return 4.0/3.0 * Math.PI * radius * radius * radius; } public void draw( Graphics g ){ //g.drawString( getID(), getX(), getY()); g.drawOval( getX(), getY(), 2*(int)radius, 2*(int)radius ); g.drawArc( getX(), getY()+(int)radius, 2*(int)radius, (int)(.5*radius), 0, -180 ); } } class Cylinder extends Shape3D { private double radius; private double height; public Cylinder() { this( 1.0, 1.0 ); } public Cylinder( double r, double h ) { radius = r; height = h; setID( "cylinder " + (int)(Math.random()*1000) ); } //others... public double dim1() { return 2*radius; } public double dim2() { return height; } public double getArea() { return 2 * Math.PI * radius * height + 2 * Math.PI * radius * radius; } public double getVolume() { return Math.PI * radius * radius * height; } public void draw( Graphics g ){ double ovalHeight; if ( height > radius) ovalHeight = radius; else ovalHeight = height; g.drawOval( getX(), getY(), 2*(int)radius, (int)ovalHeight ); g.drawLine( getX(), getY()+(int)(.5*ovalHeight), getX(), getY()+(int)(.5*ovalHeight)+(int)height); g.drawLine( getX()+2*(int)radius, getY()+(int)(.5*ovalHeight), getX()+2*(int)radius, getY()+(int)(.5*ovalHeight)+(int)height); //bottom line: g.drawLine( getX(), getY()+(int)(.5*ovalHeight)+(int)height, getX()+2*(int)radius, getY()+(int)(.5*ovalHeight)+(int)height); //g.drawString( getID(), getX(), getY()); } } class Cube extends Shape3D { private double height; public Cube() { this( 1.0 ); } public Cube( double h ) { height = h; setID( "cube " + (int)(Math.random()*1000) ); } //others... public double dim1() { return height; } public double dim2() { return height; } public double getArea() { return 6 * height * height; } public double getVolume() { return height * height * height; } public void draw( Graphics g ){ //g.drawString( getID(), getX(), getY()); g.drawRect( getX(), getY(), (int)height, (int)height ); } }