多类java模拟

时间:2015-11-17 22:13:50

标签: java

我写了一个类来模拟一个引力场,以及一个使用它的主类。我确定我的方程是正确的,但是当代码编译时,当我运行它时它返回'NaN'。这是我第一次使用多个类,所以我只是想知道我是否正确链接它们: 这是我的GravField类:

import java.lang.Math;

public class GravField{

    public static final double G = 6.674*Math.pow(10,-11);      //defines the constant G    
    private double planetMass;                      
    private double planetRadius;                        
    private double projectileX;
    private double projectileY;
    private double a = planetRadius + projectileY;  

    PhysicsVector projectilePosition = new PhysicsVector(projectileX, a);   //PhysicsVector class was written
    PhysicsVector gravityAcceleration = new PhysicsVector();        //lecturer, so that won't be the problem!

    public GravField(double planetMass, double planetRadius, double projectileX, double projectileY){

        double distance = Math.sqrt(projectileX*projectileX+projectileY*projectileY);               

        double x = (-G*planetMass*projectileX)/(distance*distance*distance);    
        double y = (-G*planetMass*a)/(distance*distance*distance);

        gravityAcceleration.setVector(x, y);        //setVector is a method in the PhysicsVector class      
        gravityAcceleration.print();            //print is also in PhysicsVector
    }

    public double magnitude(){
        double magnitudeOfGravField = gravityAcceleration.magnitude();  
        return magnitudeOfGravField;
    }

    public double componentX(){

        double xComponent = gravityAcceleration.getX();     //gravityAcceleration for some reason can't be 
        return xComponent;                  //directly accessed by main class, so use this instead
    }

    public double componentY(){
        double yComponent = gravityAcceleration.getY();
        return yComponent;
    }
}

然后在我的主要课程中:

import java.util.Scanner;
import java.lang.Math;

public class ParticleSim{

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the size of the time step:");
        double timeStep = scanner.nextDouble();
        System.out.println("Please enter the initial x velocity:");
        double initialXVelocity = scanner.nextDouble();
        System.out.println("Please enter the initial y velocity:");
        double initialYVelocity = scanner.nextDouble();

        PhysicsVector projectilePosition = new PhysicsVector();

        double earthMass = 5.972*Math.pow(10,24);
        double earthRadius = 6371000;

        GravField earth = new GravField(earthMass, earthRadius, 0, 0);

        double y=0.0;
        double x=0.0;
        double yVelocity=initialYVelocity;
        double xVelocity=initialXVelocity;

        do{                 //either this loop or my GravField class is the problem, I think!
            System.out.println("Hi! I'm in the loop!");
            y += yVelocity * timeStep;      //Euler's algorithm to calculate positions and velocities
            x += xVelocity*timeStep;
            yVelocity += earth.componentY() * timeStep;     
            xVelocity += earth.componentX() * timeStep;

            PhysicsVector velocity = new PhysicsVector();
            PhysicsVector position = new PhysicsVector();
            velocity.setVector(xVelocity, yVelocity);
            position.setVector(x, y);

            velocity.print();
            position.print();

            timeStep += timeStep;
        }while(y>0);
    }
}

每个GravField对象都有一个变量gravityAcceleration是对的吗?通过编写earth.componentY(),我要求为地球的gravityAcceleration的Y分量? 如果你设法浏览我的代码,我真的很感激!谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

你确实通过调用earth.componentx()来调用gravityAcceleration的x 但我认为这个缺陷正在推行你的逻辑

double distance = Math.sqrt(projectileX*projectileX+projectileY*projectileY);               
double x = (-G*planetMass*projectileX)/(distance*distance*distance);    
double y = (-G*planetMass*a)/(distance*distance*distance);

GravField的构造函数内部。这只是在你创造地球时被召唤一次。因此,地球以重力加速度开始,0 x和0 y,并且永远不会改变。

你需要移动物体以便它在循环中发生,并且你可以在每一步重新计算距离和射弹的矢量。

答案 1 :(得分:0)

不,GravField本身不会像在PhysicsVector中定义的那样具有变量gravityAccerlation,但是,您可以通过使用例如projectilePosition.gravityAcceleration来访问它;

当你调用earth.componentY()时,你正在访问PhysicsVector的Y值,并将它分配给GravField的Y分量,所以,你确实得到了地球的Y分量。

在ParticleSim类中,你永远不会因为(y> 0)条件而开始循环,因为你已经将y定义为等于0。