2015-03-02 33 views
0

我一直在爲學習目的創建一個類測試類型的程序,但目前我堅持如何解決我的源代碼。我寧願有人解釋它,因爲我只想了解這個新手問題。有關私人訪問錯誤的Java測試程序

/* 
* File: polygon.java 
* Author: M. Morales 
* Date: March 1, 2015 
* Purpose: Sets the foundation for the polygon 
* test 
*/ 

public class polygon { 

    // polygon class has 4 fields 
    private int numSides; 
    private double sideLength; 
    private double xCoord; 
    private double yCoord; 

    // Default constructor 
    public polygon() { 
     numSides = 4; 
     sideLength = 10.0; 
     xCoord = 0.0; 
     yCoord = 0.0; 
    } 

    // constructor 
    public polygon (double psideLength, double px, double py, int pnumSides) { 
     numSides = pnumSides; 
     sideLength = psideLength; 
     xCoord = px; 
     yCoord = py; 
    } 

    // Setter methods 
    // setnumSides 
    private void setnumSides(int pnumSides) { 
     numSides = pnumSides; 
    } 
    // setsideLength() 
    private void setsideLength(double psideLength) { 
     sideLength = psideLength; 
    } 
    // setxCoord() 
    private void setxCoord(double px) { 
     xCoord = px; 
    } 
    // setyCoord() 
    private void setyCoord(double py) { 
     yCoord = py; 
    } 


    // Getter methods 
    // getnumSides 
    public double getnumSides() { 
     return numSides; 
    } 
    // getsideLength 
    public double getsideLength() { 
     return sideLength; 
    } 
    // getxCoord 
    public double getxCoord() { 
     return xCoord; 
    } 
    // getyCoord 
    public double getyCoord() { 
     return yCoord; 
    } 

    // Use Perimeter method to get the distance around 
    public double getperiMeter(polygon s1) { 
     // perimeter 
     double periMeter = Math.abs(s1.getnumSides() * s1.getsideLength()); 
     return periMeter; 
    } 


    // toString method 
    public String toString() { 
     String str = "(" + numSides + ", " + sideLength + "," + xCoord + "," 
     + yCoord + ")"; 
     return str; 
    } 

} 

以上是第一部分,但測試是什麼將不編譯我

/* 
* File: TestPolygon.java 
* Author: M. Morales 
* Date: March 1, 2015 
* Purpose: creates simplistic polygon perimeter 
* test 
*/ 

public class TestPolygon2 { 
    public static void main(String[] args) { 

     int numSides = 4; 

     double sideLength = 10.0; 

     double xCoord = 0.0; 

     double yCoord = 0.0; 


     //Construct a polygon 
     polygon s1 = new polygon(); 


     s1.setnumSides(numSides); 

     // Call the getter methods 
     int s1numSides = s1.getnumSides(); 
     double s1sideLength = s1.getsideLength(); 
     double s1xCoord = s1.getxCoord(); 
     double s1yCoord = s1.getyCoord(); 
     // Print results 
     System.out.println("s1 values from getnumSides() getsideLength() getxCoord() getyCoord " + s1numSides + "," + s1sideLength + "," + s1xCoord + "," + s1yCoord); 

     // Call the Perimeter Method 
     double periMeter = s1.getperiMeter(s1); 
     // Print results 
     System.out.println("The perimeter of the polygon is: " + 
     periMeter); 

     // Change the value of s1 
     // Using the setter method 
     int newnumSides = 8; 
     double newsideLength = 11.0; 
     double newxCoord = 2.0; 
     double newyCoord = 2.0; 
     s1.setnumSides(newnumSides); 
     s1.setsideLength(newsideLength); 
     s1.setxCoord(newxCoord); 
     s1.setyCoord(newyCoord); 

     // Recalculate the Distance 
     periMeter = s1.getperiMeter(s1); 
     // Print results 
     System.out.println("New perimeter is: " + 
     periMeter); 
     // Display the values using toString 
     System.out.println(s1.toString()); 



    } 
} 

+0

你得到的錯誤是什麼?此外,修復您的縮進以獲得更好的可讀性。 – nomis 2015-03-02 09:44:35

+0

可能有損耗從雙倍轉換爲整數。另外,我設置的東西在多邊形中有私人訪問 – 2015-03-02 10:22:26

回答

1

polygon類的set*方法private,所以你不能從他們叫你TestPolygon2類。您必須將它們更改爲public以便能夠呼叫它們。

+0

使它們成爲私有包就足夠了(假設測試在標準的相同包中)。 – assylias 2015-03-02 09:45:43

+0

@assylias這取決於'polygon'實例是否應該能夠通過包的外部的類進行變異 – Eran 2015-03-02 09:47:52

+0

這個特定問題的全部內容是在它們是私有的時候操縱它們。如果它們被公開,它會容易得多。 – 2015-03-02 10:24:51