CIS 260 - Calculate a trajectory

The objectives of this assignment are:

  1. create and use methods
  2. validate input
  3. create and use constants
  4. format output using System.out.printf or String.format

Assignment

You have to display the trajectory information for a projectile given a firing angle, initial velocity, and time increment. This is a simplified model which does not take air resistance into account. You have to validate user input to make sure it is valid and falls within a specified range. The output has to be formatted to exactly match the output of the sample shown at the end of this page using System.out.printf or String.format.

  1. Create a function named getDouble which takes three arguments: a String prompt, a double minimum value, and a double maximum value. The prompt should be displayed before getting a string as user input. That string should then be checked to see if it represents a valid floating point number. If it does, then it should be checked to make sure it is between the minimum and maximum values. If there are any errors, an appropriate error message should be displayed and the user should be prompted to re-enter a value. If there are no errors, then the double value of the entered string should be returned. You can use data input and validation function(s) from class if you wish as long as they work properly.
  2. Create a constant PI and set it equal to 3.1415926535897.
  3. Create a constant G and set it equal to 9.8.
  4. Declare and initialize any variables you need for the program.
  5. Get the firing angle from the user (0.0 through 90.0).
  6. Get the firing velocity from the user (0.0 through 1000.0).
  7. Get the time increment from the user (0.1 through 1.0).
  8. Calculate the horizontal velocity = firing velocity * cosine(angle)
  9. Calculate the vertical velocity = firing velocity * sine(angle)
  10. Calculate the last time displayed on the report = 2 * vertical velocity / G
  11. Display report headings.
  12. For time = 0 to lastTime by time increment steps display the time, height, vertical speed, distance, and horizontal speed. Make sure the formatting is exactly like the sample.
  13. Distance is calculated as horizontal velocity * time.
  14. Vertical speed is calculated as original vertical velocity - G * time.
  15. Height is calculated as original vertical velocity * time - G * time * time / 2.

Special notes

  1. We may develop a data input method similar to getDouble in class.
  2. The Java trignometric functions expect input to be in the form of radians. There are 2 Pi radians per 360 degrees. Therefore, if you know the degrees, you can get the radians easily using this conversion: radians = angle * 2 * Pi / 360

Grading rubric

Note: Program must be able to be compiled and run.

  1. 5 pts: style conventions followed (no tabs, proper indentation, documentation comments)
  2. 12 pts: double getDouble(String, double, double) function works properly
  3. 8 pts: input data is properly validated
  4. 12 pts: program produces correct results
  5. 8 pts: printf output formatting matches sample
  6. 5 pts: report displays the moment when height returns to 0 (impact)

Sample output

Enter an angle (0-90): Hello
Error: Invalid number
Enter an angle (0-90): -6
Error: Value entered is below minimum value of 0.0
Enter an angle (0-90): 91
Error: Value entered is above maximum value of 90.0
Enter an angle (0-90): 1.2.3
Error: Invalid number
Enter an angle (0-90): -.
Error: Invalid number
Enter an angle (0-90): 45
Enter a velocity (0-1000): 100
Enter the time increment (0.1-1.0): 1

 Time  Height Speed Y    Dist Speed X
 0.00    0.00    70.7    0.00    70.7
 1.00   65.81    60.9   70.71    70.7
 2.00  121.82    51.1  141.42    70.7
 3.00  168.03    41.3  212.13    70.7
 4.00  204.44    31.5  282.84    70.7
 5.00  231.05    21.7  353.55    70.7
 6.00  247.86    11.9  424.26    70.7
 7.00  254.87     2.1  494.97    70.7
 8.00  252.09    -7.7  565.69    70.7
 9.00  239.50   -17.5  636.40    70.7
10.00  217.11   -27.3  707.11    70.7
11.00  184.92   -37.1  777.82    70.7
12.00  142.93   -46.9  848.53    70.7
13.00   91.14   -56.7  919.24    70.7
14.00   29.55   -66.5  989.95    70.7
14.43    0.00   -70.7 1020.41    70.7