Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a simple prototype program to predict the trajectory of a projectile und

ID: 3795432 • Letter: T

Question

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 Distance to target Time Velocitycos (angle of elevation in radians) The distance the projectile is off the ground at the target's distance is calculated using the formula Height-Velocity Timesin (angle of elevation in radians) Force of Gravity Time Time 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: 1. The target was hit by the projectile 2. The projectile was too low, height was: ### feet

Explanation / Answer

//The program below calculates the trajectory of a projectile according to the question and prints if the projectile reaches the target or not.

import java.util.*;   

public class Projectile {
  
/***********************************************************
* This the constant for gravity as mentioned in the question.
* To make any variable constant in java we use final keyword(since its value cannot be changed after being initialized once).
* Static is used to associate a variable with class. All the instances will share same GRAVITY value.
***********************************************************/
public static final double GRAVITY = 32.17;

public static void main(String[] args) {
try{
Scanner in = new Scanner(System.in);

Boolean flag = true; // use flag to continue until user enters all values as 0
/***************************
* As mentioned in the question, we will input
* projectile velocity (feet/s)
* angle of elevation (degrees)
* distance from target (feet)
* target height (feet)
* elevation of target (to bottom of target in feet)
**************************/
  
while(flag){
System.out.print("Enter the projectile velocity (feet/second)? ");
double velocity = in.nextDouble();
/*******************
* 1 foot = 0.3048 meters
* So we convert from feet to m
*******************/
velocity = velocity * 0.3048;
  
System.out.print("Enter the angle of elevation (degrees)? ");
double angle = Math.toRadians(in.nextDouble());
  
System.out.print("Enter the distance from target (feet)? ");
double distanceFromTarget = in.nextDouble();
distanceFromTarget = distanceFromTarget * 0.3048;
System.out.print("Enter the target height (feet)? ");
double targetHt = in.nextDouble();
targetHt = targetHt * 0.3048;
System.out.print("Enter the angle of elevation of target (degrees)? ");
double targetAngleOfElevation = Math.toRadians(in.nextDouble());
System.out.println();
if(velocity == 0 && angle == 0 && distanceFromTarget ==0 && targetHt == 0 && targetAngleOfElevation == 0){
flag = false;
System.out.println("Exiting...");
break;
}
/***************************************
* According to question :
* Time taken by projectile to reach the target = Distance to target / velocity * cos(angle of elevation in radians)
***************************************/
double timeTakenByProjectile = distanceFromTarget / (velocity * Math.cos(angle));
  
/****************************************
* Now calculating the distance the projectile is off the ground at target's distance:
* Height = velocity * time * sin(angle of elevation in radians) - (Force of gravity * (Time)^2)/2
*/

double finalHeight = (velocity * timeTakenByProjectile * Math.sin(angle)) - (GRAVITY * (timeTakenByProjectile)*(timeTakenByProjectile))/2;

/******************************
* if finalHeight of projectile when it is at target is equal to or less than (height of target + its angle of elevation)
* but greater than or equal to target foot i.e. if the projectile reaches the target's foot
* then it reaches target successfully
* else it doesnot reach the target.
*****************************/
if((finalHeight <= (targetHt + Math.cos(targetAngleOfElevation)) && finalHeight >= Math.cos(targetAngleOfElevation))){
System.out.println("The target was hit by the projectile");
}else if(finalHeight < Math.cos(targetAngleOfElevation) && finalHeight > 0){
System.out.println("The projectile was too low , height was : " + finalHeight/0.3048 + "feet");
}else if (finalHeight < 0){
System.out.println("The computed distance was too short to reach the target");
}else if(finalHeight > (targetHt + Math.cos(targetAngleOfElevation))){
System.out.println("The computed distance was too high , height was : " + finalHeight/0.3048 + "feet");
}
}
  
}catch(Exception e){
/*************************
* This is to handle the exceptions while taking input as well as the exception that occurs while computing if some of the input value is 0
*/
System.out.println("The computed distance cannot be calculated with given data");
}
}
}

// The following is the edited version as per the request.

Also I would like to inform you that String Tokenizer is retained in java only for compatibility and its use is discouraged because it cannot distinguish between numbers, strings, identfiers and also it does not recognise the comments.

