********JAVA*******PLEASE COMMENT ALL LINES OF CODE******** This is a simple pro
ID: 670494 • Letter: #
Question
********JAVA*******PLEASE COMMENT ALL LINES OF CODE********
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
//you didnot speciffied how to calculate force of gravity, so fill up the logic in that line. And you didnot specified how to use elevation of target (to bottom of target in feet) and with what distance we need to compare the height. So change the code accordingly.
import java.text.DecimalFormat;
import java.util.Scanner;
public class Trajectory {
public static double GRAVITY_CONST = 32.17;
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("Enter projectile velocity (feet / sec)");
double projVelocity = sc.nextDouble();
System.out.println("Enter angle of elevation (degrees)");
double angleOfElev = sc.nextDouble();
System.out.println("Enter distance from target (feet)");
double dis2Target = sc.nextDouble();
System.out.println("Enter target size (height of target in feet)");
double targSize = sc.nextDouble();
//Where are you going to use this angle? is it repeated? if repeated then remove, otherwise use it in proper place
System.out.println("Enter elevation of target (to bottom of target in feet)");
double elevOfTarget = sc.nextDouble();
if(projVelocity == 0 && angleOfElev == 0 && dis2Target == 0 && targSize ==0 && elevOfTarget == 0)
break;
/* Distance to target
Time = -----------------------------------
Velocity * cos (angle of elevation in radians)
*/
double cos0val = Math.cos(angleOfElev);
if(cos0val ==0 || projVelocity == 0) {
System.out.println("The computed distance cannot be calculated with the given data");
break;
}
double time = dis2Target / (projVelocity * cos0val);
/* Height = Velocity * Time * sin (angle of elevation in radians)
Force of Gravity * Time * Time
- -------------------------
2
*/
double sin0 = Math.sin(angleOfElev);
//Here write the formula for calculating force of gravity, i have no idea about the formula
double forceOfGravity = Trajectory.GRAVITY_CONST;
double height = projVelocity * time * sin0 - (forceOfGravity * time * time)/2.0;
//you didn't specified how to caluclate comparing distance to print the statements
//change according to your forumula
double comparingDistance = dis2Target;
if(height == comparingDistance)
System.out.println("The target was hit by the projectile");
DecimalFormat df = new DecimalFormat("#.###");
String htStr = df.format(height);
if(height < comparingDistance)
System.out.println("The projectile was too low, height was: "+htStr+" feet");
if(height > comparingDistance)
System.out.println("The projectile was too high, height was: "+htStr+" feet");
if(height < 0)
System.out.println("The computed distance was too short to reach the target");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.