2017-04-23 112 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)); 
} 
相关问题