Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Spring 2015
Project #1
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by 10:19 am on Thursday, February 12

# Resources

• Eck, 2.3.1
• Notes from class on the use of methods ("subroutines")
• Documentation on the Math library (link)

# Projectile Motion

Suppose we launch a ball from the ground, with velocity v, attempting to hit a target at coordinates (tx,ty). At what angle must we set our launcher in order to hit the target? To complicate matters, suppose there is a barrier of height h, at a horizontal distance away from our launcher, x. Is there some angle, θ, that will do the job?

Your job is to write a program, LaunchAngle, which can be used to answer this question. Your program should take five command-line arguments: the initial velocity, the (tx,ty) coordinates of the target, the distance of the barrier wall, and its height (in that order). For every such combination, we can calculate two possible angles for the projectile's launch. Your program should display both angles (in degrees, rather than radians or gradients), then indicate whether the angle will successfully clear the barrier wall, hitting the target.

### Sample Interactions

The following interactions show the results of launching the projectile at 22 m/sec, with the target at coordinates (30,1.8) and the barrier at a horizontal distance of 15 meters from the launcher. We'll try various heights for the barrier: 3.5 meters, 10, and 23 meters

```John-Lasseter:hw1 jlasseter\$ java LaunchAngle 22 30 1.8 15 3.5
Angle #1 == 22.629350 degrees:true
Angle #2 == 70.804281 degrees:true
John-Lasseter:hw1 jlasseter\$ java LaunchAngle 22 30 1.8 15 10
Angle #1 == 22.629350 degrees:false
Angle #2 == 70.804281 degrees:true
John-Lasseter:hw1 jlasseter\$ java LaunchAngle 22 30 1.8 15 23
Angle #1 == 22.629350 degrees:false
Angle #2 == 70.804281 degrees:false```

# Mathematics

There are two equations that come in to play for this problem. The first gives us the two possible angles that will result in a successful hit a target at (x,y), with initial velocity of v:

(Okay, that's really two equations, one for the + and the other for the -). Recall that "tan-1 (E)" is the inverse tangent (a.k.a. "arctangent") of E, that is, the angle whose tangent is E. In the Java Math library, this is available as the atan() method.

The other equation you'll need to know is the height y of a projectile at horizontal distance x from the launch site, when we launch it at an angle θ, with velocity v:

For both of these, the value g stands for the acceleration of an object due to gravity on Earth. Use the value 9.81 (m/sec2) for this.

First, this will be a much easier problem if you separate the concerns into three major parts: the calculation of the two potential angles, the calculation of a projectile's height, and determining whether the projectile launched at some angle will clear the barrier wall. I strongly suggest that you write a few supplemental programs to test your work on these parts before you try to put everything together. For example, you might write a program to test heights, checking that you are correctly calculating the height of a projectile launched with velocity 22 m/sec at an angle 70.804281 degrees, when it has traveled a horizontal distance of 15 meters:

```John-Lasseter:hw1 jlasseter\$ java TestHeight 22 70.804281 15
21.992248332301333
```

Try to keep this separation of concerns discipline in mind throughout, and the project should go pretty smoothly.

Finally, a caution about angles: although you're displaying results in the 0 ... 360 degrees format (well, really, 0 .. 90 degrees, since nothing else would work), nearly all of the methods in the Math library expect their angle arguments in radians rather than degrees. You can convert a value from degrees to radians using the toRadians method, and you can convert radians to degrees using the toDegrees method.

# Summary of Expectations

• Your program must be free of all compiler errors. I cannot emphasize this enough. Any solution that cannot be successfully compiled with javac will receive no credit. If you run into any trouble here, please ask me for help, which I am always happy to give (as are the TAs). I know this seems draconian, but I promise you'll thank me for it later.

• Your program should give correct results, matching at least the sample inputs I've given above. Note, however, that partial credit will be given for partially correct programs, so long as they are free of compile-time errors.

• Your program should adhere to principles of clear, elegant, and beautiful code, as outlined in the Style Guide (See "Java Style Guide", under the General Notes section of the web site).

Turn in a paper copy of your LaunchAngle.java source code. This must be turned in by the beginning of lab on Thursday.

Submit an electronic copy of the final version of your program in a folder named hw1. Remember to copy this to your turn in folder:

`/classes/cs124/<your last name>/hw1/`

NOTE: Make sure you're actually copying it to this folder and not your own home folder at /afs/afs.hws.edu/home/<your user name>! This is an obvious source of confusion, and will lead you to think you've turned in your work to me when you haven't.

Also, please respect my file name requests. I ask for precise names because it makes it easier for me to test your work. Thanks.

John H. E. Lasseter