有关私人访问错误的Java测试程序

问题描述:

我一直在为学习目的创建一个类测试类型的程序,但目前我坚持如何解决我的源代码。我宁愿有人解释它,因为我只想了解这个新手问题。有关私人访问错误的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

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