I am trying to implement the calculations, and I am getting totally wrong answers. This program is to calculate the distance of a projectile based on launch angles as well as (of course) launch velocities(speeds). The formula is as follows: http://learn.flvs.net/webdav/educator_apcsa_v8/module09/rtfmod09/09_04_VirtualLectureNotes.pdf

Here is the code that I have for doing the calculations:

/**
 * This purpose of this program is to calculate the trajectory of a projectile based on launch angles and launch velocities.
 * 
 * @author John D. Barry
 * @version 03/24/09
 */
class Catapult
{
    //declaration of private instance variables
    private int launchSpeed;
    private double distance, distanceInFeet, launchAngle, calculateAngleLaunch;
    
    
    //constructor for ojbects of type CO2Footprint
    Catapult(int theLaunchSpeed, double theLaunchAngle)
    {
        launchSpeed = theLaunchSpeed;
        launchAngle = theLaunchAngle;
     }
    
    //mutator method to calculate the the distance
    public void calcDistance()
    {
        distance = (Math.pow(launchSpeed,2) * Math.sin(launchAngle * 2)) / 9.8;
    }
    
    //mutator method to calculate the distance in feet
    public void calcDistanceInFeet()
    {
        distanceInFeet = (distance * 100) / 2.54 * 12;
    }
    
    //getter method to get the value of the distance in feet
    public double getDistanceInFeet()
    {
        return distanceInFeet;
    }
    

}
/**
 * This class tests the Catapult class. 
 * 
 * @author John D. Barry 
 * @version 03/24/09
 */
import java.util.ArrayList;
public class CatapultTester
{   
    public static void main(String[] args)
    {
        ArrayList<Catapult> catapult = new ArrayList<Catapult>();
        catapult.add(new Catapult(40,25));
        catapult.add(new Catapult(20,30));
        catapult.add(new Catapult(20,35));
        catapult.add(new Catapult(20,40));
        catapult.add(new Catapult(20,45));
        catapult.add(new Catapult(20,50));
        catapult.add(new Catapult(20,55));

        Catapult catapultRecord;                   //creates a new catapultRecord object of type Catapult
        
        for(int index = 0; index < catapult.size(); index++)
        {
            catapultRecord = catapult.get(index);
            catapultRecord.calcDistance();
            catapultRecord.calcDistanceInFeet();
        }
        
        System.out.println("                                                  Projectile Distance (feet) ");
        System.out.println("    MPH          25 deg            30 deg            35 deg            40 deg            45 deg          50 deg");
        System.out.println("=================================================================================================================");

                
        
        for(int index = 0; index < catapult.size(); index++)
        {
            catapultRecord = catapult.get(index);
            System.out.printf("%12.2f",catapultRecord.getDistanceInFeet());
            


        }
    }
}

For example, I know that the first problem should equal 410 feet and by the program calculation it is showing: -2065.08

So.... ...something is wrong here I just don't know what!!

Add System.out.println() statements in the calculations themselves so you can debug the intermediate results.

Also, note that most angle parameters to the Math methods are in radians, not degrees.

What equation exactly are you using? distance = (velocity initial) * time + one half (acceleration)(time squared) is the equation I remember. Your site is not accessible, probably because I don't go to your school. And where in your program are you getting the wrong results? You should be able to tell us which method is not working as you expect it to simply by doing some print statements.

edit: Ezzaral beat me to it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.