以下的有两个文件。这是Projectile.java
package au.edu.jcu.cp2004.model;import java.text.DecimalFormat;
/**
   Set all required variables: GRAVITY, currentTime,
   initialVelocity, currentVelocity, position and exactPosition
**/
public class Projectile {    static DecimalFormat df = new DecimalFormat("#.###");
    static final double GRAVITY = 9.81;
    double currentTime = 0;
    double initialVelocity;
    double currentVelocity;
    double position;
    double exactPosition = 0;
    /**
   Creates a Projectile object
   Initialize all the variables declared
   Using this. can call another constuctor
**/
public Projectile(double initialVelocity) {
        this.initialVelocity = initialVelocity;
        this.currentVelocity = currentVelocity;
        currentVelocity = initialVelocity;
        this.position = position;
        position = 0;
    }
public void move(double timeInterval) {
    position = position + (currentVelocity * timeInterval);
    // Calulate position by using the formula: sc = sc + (vc *  t) //
        
    currentVelocity = currentVelocity - (GRAVITY * timeInterval);
    // Calulate currentVelocity by using the formula: vc = vc - (g *  t) //
        
    currentTime += timeInterval;    
    // Calulate currentTime by using the formula: ct = ct + ti //
    }
public void calculateBallisticFormulaPosition() {
        exactPosition = (-0.5 * GRAVITY * currentTime * currentTime) + (initialVelocity * currentTime);
    } // Calculate exactPosition [ use formula st = -(0.5 * g * t) + (v * t) //
public double getCurrentVelocity() {
        return currentVelocity;
    } // Get the current velocity of Projectile, return the double value of currentVelocity //
private double getPosition() {
        return position;
    } // Get the current positon pf Projectile, return the doulbe value of positon //  
public void printPositionAndVelocity() {
     System.out.println("After " + df.format(currentTime)
           + " secs: Simulation position: " + df.format(getPosition())
           + " Velocity: " + df.format(getCurrentVelocity()) + " m/sec");
     System.out.println("\tThe ballistic formula position is: " + df.format(exactPosition));
    }
} // Prints both the details of the simulated velocity and position and the 
  // ballistic formula position下面的是Cannon.javapackage au.edu.jcu.cp2004.model;// declare and initialize variables: timeInterval and initialVelocity //
