Thursday, September 15, 2011

Abstract Classes

An abstract class for a three dimensional object (ThreeDObject) and two concrete classes (Sphere and Cylinder) derived from that abstract class. The abstract class defines the method getSurfaceToVolumeRatio() which uses the abstract methods getSurfaceArea() and getVolume(). Any ThreeDObject derived class can have it's own mechanism to calculate surface area and volume.

browse

package org.megha.blog.example.part6;

/** 
 * A 3-Dimensional object, which has a surface area, volume and
 * a surface area to volume ratio. This abstract class has a
 * concrete surface-area to volume ratio method implementation
 * which depends on the object specific surface-area and volume
 * definitions.
 */
public abstract class ThreeDObject {

    public abstract double getSurfaceArea();
    public abstract double getVolume();

    public double getSurfaceToVolumeRatio() {
        return getSurfaceArea()/getVolume();
    }
}

package org.megha.blog.example.part6;

/** A sphere with a volume and surface area */
public class Sphere extends ThreeDObject {

    double radius;

    public Sphere(double radius) {
        this.radius = radius;
    }

    @Override
    public double getSurfaceArea() {
        return (4 * Math.PI * radius * radius);
    }

    @Override
    public double getVolume() {
        // note that doing 4 / 3 * Math.PI * radius * radius * radius
        // yields a differnt result as
        // 4 / 3 (integer division) equals 1
                return (4 * Math.PI * radius * radius * radius) / 3;
        }

        @Override
        public String toString() {
                return "sphere radius(" + radius + ")";
        }
}

package org.megha.blog.example.part6;

/** A Cylinder 3-D object */
public class Cylinder extends ThreeDObject {

    private double height;
    private double radius;

    public Cylinder(double height, double radius) {
        this.height = height;
        this.radius = radius;
    }

    @Override
    public double getSurfaceArea() {
           return 2 * Math.PI * radius * (radius + height);
    }

    @Override
    public double getVolume() {
        return Math.PI * radius * radius * height;
    }

    @Override
    public String toString() {
        return "cylinder radius(" + radius + "), height(" + height + ")";
    }
}

package org.megha.blog.example.part6;

/** Creates some ThreeDObjects and prints their details */
public class Main {

    private static void printSurfaceAreaRatio(ThreeDObject obj) {
        System.out.println("------------------");
        System.out.println("for object " + obj);
        System.out.println("surface area " + obj.getSurfaceArea());
        System.out.println("volume " + obj.getVolume());
        System.out.println("surface area to volume ratio " + obj.getSurfaceToVolumeRatio());
    }

    public static void main(String args[]) {
        printSurfaceAreaRatio(new Sphere(1));
        printSurfaceAreaRatio(new Sphere(2));
        printSurfaceAreaRatio(new Cylinder(1, 1));
        printSurfaceAreaRatio(new Cylinder(1, 2));
        printSurfaceAreaRatio(new Cylinder(2, 1));
        printSurfaceAreaRatio(new Cylinder(2, 2));
    }
}

No comments:

Post a Comment