1. 程式人生 > >Java 抽象類與繼承訓練

Java 抽象類與繼承訓練

(1) 請運用面向物件程式設計思想編寫完成幾何物件的面積計算程式,

具體要求為:

  • GeometricObject(抽象類)、Circle、Cylinder和Rectangle之間的繼承關係如下圖所示,

  • 請分按類圖分別實現這四個類;
  • 然後編寫一測試類GeoAreaSum,在該類中編寫一個方法求陣列中所有幾何物件的面積之和,其方法頭為:public static double sumArea(GeometricObject[ ] a),
  • 再在GeoAreaSum的main方法中建立一個包括三個物件(一個半徑為5的圓、一個半徑為10、高為5的圓柱體和一個寬和高分別為6和8的矩形)的GeometricObject陣列,
  • 使用sumArea方法計算出它們的總面積並在螢幕上列印輸出。
package geo;
abstract class GeometricObject{
    public abstract double findeArea();
    public abstract double findPerimeter();
}
class Rectangle extends GeometricObject{
    private double w,h;
    public Rectangle(double w,double h){
        this.w=w;this.h=h;
    }
    @Override
    public double findeArea() {
        return w*h;
    }

    @Override
    public double findPerimeter() {
        return (w+h)*2;
    }
}
class Circle extends GeometricObject{
    private double r;
    public Circle(double r){
        this.r=r;
    }
    public double getR(){
        return r;
    }
    @Override
    public double findPerimeter() {
        return Math.PI*r*2;
    }

    @Override
    public double findeArea() {
        return Math.PI*r*r;
    }
}
class Cylinder extends GeometricObject{
    private double r,h;
    public Cylinder(double r,double h){
        this.r=r;
        this.h=h;
    }

    @Override
    public double findeArea() {
        return Math.PI*r*r*h;
    }

    @Override
    public double findPerimeter() {
        return 2*Math.PI*r*r*+2*Math.PI*r*h;
    }
}
public class GeoAreaSum {
    public static double sumArea(GeometricObject[] a){
        double sum=0;
        for(int i=0;i<a.length;i++){
           sum+=a[i].findeArea();
        }
        return sum;
    }
    public static void main(String[] args){
        GeometricObject t[]={new Circle(5),new Cylinder(10,5),new Rectangle(6,8)};
        System.out.println("The sum of area is "+sumArea(t));
    }
}

另請思考如果將Cylinder與GeometricObject的繼承關係改成與Cylinder與Circle的繼承關係,同時將Cylinder中的私有變數r去掉,Cylinder又該如何編寫?

 

package geo;
abstract class GeometricObject{
    public abstract double findeArea();
    public abstract double findPerimeter();
}
class Rectangle extends GeometricObject{
    private double w,h;
    public Rectangle(double w,double h){
        this.w=w;this.h=h;
    }
    @Override
    public double findeArea() {
        return w*h;
    }

    @Override
    public double findPerimeter() {
        return (w+h)*2;
    }
}
class Circle extends GeometricObject{
    private double r;
    public Circle(double r){
        this.r=r;
    }
    public double getR(){
        return r;
    }
    @Override
    public double findPerimeter() {
        return Math.PI*r*2;
    }

    @Override
    public double findeArea() {
        return Math.PI*r*r;
    }
}
class Cylinder extends Circle{
    private double h;
    public Cylinder(double r,double h){
        super(r);
        this.h=h;
    }

    @Override
    public double findeArea() {
        return Math.PI*super.getR()*super.getR()*h;
    }

    @Override
    public double findPerimeter() {
        return 2*Math.PI*super.getR()*super.getR()*+2*Math.PI*super.getR()*h;
    }
}
public class GeoAreaSum {
    public static double sumArea(GeometricObject[] a){
        double sum=0;
        for(int i=0;i<a.length;i++){
           sum+=a[i].findeArea();
        }
        return sum;
    }
    public static void main(String[] args){
        GeometricObject t[]={new Circle(5),new Cylinder(10,5),new Rectangle(6,8)};
        System.out.println("The sum of area is "+sumArea(t));
    }
}