import java.util.*;

public class Projectile {

/***********************************************************
* This the constant for gravity as mentioned in the question. To make any variable constant in java we use final
* keyword(since its value cannot be changed after being initialized once). Static is used to associate a variable
* with class. All the instances will share same GRAVITY value.
***********************************************************/
public static final double GRAVITY = 32.17;

public static void main(String[] args) {
try {
Scanner in = new Scanner(System.in);

Boolean flag = true; // use flag to continue until user enters all values as 0
/***************************
* As mentioned in the question, we will input projectile velocity (feet/s) angle of elevation (degrees)
* distance from target (feet) target height (feet) elevation of target (to bottom of target in feet)
**************************/

while (flag) {
System.out.print("Enter the projectile velocity (feet/second)? , angle of elevation (degrees)?, distance from target (feet)? ,"
+ "the target height (feet)?, angle of elevation of target (degrees)? ");
System.out.println("Please enter the input according to the order above and seperated by space.");
/********************************************
* StringTokeniser breaks the strings into tokens. Here we break it on spaces. Here either you can get
* all the inputs together in a line separated by any delimiter which can be used to break the string
* into tokens Or you can take input one by one. I am taking input in a single line so the user must
* ensure that he enters input correctly.
*******************************************/
StringTokenizer st = new StringTokenizer(in.nextLine(), " ");
//if there are no more tokens then NoSuchElementException is thrown which will be catch by our try catch block.
double velocity = Double.parseDouble(st.nextToken());
/*******************
* 1 foot = 0.3048 meters So we convert from feet to m
*******************/
velocity = velocity * 0.3048;

//the angle of elevation (degrees)
double angle = Math.toRadians(Double.parseDouble(st.nextToken()));

//the distance from target (feet)
double distanceFromTarget = Double.parseDouble(st.nextToken());
distanceFromTarget = distanceFromTarget * 0.3048;
//the target height (feet)
double targetHt = Double.parseDouble(st.nextToken());
targetHt = targetHt * 0.3048;
//the angle of elevation of target (degrees)
double targetAngleOfElevation = Math.toRadians(Double.parseDouble(st.nextToken()));

if (velocity == 0 && angle == 0 && distanceFromTarget == 0 && targetHt == 0 && targetAngleOfElevation == 0) {
flag = false;
System.out.println("Exiting...");
break;
}
/***************************************
* According to question : Time taken by projectile to reach the target = Distance to target / velocity
* * cos(angle of elevation in radians)
***************************************/
double timeTakenByProjectile = distanceFromTarget / (velocity * Math.cos(angle));

/****************************************
* Now calculating the distance the projectile is off the ground at target's distance: Height = velocity
* * time * sin(angle of elevation in radians) - (Force of gravity * (Time)^2)/2
*/

double finalHeight = (velocity * timeTakenByProjectile * Math.sin(angle)) - (GRAVITY * (timeTakenByProjectile) * (timeTakenByProjectile)) / 2;

/******************************
* if finalHeight of projectile when it is at target is equal to or less than (height of target + its
* angle of elevation) but greater than or equal to target foot i.e. if the projectile reaches the
* target's foot then it reaches target successfully else it doesnot reach the target.
*****************************/
if ((finalHeight <= (targetHt + Math.cos(targetAngleOfElevation)) && finalHeight >= Math.cos(targetAngleOfElevation))) {
System.out.println("The target was hit by the projectile");
} else if (finalHeight < Math.cos(targetAngleOfElevation) && finalHeight > 0) {
System.out.println("The projectile was too low , height was : " + finalHeight / 0.3048 + "feet");
} else if (finalHeight < 0) {
System.out.println("The computed distance was too short to reach the target");
} else if (finalHeight > (targetHt + Math.cos(targetAngleOfElevation))) {
System.out.println("The computed distance was too high , height was : " + finalHeight / 0.3048 + "feet");
}
}

} catch (Exception e) {
/*************************
* This is to handle the exceptions while taking input as well as the exception that occurs while computing
* if some of the input value is 0
*/
System.out.println("The computed distance cannot be calculated with given data");
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote