CSE 110 - 1
Overview
In this project we will write a program that will “fire” a cannonball at a mid-air target and determine if the cannonball hits the target or not. Five variables needed for this calculation will be provided by the user. Our program will do minor input validation on this input and will output the outcome of your calculations. See the diagram below for an example of what we are calculating.
Requirements
Our program must do the following:
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.Calculate the time it takes the object to reach the distance.
a. The formula is (distance / (velocity * the cosine of angle in radians).
Calculate the height of the projectile at that 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. (1) 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. (1) Print the height of the cannonball as part of your message. v. Note: I worded the above conditions similar to how you you might consider setting up your if, else-if, and else statements, but there are other, better, ways to organize this. Give it some thought.
Example Inputs
Below are five example runs of the program with the inputs and outputs. Remember, the graders will be testing your program against these as well as their own, so make sure you test these and come up with your own before submitting your program.
#1
Please enter the velocity of the cannonball.
5
Please enter the angle the cannonball will be shot at.
45
Please enter the elevation of the bottom of the target.
10
Please enter the size of the target.
7
The cannonball did not make it to the target.
#2
Please enter the velocity of the cannonball.
500
Please enter the angle the cannonball will be shot at.
20
Please enter the distance to the target.
12
Please enter the elevation of the bottom of the target.
3
Please enter the size of the target.
1
The cannonball went over the target. Height at the target was 4.357150481812686
#3
Please enter the velocity of the cannonball.
0
Please enter the angle the cannonball will be shot at.
50
Please enter the distance to the target.
29
Please enter the elevation of the bottom of the target.
46
Please enter the size of the target.
19
The distance cannot be calculated with this input.
#4
Please enter the velocity of the cannonball.
500
Please enter the angle the cannonball will be shot at.
45
Please enter the distance to the target.
5
Please enter the elevation of the bottom of the target.
2
Please enter the size of the target.
50
The cannonball hit the target!.
#5
Please enter the velocity of the cannonball.
100
Please enter the angle the cannonball will be shot at.
10
Please enter the distance to the target.
20
Please enter the elevation of the bottom of the target.
50
Please enter the size of the target.
1
The cannonball went under the target. Height at the target was 2.8631355334347828
Sample Code
1 | import java.util.Scanner; |