Your program must do the following in order to receive full credit on this assig
ID: 3882522 • Letter: Y
Question
Your program must do the following in order to receive full credit on this assignment. Ask the user to provide the following values which you will store as a double, one at a time, with a proper message for each (ie. "Please type in the height"). a. Velocity - The speed (in feet per second) at which the cannonball is fired. b. Angle - The angle (in degrees) above the ground at which the cannonball is fired (this is called the inclination angle in the above diagram). c. Distance This is the horizontal distance (in feet) to the target (RH in the above diagram) d. Elevation - The height (in feet) of the bottom of the target from the ground (y value on an x-y plane). e. Size - This is how tall the target is from its bottom (elevation) to its top (elevation + size). Validate the user's input. One of the calculations you need to do requires division and we must ensure that the denominator of the equation is not 0. a. Calculate (velocity * the cosine of angle in radians). i. Use the Math class for cosine and converting to radians. b. If that value if 0, print a message that the distance cannot be calculated with that input and do not continue. Otherwise, have the program continue to the next step. i. That is, the rest of the program should be in an else block. C. Calculate the time it takes the object to reach the distance. a. The formula is (distance/ (velocity * the cosine of angle in radians). d. Calculate the height of the projectile at the distance. a. The formula is (velocity *time *the sine of the angle in radians - ((32.17 * time squared)/2)) i. Again, use the math class for the sine and squaring time. ii. If you're having trouble with this formula, try breaking it into smaller parts. Print the outcome of this cannonball shot. a. There are four possible outcomes and you should print a proper message for each one. i. Height is less than 0: This means that the cannonball did not even reach the target. ii. Height is greater than 0 but less than the elevation of the target: This means the cannonball went under the target. Print the height of the cannonball as part of your message. iii. Height is greater than elevation and less than elevation plus size: This means the cannonball hit the target! iv. Height is greater than elevation plus size: The cannonball went over the top of the target. iv. Height is greater than elevation plus size: The cannonball went over the top of the target.Explanation / Answer
import java.util.*;
import java.lang.*;
class Test
{
public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
double heightProjectile = 0;
//input variables
System.out.println("Please type in the velocity : ");
double velocity = scan.nextDouble();
System.out.println("Please type in the angle : ");
double angle = scan.nextDouble();
System.out.println("Please type in the distance : ");
double distance = scan.nextDouble();
System.out.println("Please type in the elevation : ");
double elevation = scan.nextDouble();
System.out.println("Please type in the size : ");
double size = scan.nextDouble();
//check denominator condition for 0 value
if(velocity * Math.toRadians(Math.cos(angle)) == 0)
System.out.println("Distance cannot be calculated ");
else
{
double time = distance/(velocity * Math.toRadians(Math.cos(angle))) ;
heightProjectile = velocity * time * (Math.toRadians(Math.sin(angle))) -(32.17*time*time/2);
}
if(heightProjectile < 0)
System.out.println("Cannonball did not even reach the target.");
else if(heightProjectile > 0 && heightProjectile < elevation)
System.out.println("Cannonball went under the target.Height : "+heightProjectile);
else if(heightProjectile > elevation && heightProjectile <(elevation+size))
System.out.println("Cannonball hit the target.");
else if(heightProjectile >(elevation + size))
System.out.println("Cannonball went over the top of the target . Height : "+heightProjectile);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.