Introduction to java class program assignment help

We are susposed to be writing a program that calculates the best angle for firing a cannon ball to get the best distance through different substances. My teacher and i could not see why the program was not working and i was wondering if anyone could spot what is wrong with the code. The program is susposed to go through theta from 0 - 90 and remember the best theta for the best distance. we were testing with the drag constant as 0 because the theta should be 45 but the program outputs 81 instead. The big problem is that my x value goes negtive which should not happen and this is where my teacher gave up because he was unable to see why. my code is below for the program.

Thanks for the help in advance

Brian

import javax.swing.*;

publicclass Cannon2

{

privatestaticfinaldouble g = -9.8;

publicstaticvoid main(String[] args)

{

int theta = 0;// initilizes theta

int maxtheta = 0;

double speed = 100;// initilizes speed

double dt = 0.01;

double time = 0.0;

double vx, vy, vx1, vy1, xpos, ypos, oxpos, dragconst;

double x = 0;

double y = 0;

ypos = 0;

xpos = 0;

oxpos = 0;

dragconst = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter drag constant:"));

while (theta <= 90)

{

vx = speed * Math.cos(theta);

vy = -speed * Math.sin(theta);

x = 0;

y = 0;

ypos = 0;

xpos = 0;

while (ypos >= 0)

{

ypos = y + vy * time;// y position

xpos = x + vx * time;// x position

time = time + dt;// increments time

x = xpos;

y = ypos;

vx = vx - Math.pow(vx, 2) * dragconst;// velosity calculator for x

vy = (vy + g * time) - (Math.pow(vy, 2) * dragconst);// velosity calculator for y

System.out.println(ypos);

System.out.println(xpos);

System.out.println(theta);

}

if (xpos > oxpos)

{

oxpos = xpos;

maxtheta = theta;

}

theta = theta + 1;

}

System.out.println("The best angle is: " + maxtheta);

}

}

[3371 byte] By [bhawkins4194a] at [2007-10-2 2:16:59]
# 1

Simple. Reread the Math.cos (and Math.sin) documentation:

cos

public static double cos(double a)

...

Parameters:

a - an angle, in radians.

Returns:

the cosine of the argument.

I don't think 90 is what you want...

doremifasollatidoa at 2007-7-15 20:09:22 > top of Java-index,Java Essentials,New To Java...
# 2
Thank You i would have never thought to look at that.
bhawkins4194a at 2007-7-15 20:09:22 > top of Java-index,Java Essentials,New To Java...
# 3
You are welcome. The sad thing is that the teacher didn't think about it.Good luck!
doremifasollatidoa at 2007-7-15 20:09:22 > top of Java-index,Java Essentials,New To Java...