2017-04-23 147 views
0

我是一名工程學學生,忙於使用DrJava作爲IDE(它是我們在課程期間使用的標準IDE)以及普林斯頓STDLIB的項目。無法對對象執行操作

我一直有理解,寫作和使用對象的問題。我想問一下我寫下我的代碼的方式有什麼問題。編碼後,我會得到錯誤的行:

public class GameObject 
{ 
// Default implementation of a game object 

private double G = 6.67408e-11; 
private double radiusKoeff = 0.01; 

public class Planet 
{ 
    double mass; 
    double size; 
    double velocityX; 
    double velocityY; 
    double positionX; 
    double positionY; 

    public Planet(double m, double vx, double vy, double px, double py) 
    { 
    mass = m; 
    size = m * radiusKoeff; 
    velocityX = vx; 
    velocityY = vy; 
    positionX = px; 
    positionY = py; 
    }//constructor for the planet type 

    public double GravForce(Planet a, Planet b) 
    { 
    double distanceX, distanceY, distance; 
    distanceX = Math.abs(a.positionX - b.positionX); 
    distanceY = Math.abs(a.positionY - b.positionY); 
    distance = Math.sqrt((distanceX)*(distanceX) + (distanceY)*(distanceY)); 

    double force = (G * a.mass * b.mass)/(distance*distance); 

    return force; 
    }//calculates the gravitational force between two objects  
} 

public static void main(String[] args) 
{ 
    String filename = args[0]; 

    Planet first = new Planet(1.25e24, 1, 0, 0, 0); 
    Planet second = new Planet(1e24, 1, 0, 5, 0); 

    **StdOut.println(GravForce(first, second));** 
} 
} 

錯誤:方法GravForce(GameObject.Planet,GameObject.Planet)是未定義的類型遊戲對象。

我嘗試調用GravForce函數引發該錯誤。

任何幫助將不勝感激。

回答

0

修改你的方法是這樣

public double GravForce(Planet b) 
    { 
    double distanceX, distanceY, distance; 
    distanceX = Math.abs(this.positionX - b.positionX); 
    distanceY = Math.abs(this.positionY - b.positionY); 
    distance = Math.sqrt((distanceX)*(distanceX) + (distanceY)*(distanceY)); 

    double force = (G * this.mass * b.mass)/(distance*distance); 

    return force; 
    }//calculates the gravitational force between two objects 

然後在main

public static void main(String[] args) 
{ 
    String filename = args[0]; 

    Planet first = new Planet(1.25e24, 1, 0, 0, 0); 
    Planet second = new Planet(1e24, 1, 0, 5, 0); 

    StdOut.println(first.GravForce(second)); 
} 
相關問題