更新实例方法问题

时间:2016-02-19 01:34:20

标签: java

无法在currentTime方法中将updateField更新一秒,从而触发currentVelocitycurrentPosition公式。这一切都在tracking方法中进行了合作。

非常感谢任何帮助。 (旁注,我刚刚开始学习如何在一个月前用java编程,所以不要打扰我太多。)

public class FallingItem {

    private double INITIAL_POSITION = 0;
    private double INITIAL_VELOCITY = 0;
    private int currentTime = 0;
    private double currentPosition = 0;
    private double currentVelocity = 0;

    public static final double TERMINAL_VELOCITY = -500;

    public FallingItem (double initialVelocity, double initialPosition){

        INITIAL_VELOCITY = initialVelocity; 
        currentVelocity = initialVelocity;
        INITIAL_POSITION = initialPosition;
        currentPosition = initialPosition;
        currentTime = 0;
    }

    public double getCurrentTime () {

        return currentTime;
    }

    public double getCurrentPosition () {

        return currentPosition;
    }
    public void updateFields () {

        currentTime++;      

        this.currentVelocity = -32 * this.currentTime + INITIAL_VELOCITY;

        if (this.currentVelocity < TERMINAL_VELOCITY){

            this.currentVelocity = TERMINAL_VELOCITY;
        } 

        if (this.currentVelocity <= TERMINAL_VELOCITY){

            this.currentPosition = this.currentPosition - TERMINAL_VELOCITY;
        }

        else {

            this.currentPosition = -16 * Math.pow(this.currentTime, 2) + currentVelocity
            * this.currentTime + this.currentPosition;
        }
    }   

    public void tracking () {

        System.out.println ("Object released at " + INITIAL_POSITION + " feet, at"
        + " an initial velocity of " + INITIAL_VELOCITY + " feet/sec.");

        while (this.currentVelocity >= TERMINAL_VELOCITY) {
            System.out.println ("At " + this.currentTime + " seconds, position is " + this.currentPosition
            + " feet and velocity is " + this.currentVelocity + " feet/sec");

        }    
    }
}

1 个答案:

答案 0 :(得分:0)

经过一些修改:

package general;

public class DummyTesting {

private double INITIAL_POSITION = 0;
private double INITIAL_VELOCITY = 0;
private long currentTime = 0;
private double currentPosition = 0;
private double currentVelocity = 0;

public static final double TERMINAL_VELOCITY = -500;

public DummyTesting (double initialVelocity, double initialPosition){
    INITIAL_VELOCITY = initialVelocity; 
    currentVelocity = initialVelocity;
    INITIAL_POSITION = initialPosition;
    currentPosition = initialPosition;
    currentTime = 0;
}

public long getCurrentTime () {
    return System.currentTimeMillis() / 1000L;
}

public double getCurrentPosition () {
    return currentPosition;
}
public void updateFields () {
    currentTime = this.getCurrentTime() + 1;      
    this.currentVelocity = -32 * this.currentTime + INITIAL_VELOCITY;
    if (this.currentVelocity < TERMINAL_VELOCITY){
        this.currentVelocity = TERMINAL_VELOCITY;
    } 

    if (this.currentVelocity <= TERMINAL_VELOCITY){
        this.currentPosition = this.currentPosition - TERMINAL_VELOCITY;
    }
    else {
        this.currentPosition = -16 * Math.pow(this.currentTime, 2) + currentVelocity
        * this.currentTime + this.currentPosition;
    }
    System.out.println("System parameters: CurrentPosition:" + currentPosition + " Current Time:" + currentTime + " currentVelocity:" + currentVelocity);
}   

public void tracking () {
    System.out.println ("Object released at " + INITIAL_POSITION + " feet, at"
    + " an initial velocity of " + INITIAL_VELOCITY + " feet/sec.");

    while (this.currentVelocity >= TERMINAL_VELOCITY) {
        System.out.println ("At " + this.currentTime + " seconds, position is " + this.currentPosition
        + " feet and velocity is " + this.currentVelocity + " feet/sec");
    }    
}

public static void main(String[] args){
    DummyTesting dt = new DummyTesting(-70, 40);
    dt.updateFields();
}

}

输出 系统参数:CurrentPosition:540.0当前时间:1455848058 currentVelocity:-500.0