Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.

Paste

Pasted as Java by ms ( 14 years ago )
abstract class Shape {
  private int x, y;
  public Shape( int x, int y ){
  this.x = x;
  this.y = y;
  }
  public void setX( int x ) {
   this.x = x; }
  public void setY( int y ) {
   this.y = y; }
  public int getX() {
   return x; }
  public int getY() {
   return y; }
  //The abstracted method that will be used in all classes
  public abstract String getName();
  public abstract void toString();
}


abstract class TwoDimensionalShape extends Shape {
  private int dimension1, dimension2;
  public TwoDimensionalShape(int x, int y, int d1, int d2 ){
  super( x, y );
  dimension1 = d1;
  dimension2 = d2; }
  public void setDimension1( int d ) {
  dimension1 = d; }
  public void setDimension2( int d ){
  dimension2 = d; }
  public int getDimension1() {
  return dimension1; }
  public int getDimension2() {
  return dimension2; }
  public abstract int area();}


abstract class ThreeDimensionalShape extends Shape {
  private int dimension1, dimension2, dimension3;

  public ThreeDimensionalShape( int x, int y, int d1, int d2, int d3 ) {
   super( x, y );
   dimension1 = d1;
   dimension2 = d2;
   dimension3 = d3; }

  public void setDimension1( int d ) {
   dimension1 = d; }
  public void setDimension2( int d ) {
   dimension2 = d; }
  public void setDimension3( int d ) {
   dimension3 = d; }
  public int getDimension1(){
   return dimension1; }
  public int getDimension2() {
   return dimension2; }
  public int getDimension3() {
   return dimension3; }
  public abstract int area();
  public abstract int volume();}


class Circle extends TwoDimensionalShape {
  public Circle( int x, int y, int radius )  {
   super( x, y, radius, radius ); }

  public String getName() {
  return "Circle"; }

  public void toString() {
   System.out.println( "(" + super.getX() + ", " + super.getY() +") " + "radius: " + super.getDimension1() ); }

  public int area(){
  return(int)( Math.PI * super.getDimension1() * super.getDimension1() ); }

  public void setRadius( int radius )  { super.setDimension1( radius ); }

  public int getRadius(){ return super.getDimension1();}}

class Square extends TwoDimensionalShape {

  public Square( int x, int y, int side ) {
   super( x, y, side, side ); }

  public String getName() {
   return "Square";}

  public void toString() {
  System.out.println( "(" + super.getX() + ", " +
  super.getY() +") " + "side: " + super.getDimension1() );
  }
  public int area(){
  return super.getDimension1() * super.getDimension1();}
  public void setSide( int side ) {
  super.setDimension1( side ); }
  public int getSide(){
  return super.getDimension1(); }}


class Sphere extends ThreeDimensionalShape{
  public Sphere( int x, int y, int radius ) {
  super( x, y, radius, radius, radius ); }
  public String getName() {
  return "Sphere";}
  public int area() {
  return ( int ) ( 4 * Math.PI * super.getDimension1() * super.getDimension1() );
  }
  public int volume() {
  return ( int ) ( 4 / 3 * Math.PI * super.getDimension1() *
  super.getDimension1() * super.getDimension1() );
  }
  public void toString() {
  System.out.println( "(" + super.getX() + ", " + super.getY() +") "
  + "radius: " + super.getDimension1() );
  }
  public void setRadius( int radius ) {
  super.setDimension1( radius );}
  public int getRadius(){
  return super.getDimension1();}
}


class Cube extends ThreeDimensionalShape {
  public Cube( int x, int y, int side ){
  super( x, y, side, side, side ); }
  public String getName() {
  return "Cube"; }
  public int area(){
  return (int)( 6 * super.getDimension1() *
  super.getDimension1() );
  }public int volume(){
  return ( int ) ( super.getDimension1() *
  super.getDimension1() * super.getDimension1() );
  }
  public void toString() {
  System.out.println( "(" + super.getX() + ", " + super.getY() +
  ") " + "side: " + super.getDimension1() );
  }
  public void setSide( int side ) {
  super.setDimension1( side ); }
  public int getSide() {
  return super.getDimension1();
}}


public class ShapeTest{
 public static void main( String args[] )
{ ShapeTest driver = new ShapeTest();
   driver.displayShapeInfo();

}

  private Shape shapeArray[];
  private TwoDimensionalShape twoDArray[];
  private ThreeDimensionalShape threeDArray[];


  public ShapeTest() {
  shapeArray = new Shape[ 4 ];
  twoDArray = new TwoDimensionalShape[ 2 ];
  threeDArray = new ThreeDimensionalShape[ 2 ];
  Circle circle = new Circle( 22, 88, 21 );
  shapeArray[ 0 ] = circle;
  twoDArray[ 0 ] = circle;
  Square square = new Square( 71, 96, 95 );
  shapeArray[ 1 ] = square;
  twoDArray[ 1 ] = square;
  Sphere sphere = new Sphere( 8, 89, 78 );
  shapeArray[ 2 ] = sphere;
  threeDArray[ 0 ] = sphere;
  Cube cube = new Cube( 79, 61, 73 );
  shapeArray[ 3 ] = cube;
  threeDArray[ 1 ] = cube;  }


  public void displayShapeInfo() {
  for ( int i = 0; i < shapeArray.length; i++ ) {
  System.out.print( shapeArray[ i ].getName() + ": " );
  shapeArray[ i ].toString();}

  for ( int j = 0; j < twoDArray.length; j++ )
  System.out.println( twoDArray[ j ].getName() + "'s area is " + twoDArray[ j ].area() );

  for ( int k = 0; k < threeDArray.length; k++ )
  {
  System.out.println( threeDArray[ k ].getName() + "'s area is " + threeDArray[ k ].area() );
  System.out.println( threeDArray[ k ].getName() + "'s volume is " + threeDArray[ k ].volume() );
}

}
}

 

Revise this Paste

Your Name: Code Language: