2016-03-08 68 views
1

這是怎麼回事, 我一直收到這個錯誤,找不到符號Circle aCircle = new Circle();,當試圖編譯我的教授給我們的驅動程序代碼。我想知道是不是因爲我沒有將它添加到我的circle.java方法中。這是圈子驅動程序。錯誤找不到符號--Java類和方法

package lab7; 

public class CircleDriver { 

     public static void main(String[] args) { 

      Circle aCircle = new Circle(); 

      aCircle.setColor("green"); 
      aCircle.setRadius(10); 
      aCircle.display(); 

      Double circleArea = aCircle.computeArea(); 
      Double circumference = aCircle.computeCircumference(); 

      System.out.println("circle area: " + circleArea); 
      System.out.println("circle circumference: " + circumference); 
      System.out.println(); 
      } 

} 

這是我的圈子method.`

public class Circle { 

    private String color; 
    private int radius; 

    public Circle(String color, int radius) { 
     this.color = color; 
     this.radius = radius; 
    } 

    public Circle() { 
    Circle aCircle = new Circle(); 

    } 

    public String getColor() { 
     return color; 
    } 

    public void setColor(String color) { 
     this.color = color; 
    } 

    public int getRadius() { 
     return radius; 
    } 

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

    public void display() { 
     System.out.println("I am a circle"); 
     System.out.println("My color is " + color); 
     System.out.println("My radius is " + radius); 
    } 

    public double computeArea() { 
     return (Math.PI * Math.pow(radius, 2)); 
    } 

    public double computeCircumference() { 
     return (2 * Math.PI * radius); 
    } 

} 
+0

CircleDriver和Cricle在同一個包中?如果沒有,你是否將Circle導入CircleDriver? –

+0

我的教授希望我們運行CircleDriver並使其運行圓圈以找出區域和周長。對不起,如果我不明確 –

+0

檢查類是在相同的包裝空間。如果沒有,你將需要將你的Circle類導入到CircleDriver類中以供使用:'import full.qualified.path.to.Circle;' 哦,並且你不需要Circle中的空構造函數實例化另一個Circle。這可能會導致StackOverflow異常。 – ManoDestra

回答

0

使用此代碼。你在構造函數中有錯誤。我希望它能解決你的問題。

Circle.java,而不是

public Circle() { 
Circle aCircle = new Circle(); 
} 

使用此代碼

public Circle() { 
    super(); 
    // TODO Auto-generated constructor stub 
} 

CircleDriver.java

public class CircleDriver { 
    public static void main(String[] args) { 
     Circle aCircle = new Circle(); 
     aCircle.setColor("green"); 
     aCircle.setRadius(10); 
     aCircle.display(); 
     Double circleArea = aCircle.computeArea(); 
     Double circumference = aCircle.computeCircumference(); 
     System.out.println("circle area: " + circleArea); 
     System.out.println("circle circumference: " + circumference); 
     System.out.println(); 
    } 
} 

Circle.java

public class Circle { 

    private String color; 
    private int radius; 

    public Circle() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    public Circle(String color, int radius) { 
     super(); 
     this.color = color; 
     this.radius = radius; 
    } 

    public String getColor() { 
     return color; 
    } 

    public void setColor(String color) { 
     this.color = color; 
    } 

    public int getRadius() { 
     return radius; 
    } 

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

    public void display() { 
     System.out.println("I am a circle"); 
     System.out.println("My color is " + color); 
     System.out.println("My radius is " + radius); 
    } 

    public double computeArea() { 
     return (Math.PI * Math.pow(radius, 2)); 
    } 

    public double computeCircumference() { 
     return (2 * Math.PI * radius); 
    } 

} 

輸出:

I am a circle 
My color is green 
My radius is 10 
circle area: 314.1592653589793 
circle circumference: 62.83185307179586 
+1

你剛剛複製並粘貼他的代碼? –

+0

@RahulSharma不,我改變了構造函數。 – SkyWalker

+0

任何代表點,呃?這可能只是一個評論, –

1

你需要調用圈時,將調用超()構造函數。當您致電

Circle aCircle = new Circle(); 

您正試圖在本地方面初始化一個圓。我想你試圖繼承已經在java中的Circle類。

離開圈構造函數

public Circle() {} 

理論上應該合作,以實例化類。

+0

感謝您的幫助。 –