## CS 1401 Assignment #5

Date Assigned: Wednesday, February 16, 2005

Due Date: Wednesday, February 23, 2005, before the beginning of your lab section.

Goals: to learn mathematical functions like sin(x), conditions, and logical expressions.

Points: This assignment is worth a total of 20 points. No late assignments will be accepted, but credit will be given for partially completed assignments turned in on time.

Assignment: To celebrate the good grades of CS 1401 students, the Department of Computer Science decided to launch some fireworks. To avoid accidents, the department decided to design a software tool that would compute the trajectory of each launched rocket.

This tool uses the following formulas. Let us design a coordinate system XY in such a way that the launcher is located at the center of this coordinate system, i.e., at a point with coordinates (0,0), the X axis is horizontal, and the Y axis is vertical.

For each rocket, we know the velocity V0 (in feet per second) with which it starts moving, and we set up an angle A (in degrees) at which this rocket is shot. The angle should be between 0 and 90 degrees: 0 degrees means that we shoot the rocket parallel to the earth; 90 degrees means that we shoot the rocket straight into the sky. Let g = 9.8 m/sec^2 be the free-fall acceleration.

Once we know the initial velocity and the angle, we can compute the horizontal and vertical components of this velocity as VX_0 = V0*cos(A) and VY_0 = V0*sin(A). Then, once someone wants to find the position and velocity of the rocket at some moment of time t, this person can use the following formulas:

• If t<0, this means that the rocket was not launched yet, so it located at the launcher (X = Y = 0) and both its velocity components VX and VY are 0.
• If the value V_Y0*t - (1/2)*g*t^2 is non-negative, this means that the rocket is still in the air, then X = VX_0*t, Y = VY_0*t - (1/2)*g*t^2, VX = VX_0, and VY = VY_0 - g*t.
• If VY_0*t - (1/2)*g*t^2<0, this means that the rocket has already landed, so we must find the moment t0 = 2*(VY_0/g) when it landed. The rocket is not moving anymore (VX = VY = 0), it is lying on the surface (Y = 0) at a distance X = VX_0*t0 from the launcher.

Structure: Define a class CurrentState in which each object (state) is characterized by two coordinates and two velocity components. Make sure that your description includes:

• a constructor,
• two methods that return both coordinates, and
• two methods that return both velocity components.

Define a class Rocket in which each rocket is characterized by its initial velocity (in feet per second) and angle (in degrees). This class should include the following value-returning methods:

• a method that returns the angle in radians;
• a method that returns the horizontal component of the rocket's initial velocity;
• a method that returns the vertical component of the rocket's initial velocity;
• a method that, given time t, uses the above conditional formulas to return the state of the rocket at the time t (a state is an object of type CurrentState).
Named constants such as g and named constants that are needed for converting from degrees to radians and from meters to feet should be defined in the class Rocket.

All readings from the user and all printing to the screen should be in the main method of the application class.

The application class should do the following:

• ask a user for the velocity and angle of his or her rocket, and for the time t;
• based on the answer, construct the corresponding object of type Rocket;
• use the methods defined for such objects to print a receipt that displays the coordinates and velocity components of this rocket at the given time t.

Example. Let us assume that we launch the rocket with velocity V0 = 30 feet/sec, at an angle of 60 degrees. Then, we do the following preliminary computations.

• First, since Java requires that the angles are given in radians, we must transform the angle from degrees to radians. By definition, a radian is 180/PI degrees, so 1 degree is PI/180 radians, where PI = 3.141592653... So, PI/180 = 0.0174532..., and 60 degrees is 60*(PI/180) = 1.047192.
• Next, we compute VX_0 = V0*cos(A) = 30*0.5 = 15 and
VY_0 = V0*sin(A) = 30*0.8660254 = 25.980762.
• We also transform the free-fall acceleration g from meters per square sec to feet per square sec. It is known that 1 meter = 3.2808399 feet, so
9.8 m/sec^2 = 9.8*3.2808399 ft/sec^2 = 32.152231 ft/sec^2.
Now, if the user asks for the state of the rocket at a time t = - 1.0 sec, since this time is negative, the answer should be: the rocket's coordinates are X = 0 and Y = 0, and its velocity components are VX = 0 and VY = 0.

If the user asks for the state of the rocket at a time t = 1.5 sec, then

V_Y0*t - (1/2)*g*t^2 = 25.980762*1.5 - (1/2)*32.152231*1.5^2 =
38.971143 - 36.1712259 = 2.799884
is non-negative. So:
• X = VX_0*t = 15*1.5 = 22.5 ft;
• Y = V_Y0*t - (1/2)*g*t^2 = 2.799884 ft;
• VX = VX_0 = 15 ft/sec;
• VY = VY_0 - g*t = 25.980762 - 32.152231*1.5 = - 22.247584 ft/sec.

If the user asks for the state of the rocket at a time t = 2.0 sec, then

V_Y0*t - (1/2)*g*t^2 = 25.980762*2.0 - (1/2)*32.152231*2.0^2 =
51.961524 - 64.304462
is negative, which means that the rocket has alreaxdy landed. In this case, we compute the moment t0 = 2*(VY_0/g) when it landed as
t0 = 2*(25.980762/32.152231) = 2*0.8080547 = 1.6161094 sec.
In this case:
• X = VX_0*t0 = 15*1.6161094 = 24.241641 ft;
• Y = 0 ft;
• VX = 0 ft/sec;
• VY = 0 ft/sec.

Deliverables: as announced in the labs and explained in the handouts given to you in the labs.