public class Cannon {    double initialVelocity = 50;   // Unit is m/s //
    double timeInterval = 0.0001;  // Unit is s //      /*
      check the length of array if it is two then initialize the 
      variable declared above. otherwise show usage message.
    */    public Cannon(String[] args) {
     System.println ("java au/edu/jcu/cp2004/model/Cannon" + "50" + "0.0001")
        if (args.length == 2) {
         initialVelocity = Double.parseDouble(args[0]);
         timeInterval = Double.parseDouble(args[1]);
        
        else if (args.length !== 2) {
            System.out.println("Usage:java Cannon <inital velocity> <short time interval>");
            System.exit(1);
 
            fireShot();
    }           
    /** create a Projectile object
call printPositionAndVelocity() for reporting position and velocity
make a loop until projectile' current velocity falls below '-initialVelocity'
call move function of projectile
call projectile's calculateBallisticFormulaPosition();
        report position and velocity by calling printPositionAndVelocity()
    **/
    private void fireShot() {
        Projectile projectile = new Projectile(initialVelocity);
        projectile.printPositionAndVelocity();
        while (projectile.getCurrentVelocity() > -initialVelocity) {
            for (int i = 0; projectile.getCurrentVelocity() > -initialVelocity && i < (1/timeInterval); i++) {
                projectile.move(timeInterval);
            }
            projectile.calculateBallisticFormulaPosition();
            projectile.printPositionAndVelocity();
        }
    }
}

解决方案 »

  1.   

    以下是题目要求:Write a Java program that simulates the shooting of a projectile vertically up into the air. Assume that the projectile will be fired straight up into the air (vertically) with a starting velocity of v. Basic calculus tells us that the position s of the projectile after t seconds is:  
    st = -(0.5 * g * t) + (v * t), 
    where g = 9.81 m/sec2, ie the gravitational force of the earth. This is a version of the historic ballistic formula used to calculate the trajectories of projectiles such as artillery shells, and was the basis of one of the first-ever computer applications.The problem, however, is that this formula is an approximation – it is not exact. In fact, the gravitational force exerted by the earth diminishes the farther the projectile is from the earth’s surface. The effect of gravity on the projectile is to constantly change the velocity of the projectile. We can model the constantly changing velocity by using a simulation. We will simulate the motion of the projectile by calculating the projectile’s position and velocity many times per second (say, 100 times, to start with). In doing so, we are considering how the projectile moves in a very short time interval, short enough for the velocity v to be constant for that time period. Each time we calculate the new position the projectile has moved to and the current velocity, we use the updated velocity and position values. We update the current velocity vc using the formula: 
    vc = vc – (g * &#61508;t) 
    where &#61508;t = our short time interval. We also need to calculate how much further the projectile has moved. To do that we need to use the formula:
    sc = sc + (vc * &#61508;t)  The program must consist of two classes: 
    i) Projectile.java, which includes a single constructor and a number of methods:
    a. move(double timeInterval) - used to calculate the current Position and current Velocity of the projectile. This is  where position and velocity are updated;
    b. calculateBallisticFormulaPosition () - used to calculate the Position of the projectile according to a formula for calculating the trajectory;
    c. getCurrentVelocity() – returns the velocity of a projectile at the current moment (measured in m/sec2); 
    d. getPosition() - returns the position of the projectile at the current moment (measured in metres);
    e. printPositionAndVelocity() – prints both the details of the simulated velocity and position and the ballistic formula position (NB it is only necessary to print this information out every 1 second);You might require other methods – the list above is not exhaustive.Numbers displayed should be accurate to three decimal places. (NB, under some operating systems Java has problems with decimal numbers. So, for example, on my machine, 0.1 + 0.2 = 0.300000000004.  Restricting the displayed numbers hides this problem.) 
    ii) Cannon.java which:
    a. Has a single constructor that calls the setInitialValues method and then the fireShot method;
    b. Has a method: setInitialValues() which sets the initial velocity (eg 50). Units do not matter, but it might be easier to think in terms of m/sec/sec. Also sets the short time interval (eg 0.001 secs). These values are entered via the command line.
    c. Has a method: fireShot()  which creates a new projectile object and then uses the projectile’s move() method to simulate it being fired straight up. fireShot() also requires that object to report its position and velocity once every second (by calling the projectile’s printPositionAndVelocity() method) until the object 'falls' back to earth.  Remember that, in the simulation, the position and velocity is calculated many times a second - it is only reported once per second, however.You might require other methods – the list above is not exhaustive. Cannon.java also checks to ensure that two values (initial velocity and short time interval) were input as parameters and prints a usage statement if not.Each class must be appropriately documented, using javadoc and comments as necessary. 
      

  2.   

    To akimoto() :
    没关系啊,帮我看看我写的程序也可以啊。^_^
      

  3.   

    首先, 我们假设这个弹道的轨迹是垂直向上发射, 并且带有一个初始的速度。我们把这个速度设置为v,所以这个基本的在发射的多少时间后这个弹道的位置的公式为:st = -(0.5 * g * t) + (v * t),重力加速度:g = 9.81 m/sec2  我们要做的就是用多次计算弹道的位置和初始速度模拟的就是弹道的运动,我们要考虑的是弹道怎样在一个很短的时间移动。每一次我们计算新的弹道位置和当前的初始速度。然后我们更新着初始速度和位置的值。更新当前初始速度vc ,我们用vc = vc – (g * Dt)  公式。Dt = 间隔时间。我们还需要计算的是这个弹道已经移动了多远。计算公式为:
    sc = sc + (vc * Dt)  呵呵,写到这里大家都明白了一些了吧,都是咱们初中学的公式哦。要求:这个程序应该包括两个类:Projectile.java  包括一个单一的构造器和方法:第一, move(double timeInterval) 用来计算当前弹道位置和当前初始速度。这些都是要更新的。
    第二, calculateBallisticFormulaPosition () 用来根据公式计算轨道位置。
    第三, getCurrentVelocity() 返还在当前的时刻发射的初始速度。
    第四, getPosition() 返还当前时刻发射轨道的位置
    第五, printPositionAndVelocity() 列出模拟出的初始速度和位置的详细信息。你可以用其他以上没有列出的方法来实现。还有一个Cannon.java同上,大家也可以用其他方法来实现。Cannon.java   检查并确保两个数值(初始速度和间隔时间)被作为参数输入。这个程序输出的例子:java au/edu/jcu/cp2004/model/Cannon “50”  “0.0001”After 0 secs: Simulation position: 0 Velocity: 50 m/sec
    The ballistic formula position is: 0
    After 1 secs: Simulation position: 45.095 Velocity: 40.19 m/sec
    The ballistic formula position is: 45.095
    After 2 secs: Simulation position: 80.381 Velocity: 30.38 m/sec
    The ballistic formula position is: 80.38
    After 3 secs: Simulation position: 105.856 Velocity: 20.57 m/sec
    The ballistic formula position is: 105.855
    After 4 secs: Simulation position: 121.522 Velocity: 10.76 m/sec
    The ballistic formula position is: 121.52
    After 5 secs: Simulation position: 127.377 Velocity: 0.95 m/sec
    The ballistic formula position is: 127.375
    After 6 secs: Simulation position: 123.423 Velocity: -8.86 m/sec
    The ballistic formula position is: 123.42
    After 7 secs: Simulation position: 109.658 Velocity: -18.67 m/sec
    The ballistic formula position is: 109.655
    After 8 secs: Simulation position: 86.084 Velocity: -28.48 m/sec
    The ballistic formula position is: 86.08
    After 9 secs: Simulation position: 52.699 Velocity: -38.29 m/sec
    The ballistic formula position is: 52.695
    After 10 secs: Simulation position: 9.505 Velocity: -48.1 m/sec
    The ballistic formula position is: 9.5
    After 10.194 secs: Simulation position: 0.004 Velocity: -50 m/sec
    The ballistic formula position is: -0.001Process finished with exit code 0
    2 不输入任何参数,来运行这个程序:java au/edu/jcu/cp2004/model/Cannon “50”  “0.0001”Usage: java Cannon <inital velocity> <short time interval> Process finished with exit code 1
    最后大家看看我写这两个程序,在一楼呢,帮我改改,Cannon.java 总是有错误,不过能运行,结果也和要求的一样,大家帮忙看看。谢谢了
      

  4.   

    To mingr6370(丑男):呵呵,是作业啊,我已经把这两个程序写出来了,可是都有ERROR,不过Cannon.java最后可以运行出正确的结果,我是想精益求精。希望大侠指点一下哦。其实是很简单的,我都做完了,大侠可以看看一楼我的帖子。你们的指点和建议才使我们这些新手进步的更快哦。