Project 4

CS 178 - Fall 2004
Assigned:November 8, 2004
Due: Sunday, November 21, 2004 at 11:59pm
Created by the diabolical mind of Barry Wittman


Introduction

The purpose of this project is to once again demonstrate the awesome problem solving powers that Java posesses, this time applied to... Physics, no doubt to everyone's irrepressible glee. You will use what you know about widgets, if statements, loops, mathematical operations, and even grid layouts to find more efficient ways to kill people, virtually, at least. After all, calculus was invented to better understand the trajectories of artillery.


The Task

Your mission, should you choose to accept it (and, well, your grade depends on your acceptance), is to construct an applet which can properly determine the trajectory of a cannon ball fired under some ideal laboratory conditions, that is, the kind you would never find in a war. However, to respond to a variety of tactical situations, your code must be able to compute various pieces of data based on different inputs. The angle that the cannon is tilted at (always between 0 and 90 degrees) is always supplied. However, there are 3 pieces of data that are sometimes supplied and sometimes not:

You must have two out of three of these quantities in order to determine the path of the cannon ball. You can find out which quantities you have by examining each of the fields to see which one is blank. If fewer than two of these quantities are specified, you cannot solve the problem and must report an error. If all three are specified, ignore the value for speed.

Finally, time of flight is never given, and you must always compute it (in seconds).


Wrath of the Math

Ah, but which of your are Physics majors? Very few, if any. Perhaps high school Physics was a long time ago in the dark and murky past. Never fear, we would not leave you totally defenseless. Mostly, but not totally.

First, if you don't know, the cannon ball is moving (in this project, anyway) in two dimensions: x and y. These two dimensions are totally independent of each other. The cannon ball always begins at x = 0 and y = height, where the height is one of the three quantities you must sometimes determine. The flight of the cannon ball always ends when it hits the ground, that is, when y = 0.

Motion in the x dimension always obeys the following equation:


x = speed*cos(angle)*time

Motion in the y dimension is a little more complicated because gravity is affecting the y velocity. y movement always obyes the follwing equation:


yfinal - yinitial = speed*sin(angle)*time + 0.5*g*time2

With these two equations, you can find the solution for any single unknown among the three, but there are other ways to perform the same operations. About.com has a nice page about constant acceleration equations that you can find here if you want more information. There are hundreds of other excellent resources on elementary physics available on the Internet and in libraries.

Please note that we are assuming that the value of g, acceleration due to the force of gravity is -9.8 meters/second2


Shockingly Graphic

Naturally, you are all such accomplished programmers that solving the motion equations for the cannon ball poses far too small of a challenge for you to feel as if you are truly exercising your potential. So, we tacked on a nice little graphical display that will show the path the cannon ball takes.

However, we don't want to overburden you with the annoying details of adding graphical support. So, we added a class to do it for you. BackgroundCanvas is a class which draws an nice background image and a path of a cannon ball over top of it. You need to make no modifications to BackgroundCanvas, and all its initialization should be set up for you in the skeleton code we give you. You only need to be concerned with two methods on this class:

public void addPoint( int x, int y )

is a function that lets you add points along the path of the cannon ball that BackgroundCanvas will draw for you. You see, nothing in the computer world is continuous. Even though the path of the cannon ball is one continuous path, we are going to draw it as a lot of little points. If we draw the points close enough together, then it will appear to be one continuous curve. There are functions in Java that are designed to draw curves, but this is more, uh, educational. You simply have to go through values of time, starting at 0 and stepping up to the final time value that you compute, and add a point at that time with the correct x and y values. You can choose different time steps if you want, but we recommend a time step of 0.05 seconds. For consistency with the sample output, please consider the lower left corner of the the image to be the point (0,0) and consider every pixel of image space to be equal to one meter. Thus, all your graphical output is assumed to be in meters. Unfortunately, this choice of units means that we only have 225 meters of display area in the y direction and 300 meters of display area in the x direction. It is not very difficult to construct a cannon ball path that will not be displayed at all graphically. Do not be concerned about this limitation.

public void clear()

is a much simpler function. All that this function does is clear points that have already been created. You only need to add points each time you find the values for the cannon ball's flight. Once the points have been added to the BackgroundCanvas, it will automatically redraw the points.


The Applet

Here, finally is the applet. Try to duplicate its functionality exactly. Note that something that looks suspiciously like a GridLayout is used to arrange the TextFields. The meaning of the two buttons should be fairly clear. Note that there are several error cases that are possible as a result of 1) failing to specify the angle, 2) specifying the angle outside of the legal range, and 3) not specifying enough unknowns. Please try to duplicate our error handling.

By the way, if you are trying to exactly match our sample applet, its size is 400x500 pixels.

Here, of course is the sample applet:

And here is the single graphics file you'll need:

background.jpg

If you missed it, here's the skeleton code and the code for the BackgroundCanvas:

CannonSkeleton.java
BackgroundCanvas.java


Hints, Misdirections, and Lies

You might be suddenly stricken by fear that everyone else in the class knows how to find sines, cosines, and other mathematical functions in Java while you are left out. If this is the case, you might be interested in the following items:

	double Math.sin( double angle )
	double Math.cos( double angle )
	double Math.tan( double angle )
	double Math.sqrt( double value )
	Math.PI
	

The functions are all the mathematical functions you should need for this project. The first three are simply sine, cosine, and tangent. However, PLEASE NOTE that the input to these functions is radians NOT degrees. There are a few places in the code where you have to convert between radians and degrees and feet and meters, that sort of thing. Such conversion is by design, and all of you are certainly capable of looking up the proper conversions. The last function is square root, and the final item is a constant that holds the value of pi out to quite a few decimal places.

On a related mathematical note, some of you may need to solve a quadratic equation to make your projects work. Doing so should be a simple task... or should it? Quadratic equations are annoying in that they (generally) have two roots. So, in a program that has one correct answer, how do you choose which of the two roots you are going to pick? The only hint I can give is that a negative time value is unlikely to be helpful.


The Submission

The project should be turned in electronically by Sunday, November 21, 2004 at 11:59pm. In accordance with the Grading Policy, projects which are turned in more than 24 hours in advance of the deadline or at any time after the deadline will have their grades adjusted accordingly. You should turn in a single Java source file called CannonBall.java. Project submission should be done via CVS in the same way as the earlier projects... except better. If you are having trouble submitting your files, talk to your TA for help.