Write a class called Projectile to simulate ballistic motion. Add methods for computing the range, height and time of flight for the projectile. For writing the simulation, your program will take a user input for the velocity of projection, vi (m/s). Further, take a user input for the length of each interval of the angle of projection, θ (degrees).The program should then simulate the projectile motion for values of θ between 0 and 90 degrees as per the user specified intervals. After writing all the numerical values of θ, range, time of flight and height to an Excel file, your program has to compute and print the following:
The range, R is the horizontal distance between the point of launch and the point of landing of a projectile:
The height, H is the maximum vertical displacement of the projectile during its motion:
The total time of flight, T of the projectile is given by:
WHERE:
To use the sin
function in Java, the angle should be in radians. To convert the angle, θ (in degrees) to radians:
double angle = Math.toRadians(θ);
To illustrate the use of the sin
function in Java, the method to compute the maximum vertical displacement of the projectile is written below:
public double Height()
{
return ((Math.pow(v, 2) * Math.pow(Math.sin(angle), 2))) / (2 * g);
}
import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.FileWriter;
import java.io.IOException;
public class ProjectileWriteToFile
{
public static void main(String[] args)
{
System.out.println("Modeling Projectile Motion");
Scanner scanner = new Scanner(System.in);
System.out.println("Velocity of Projection in meters/second?");
double velocity = scanner.nextDouble();
Projectile object = new Projectile();
object.setVelocity(velocity);
System.out.println("Length of Each Interval in Degrees?");
double degrees_interval = scanner.nextDouble();
DecimalFormat decimalformat = new DecimalFormat("0.00");
double max_height = -100, max_range = -100;
double max_height_angle = 0, max_range_angle = 0;
double height_at_max_range = 0;
try {
FileWriter filewriter = new FileWriter("File.xls");
filewriter.write("Degrees" + "\t\t" + "Height" + "\t\t" + "Range\n");
filewriter.write("\t\t" + "(m)" + "\t\t" + "(m)" + "\t\t" + "(s)\n");
for (double i = 0; i <= 90; i = i + degrees_interval) {
object.setAngle(i);
double h = object.Height();
double r = object.Range();
if (h > max_height) {
max_height = h;
max_height_angle = i;
}
if (r > max_range) {
max_range = r;
max_range_angle = i ;
height_at_max_range = h;
}
filewriter.write(i + "\t\t" + decimalformat.format(object.Height()) +
"\t\t" + decimalformat.format(object.Range()) + "\n");
}
filewriter.close();
System.out.println("End of Data Collection");
System.out.println("Maximum Height is found at " +
decimalformat.format(max_height_angle) + " degrees");
System.out.println("Maximum Range is found at " +
decimalformat.format(max_range_angle) + " degrees");
System.out.println("Value of height when the range is maximum = " +
decimalformat.format(height_at_max_range) + " meters");
} catch (IOException e) {}
}
}
Modeling Projectile Motion
Velocity of Projection in meters/second?
> 25
Length of Each Interval in Degrees?
> 9
End of Data Collection
Maximum Height is found at 90.00 degrees
Maximum Range is found at 45.00 degrees
Value of height when the range is maximum = 15.94 meters
Degrees Height Range
(m) (m)
0 0 0
9 0.78 19.71
18 3.05 37.49
27 6.57 51.60
36 11.02 60.65
45 15.94 63.78
54 20.87 60.65
63 25.32 51.60
72 28.84 37.49
81 31.11 19.71
90 31.89 0