Projectile Motion (while loop) - HELP NEEDED
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Projectile Motion (while loop) - HELP NEEDED

1. Junior Member
Join Date
Jun 2007
Posts
5

## Projectile Motion (while loop) - HELP NEEDED

Hi everyone.... I'm new to the forum and new to Java programming.... I have a project with the following info.
1. A projectile is fired with a given initial velocity at a launch angle of 45 degrees. If the trajectory is too short a message will be displayed such that with the given gunnery the target is out of range.
2. If the trajectory overshoots, then the new launch angle will be half of the previous launch angle.
3. If the trajectory is too short (but not at the very first instance) then the new launch angle will be half way between the current angle and the last angle of overshooting.
4. The steps are repeated as long as the projectile hits the target, considering the given precision.

3 Classes (ProjectileMain - main method, does the calling, Projectile - calculations, Gunnery - handles the above algorithm and outputs message).

My code is finished but it seems that the while loop in the Gunnery class (iterateShooting method) is not working properly. Any help would be appreciated.

Code:
```import java.util.*;
import javax.swing.*;

public class ProjectileMain {
//
// The main method obtains the input values, instantiates a Gunnery object with the
// input values and calls the checkRange method of the Gunnery class.
//
public static void main(String [] args)  {

String input; // Input string used for initialVelocity, distanceToTarget, and precison.

// Obtain initialVelocity, distanceToTarget, and precision utilizing a GUI input window.

input = JOptionPane.showInputDialog (null, "Enter the initial velocity (feet/sec),"
+"\nthe required precision of the hit,"
+"\nand the distance to the desired target (in feet)"
+" seperated by spaces");

// Convert the string to tokens and instantiate a Gunnery object with the input values.
// The checkRange method is called which results in either a error message or final output message.

StringTokenizer st = new StringTokenizer(input);

while (st.hasMoreTokens())
{
Gunnery gunnery = new Gunnery (Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()),
Double.parseDouble(st.nextToken()));
gunnery.checkRange();
}

} // end method main

} // end class ProjectileMain

import java.text.*;

public class Projectile {

private static final double GRAVITATION = 32; // The gravitational force (feet/sec).
//
// The method toRadians takes the degree value of a given angle as a parameter;
// the method computes and returns the value of that angle in radians.
//
public static double toRadians(double angle)  {

return (angle * (Math.PI/180));

//
// The method flightTime computes and returns the flight time of the trajectory with
// the given launch angle and initial velocity.
//
public static double flightTime (double angle, double velocity)  {

DecimalFormat df = new DecimalFormat ("0.00");

return Double.parseDouble (df.format (2 * ((velocity * Math.sin(angle)) / GRAVITATION)));

} // end method flightTime
//
// The method distanceTraveled computes and returns the total distance traveled by the projectile.
//
public static double distanceTraveled (double angle, double velocity)  {

DecimalFormat df = new DecimalFormat ("0.00");

return Double.parseDouble (df.format (velocity * Math.cos(toRadians(angle))
* flightTime(angle, velocity)));

} // end method distanceTraveled

} // end class Projectile

import javax.swing.*;
import java.text.*;

public class Gunnery {

private double distanceToTarget; // The distance to the target in feet.
private double initialVelocity; // The initial velocity.
private double precision; // The required precision of the hit.
private int counter; // Counts the number of attempts until the target was hit
//
// The method checkRange checks if, for launch angle = 45 degrees, the distance traveled plus the
// precision is less than the distance to target, the method sends out a message of failure
// and terminates the process; otherwise it calls iterateShooting() with 45 degrees.
//
public void checkRange()  {

if ((Projectile.distanceTraveled(45, initialVelocity) + precision) < distanceToTarget)
{
JOptionPane.showMessageDialog (null, "Target beyond reach! Choose a bigger gun!\n"
+"Program exits.");
System.exit(0);
}
else iterateShooting (45);

} // end method checkRange
//
// The method iterateShooting runs a while loop until precision is attained; then calls
// displayResults() with the last distanceTraveled, the hitting angle and the number of attempts.
//
public void iterateShooting (double angle)  {

double updateAngle = angle;
counter = 0;

while (Projectile.distanceTraveled(angle, initialVelocity) != (distanceToTarget + precision))
{
// If the trajectory overshoots, then the new launch angle will be half of the previous launch angle.
if (Projectile.distanceTraveled(angle, initialVelocity) > (distanceToTarget + precision))
{
updateAngle = angle;
angle = angle / 2;
counter++;
}
// If the trajectory is too short, then the new launch angle will be half way between the current angle
// and the last angle of overshooting.
else if (Projectile.distanceTraveled(angle, initialVelocity) < (distanceToTarget + precision))
{
angle = (angle + updateAngle) / 2;
counter++;
}
} // end while

displayResults(Projectile.distanceTraveled(angle, initialVelocity), angle, counter);

} // end method iterateShooting
//
// The method displayResults composes and displays an output message on a JOptionPane window
// based on paremeter values.
//
public void displayResults (double distance, double angle, int counter)  {

DecimalFormat df = new DecimalFormat ("0.00");

JOptionPane.showMessageDialog(null, "Target was hit\n\n Initial velocity = " +initialVelocity
+"\nPrecision: " +precision +"\n Distance to target: "
+distanceToTarget +"\nDistance traveled by the projectile: "
+distance +"\n The hitting angle: "+Double.parseDouble(df.format(angle))
+"degrees" +"\nThe number of attempts: "+counter);

} // end method displayResults
//
public Gunnery ()  {

} // end default constrictor Gunnery
//
// The non-default constructor Gunnery takes three double parameters and initializes the
// initialVelocity, distanceToTarget, and the precision data fields.
//
public Gunnery (double velocity, double accuracy, double distance)  {

initialVelocity = velocity;
precision = accuracy;
distanceToTarget = distance;

} // end constructor Gunnery

} // end class Gunnery```

2. ## Re: Projectile Motion (while loop) - HELP NEEDED

For debugging (well you should learn how to use the debugger...)

But moving:
Code:
` displayResults(Projectile.distanceTraveled(angle, initialVelocity), angle, counter);`
INSIDE the while loop will reveal what is happening...

3. Junior Member
Join Date
Jun 2007
Posts
5

## Re: Projectile Motion (while loop) - HELP NEEDED

Thanks for the reply.....It would be nice to know how to use a debugger.... I'll probably have to wait until my next Java class.

It seems that there is something screwed up in my calculation area (possible rounding) as the loop never stops...... with the input values velocity = 100, precision = 0.15, distance to target = 301, the loop should stop after 21 attempts at 37.26 degrees (hitting angle) with a distance traveled by the projectile of 301.15 (mine shows 300.87 at this stage)....

I will have to look into my calcs....... THANKS FOR THE HELP

EDIT: Figured it out.... I was rounding the value in the flightTime method when I shouldn't have been......

WOW... something that easy took me THAT LONG to figure out....!!!
Last edited by magic507; June 7th, 2007 at 06:46 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•