2013-09-27 45 views
0

我正在做一個家庭作業,我確定一個圓柱體的體積。課程的目標是類和對象。我有兩個班,「CylinderTest」&「Cylinder」。氣缸測試稱爲氣缸。除了獲取和設置方法之外,一切似乎都工作得很好。我試圖阻止對負數進行計算,但這不起作用,無論如何執行計算。Java getter和setter不工作

這裏是CylinderTest類

public class CylinderTest 
{ 

    public static void main(String[] args) 
    { 
     Cylinder myTest = new Cylinder(-1, -1); 
     myTest.getHeight(); 
     myTest.getRadius(); 
     System.out.println(myTest); 

     printHeader(); 
     double volume = myTest.volume(); 
     displayCylinder(volume); 
    } 

    private static void printHeader() 
    { 
     System.out.println("Cylinder"); 
     System.out.println("________"); 
    } 

    private static void displayCylinder(double volume) 
    { 
     System.out.print("Cylinder volume = "); 
     System.out.println(volume); 
    } 
} 

這裏是Cylinder類

public class Cylinder 
{ 
    // variables 
    public static final double PI = 3.14159; 
    private double radius, height, volume; 

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

    // Volume method to compute the volume of the cylinder 
    public double volume() 
    { 
     return PI * radius * radius * height; 
    } 

    // accessors and mutators (getters and setters) 
    public double getRadius() 
    { 
     return radius; 
    } 

    public void setRadius(double radius) 
    { 
     if (radius > 0.0) 
      this.radius = radius; 
     else 
      this.radius = 1.0; 
    } 

    public double getHeight() 
    { 
     return height; 
    } 

    public void setHeight(double height) 
    { 
     if (height > 0.0) 
      this.height = height; 
     else 
      this.height = 1.0; 
    } 

    public double getVolume() 
    { 
     return volume; 
    } 

    public void setVolume(double volume) 
    { 
     this.volume = volume; 
    } 

} 
+1

你從來沒有打電話給你的製片人。而且,如果你不想允許負值,那麼爲什麼不在構造函數中驗證同樣的東西呢?此外,你似乎忽略了你的獲得者的回報價值。 –

+0

謝謝Rohit Jain。我將構造函數更改爲這個公共圓柱體(雙半徑,雙高度) { \t setRadius(radius); \t setHeight(height); } – user2802785

回答

0

你的構造函數應該調用你的setter,你應該檢查你的setters中的邏輯。如果調用代碼傳遞負值,你是否真的想繼續使用1的值?

+0

嗨戴夫,謝謝!我沒有打電話給安裝者! DUH!這個怎麼樣: \t公共靜態無效的主要(字串[] args) \t { \t \t缸MYTEST =新的汽缸(-3,-5); \t \t System.out.println(myTest); \t \t printHeader(); \t \t double volume = myTest.volume(); \t \t displayCylinder(volume); \t} \t //構造 \t公共的汽缸(雙半徑,雙倍高度) \t { // \t this.radius =半徑; // \t this.height = height; \t setRadius(radius); \t setHeight(height); \t} – user2802785

+0

我是一個新手,所以我不知道爲什麼該代碼片段沒有格式化。另外,爲了回答您的其他問題,如果代碼傳遞負數,我不想繼續使用值1。教授讓我們這樣做來強調安全性以及制定者和獲取者在這方面的重要性。在現實世界中,我會使用try-catch來強制日期完整性 – user2802785

3

在你的構造函數,你需要使用相同的測試在getter和setter方法,而不是直接設置值。目前,您可以通過new Cylinder(-1,-1)來避開測試人員的測試。

0

你可以擺脫你的構造和使用:

Cylinder myTest = new Cylinder(); 
    myTest.setHeight(-1); 
    myTest.setRadius(-1); 

或者,你可以創建一個「工廠」的方法:

public static Cylinder createCylinder(double radius, double height) 
    { 
     Cylinder tmp = new Cylinder(); 
     tmp.setRadius(radius); 
     tmp.setHeight(height); 
    } 

雖然不推薦,語法,你也可以改變你的構造函數調用setters.it將看起來像這樣:

public Cylinder(double radius, double height) 
{ 
    setRadius(radius); 
    setHeight(height); 
} 

爲什麼這是consid不好的做法,看到這個:Java call base method from base constructor

0

除了在構造函數中執行你的測試,你也沒有設置音量(任何時候都是空的)。

所以,你的構造函數更改爲:

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

而且刪除setVolume(),使setHeight()setRadius()私人。

0

您的setter方法沒有進行驗證,因爲您根本沒有調用它們。正如其他人所評論的,一個好主意是在構造函數中調用它們,而不是直接將值分配給radiusheight

像你一樣初始化氣缸的屬性本身並不正確。但是,由於您需要在輸入上運行「< = 0」驗證,您的設置人員已經實現了這種調用,所以這是一個簡單的解決方案。

一些額外的注意事項不影響你在尋找,但仍然跳出來我的結果:

  • TestCylinder,你叫您的兩個getter方法,但你不分配他們對任何事情。請記住,getters返回一個值,所以有效地自行調用它什麼都不做。
  • 同樣在TestCylinder中,您直接撥打Cylinder.volume(),而不是使用其獲取方法getVolume來獲取柱面的體積。在這裏,我建議將邏輯計算在吸氣劑上並僅使用該方法,或者讓吸氣劑呼叫volume(),以防在Cylinder類的另一部分中需要後者。