Scientific Programming II

ModSim

Midterm


Problem 1 (20 Points)

Using similar skills to the Particle applet, write an applet (using a new class named Midterm) that simulates five Projectiles in ballistic (unpowered) flight. The Projectiles should be launched at 15.0m/s and use the following angles:

Your applet should be 600x600px. You should include the following instance variables:

You may optionally also have the following constant for the size of the applet:

private static final int SIZE = 600;

You should also include implementations for the following methods:

You are only required to have the following import statements:

Additionally, if you'd like to use a LinkedList, use the following import statement:

As before, roughly ~100ms will give a smooth animation when paired with an appropriate update to the time variable. Again, make sure you're using the correct package (or change it) when copy-and-pasting.

package edu.govschool;

public class Projectile 
{
    // The initial velocity of the projectile
    private double initVelo;
    // The initial angle fired
    private double theta;
    // Gravity, or, THE LAW
    private static final double GRAVITY = 9.81;
    
    /**
     * Default constructor.
     * Creates a new <code>Projectile</code>
     * @param initVelo the initial velocity in m/s
     * @param theta the initial angle fired in degrees
     */
    public Projectile(double initVelo, double theta)
    {
        this.initVelo = initVelo;
        // The trig functions need our angle in radians
        this.theta = Math.toRadians(theta);
    }
    
    /**
     * Gets the current x-coordinate.
     * @param time the current time
     * @return the current x-coordinate
     */
    public int getX(double time)
    {
        return (int) (this.initVeloX() * time);
    }
    
    /**
     * Gets the current y-coordinate.
     * @param time the current time
     * @return the current y-coordinate
     */
    public int getY(double time)
    {
        return (int) ((this.initVeloY() * time) - (0.5 * GRAVITY * Math.pow(time, 2)));
    }
    
    /**
     * Calculates the initial velocity vector in the x-direction
     * @return the magnitude of the x-direction vector
     */
    private double initVeloX()
    {
        return initVelo * Math.sin(theta);
    }
    
    /**
     * Calculates the initial velocity vector in the y-direction
     * @return the magnitude of the y-direction vector
     */
    private double initVeloY()
    {
        return initVelo * Math.cos(theta);
    }
}

Helpful Hints

Hint 1

The x- and y-coordinates generated by Projectile are relatively small vis-à-vis the size of our applet. Therefore, it is nice to multiply the coordinate generated by Projectile in both the x- and y-directions by a constant multiplier to increase them. This will give a much easier to see animation. The following code snippet gives a good constant to multiply by:

private static final int MULT = 12;
Hint 2

As we know, the coordinates of our applet are Cartesian-based, but in the fourth quadrant, ergo, y increases as we move down. In order to have our Projectiles animate in a realistic fashion, use the following psuedocode:

// The '- 20' is to be above the 'Applet started' status bar
y-coord = (SIZE - 20) - (generated y-coord with multiplier)