java program! This is a simple prototype program to predict the trajectory of a
ID: 670025 • Letter: J
Question
java program!
This is a simple prototype program to predict the trajectory of a projectile under ideal conditions. You may assume that the projectile will have a constant velocity as it travels and that the only outside effect on the projectile will be due to the force of gravity. Thus the formula used to compute the time that it takes for the projectile to reach its target is:
The distance the projectile is off the ground at the target's distance is calculated using the formula:
You will receive the following information from the user:
projectile velocity (feet / sec)
angle of elevation (degrees)
distance from target (feet)
target size (height of target in feet)
elevation of target (to bottom of target in feet)
All values for one shot will be entered on one line of input. The program will continue reading input values until the user enters zeros for all values. Use the value of 32.17 as the constant for Gravity.
For each input set of values your program should indicates one of the following responses:
The target was hit by the projectile
The projectile was too low, height was: #.### feet
The projectile was too high, height was: #.### feet
The computed distance was too short to reach the target
This last message should be printed if the height was less then zero. The height should be printed using three digits to the right of the radix point. Example:
Error handling: If a calculation cannot be performed (ie: a divide by zero would occur) your program needs to alert the user using the message:
Explanation / Answer
ANSWER:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
public class ProjectileMotion {
public static void main(String[] args) throws IOException {
double vel , angle , dist , size ,ele;
System.out.println("Enter the Values in Single Line :");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] arr = br.readLine().split("\s+");
vel = Double.parseDouble(arr[0]);
angle = Double.parseDouble(arr[1]);
dist = Double.parseDouble(arr[2]);
size = Double.parseDouble(arr[3]);
ele = Double.parseDouble(arr[4]);
DecimalFormat df = new DecimalFormat("#.000");
double radian , time , height;
do{
radian = DegToRed(angle);
System.out.println("The projectile's velocity is "+ df.format(vel)+ " feet per second");
System.out.println("The angle of elevation is "+df.format(angle)+" degrees");
System.out.println("The distance to the target is "+df.format(dist)+" feet");
System.out.println("The target's size is "+df.format(size)+" feet");
System.out.println("The target is located "+df.format(ele)+" feet above the ground");
if(vel * Math.cos(angle) !=0)
{
time = CalTime (vel, radian, dist);
height = CalHeight (vel, radian, time);
if (height >= ele && height <= ele + size)
System.out.println ("The target was hit by the projectile");
else if (height >= 0 && height < ele)
System.out.println ("The projectile was too low, height was: " + df.format (height) + " feet");
else if (height >= ele + size)
System.out.println ("The projectile was too high, height was: " + df.format (height) + " feet");
else
System.out.println ("The computed distance was too short to reach the target");
}
else
{
System.out.println ("The computed distance cannot be calculated with the given data");
}
System.out.println("Enter the Values in Single Line :");
arr = br.readLine().split("\s+");
vel = Double.parseDouble(arr[0]);
angle = Double.parseDouble(arr[1]);
dist = Double.parseDouble(arr[2]);
size = Double.parseDouble(arr[3]);
ele = Double.parseDouble(arr[4]);
}while(check(vel , angle , dist , size ,ele));
}
public static boolean check(double vel , double angle ,double dist , double size, double ele)
{
if(vel ==0 && angle ==0 && dist ==0 && size==0 && ele==0)
return false;
else
return true;
}
public static double DegToRed(double angle)
{
double aa = (angle * Math.PI / 180.0);
return aa;
}
public static double CalTime (double vel, double angle, double dist) {
double bb= (dist / (vel * Math.cos (angle)));
return bb;
}
public static double CalHeight (double vel, double angle, double time) {
double cc= (vel * time * Math.sin (angle) - (32.17 * time * time) / 2.0);
return cc;
}
}
OUTPUT:
Enter the Values in Single Line :
2 10 55.6 4.5 100
The projectile's velocity is 2.000 feet per second
The angle of elevation is 10.000 degrees
The distance to the target is 55.600 feet
The target's size is 4.500 feet
The target is located 100.000 feet above the ground
The computed distance was too short to reach the target
Enter the Values in Single Line :
0 0 0 0 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.