-3

Here is the code from both classes, the user has to input the value for two angles and a length for part of a triangle. I understand Java uses radians, but even with a simple conversion either using Math.toDegrees or * 180/Math.Pi it still gets the wrong answer. For example when the angle 30 is entered, 100 * tan(30) degrees should give 57.1, however I get 44.92.... The Code:

/**Class to implement math methods, and math import
 * 
 */

/**
 * @author Oli
 *
 */

public class Balloon {

    //Attributes

    private int angle1;
    private int angle2;
    private double distance1;
    private int time;
    private double h2;

    //Consttructor

    public Balloon (int ang1, int ang2, double dist1, int t1){
        angle1 = ang1; angle2 = ang2; distance1 = dist1; time = t1;
}


    //Methods to calculate angles, distances and time


    public double height1(){
        double h1;


        h1 = distance1 * (Math.tan(angle1*180/Math.PI));
        return h1;
    }

    public double   height2(){
        double h2;

        h2 = distance1 * (Math.tan(angle2))-(Math.tan(angle1));
        return h2;
    }

    public double speed(){
        double sp1;

        sp1 = h2/time;
        return sp1;
    }

    public double BalloonDistance(){
        double BD1;

        BD1 = distance1 / (Math.cos(angle2));
        return BD1;

    }

    }
RomanHotsiy
  • 4,978
  • 1
  • 25
  • 36
Oli
  • 15
  • 2
  • 3
  • 1
    Sometimes, you use just `angle`, and sometimes you seem to convert it to radians. Also, your conversion seems to be wrong: Try `angle/180*pi`, but then again, you said that even using the builtin method did not work... – tobias_k Jul 15 '14 at 19:30
  • 1
    Why do you post all this code if you say that the problem is `tan` where `100*tan(30)!=57.74` ? – Sifu Jul 15 '14 at 19:31
  • 1
    "I understand Java uses radians" - apparently you don't. – duffymo Jul 15 '14 at 19:34
  • @Oli So, did any of the below answers work for you? – tobias_k Jul 18 '14 at 09:38

6 Answers6

2

Be consistent and use radians for your members.

At any rate you should use built-in functions to do the conversion:

double deg = 30;
double tan = Math.tan(Math.toRadians(deg));
System.out.println("Tan: " + tan);
System.out.println("ATan: " + Math.toDegrees(Math.atan(tan)));

Will print (demo)

Tan: 0.5773502691896257
ATan: 29.999999999999996
Scis
  • 2,934
  • 3
  • 23
  • 37
1

The argument of Math.tan() has to be in radians.

Unless there is a compelling reason not to do so, I'd use radians for every angle everywhere, they're much easier to work with. It's easy to get the conversion wrong or miss it altogether, like your code shows.

biziclop
  • 48,926
  • 12
  • 77
  • 104
1

I understand Java uses radians, but even with a simple conversion either using Math.toDegrees or *180/Math.Pi it still gets the wrong answer.

Your conversion to radians is wrong.

  • Do angle/180*pi, not angle*180/pi,
  • or use toRadians, not toDegrees.

Also, note that for some of the tan calculations you do not convert at all...

tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

A 30 degree angle should result in code that looks like:

double tangent = Math.tan(Math.PI/6.0);

Anything else is wrong.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

You use radian to degree conversation (180/Math.Pi). However, you need degree to radian conversastion which is;

Radian

Then, it is better to implement a utility function like

public int degreeToRadian(int degree)
{
   return degree * (Math.pi / 180);
}

You should use Math#tan like this for degree values

Math.tan(degreeToRadian(angle));

You can also use Math#toRadians instead of above.

From Doc;

public static double tan(double a) Returns the trigonometric tangent of an angle. Special cases: If the argument is NaN or an infinity, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument. The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Parameters: a - an angle, in radians. Returns: the tangent of the argument.

See also

Radian

erencan
  • 3,725
  • 5
  • 32
  • 50
  • 1
    Better use [`Math.toRadians`](http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#toRadians%28double%29) instead of rolling your own... – tobias_k Jul 15 '14 at 19:43
  • Yes, you are right @tobias_k. I updated my answer accordingly. – erencan Jul 15 '14 at 20:00
0

You can use Math.toRadians(int degree) for conversion instead

Tofferino
  • 23
  • 1
  • 2
  • 8