※J.Y Chen 的個人部落格 ※

Just Follow Our Heart And We will shine!

61 瀏覽人次

【程式語言】JAVA的第七堂課 |物件導向程式設計 (homework)

Published: in JAVA by .

hw8_44.java

public class hw8_44 {
    public static void main(String[] args) {
        CSphere cal = new CSphere();
        cal.setLocation(3, 4, 5);
        cal.setRadius(1);
        System.out.println("Circle surfaceArea: " + cal.surfaceArea());
        System.out.printf("Circal volume: %.2f \n" ,cal.volume());
        cal.showCenter();
    }
}

CSphere.java

public class CSphere {
    private int x ;
    private int y ;
    private int z ;
    private int radius;
    private final double pi = 3.14;
    
    void setLocation (int x , int y , int z ) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    void setRadius(int r) {
        this.radius = r;
    }

    double surfaceArea() {
        return 4*pi*radius*radius;
    }

    double volume() {
        return ((pi*4)/3) * radius * radius * radius;
    }

    void showCenter() {
        System.out.print("Circel Center: " + this.x + "," + this.y + "," + this.z + "");
    }

}

result:

©2019 - 2024 Henry Chen