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:

  1. 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).

  2. 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.

  3. Calculate the time it takes the object to reach the distance.

    a. The formula is (distance / (velocity * the cosine of angle in radians).

  4. 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.
  5. 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Scanner;
public class Assignment1
{
public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the velocity of the cannonball");

double velocity =scan.nextDouble();

System.out.println("Please enter the angle the cannonball will be shot at");
double angle = scan.nextDouble();
double radians = Math.toRadians(angle);
double radcos = Math.cos(radians);
double radsin = Math.sin(radians);

System.out.println("Please enter the distance to the target");
double distance = scan.nextDouble();
double time1 = (velocity * radcos);
double time2 = (distance/(velocity*radcos));

double TimeSquare = Math.pow(time2, 2);
System.out.println("Please enter the elevation of the bottom of the target");
double elevation = scan.nextDouble();


System.out.println("Please enter the size of the target");
double size = scan.nextDouble();
double height =((velocity*time2*radsin)-(32.17*time2*time2)/2);


if (time1 == 0)
{
System.out.println("The distance cannot be caculated with that input");
}
else if (height < 0 )
{
System.out.println("the cannonball did not even reach the target");
}
else if (height > 0 && height < elevation)
{
System.out.print("the cannonball went under the target");
System.out.println("the height is" + height);
}
else if (height > elevation && height < (elevation + size))
{
System.out.println(" the cannonball hit the target!");
}
else if (height>(elevation + size))
{
System.out.println( "The cannonball went over the top of the target.Height is" + height );

}
}
}