user1800674
user1800674

Reputation: 233

Math.cos not working

This bit of my program is supposed to calculate bottomAngle using cosine rule.

public double bottomAngle() {

        topAngleinRadians = Math.toRadians(topAngle) ;

        return (Math.cos(topAngleinRadians)(bottomAngle() = ladderLength^2 + floorLength^2 - verticalHeight^2) / 2 * ladderLength * floorLength) ;
        }

Errors produced: enter image description here

Here is my list of errors and I can't figure it out what's wrong with my formula. All the methods such verticalHeight , ladderLength works perfectly fine in other methods. There is something wrong with the way I put this formula. Can you please help me out?

Upvotes: 3

Views: 651

Answers (5)

Joel Westberg
Joel Westberg

Reputation: 2736

There's a few issues here.

Multiplication - unlike in regular algebra, you have to explicitly define that you want multiplication between two expressions Math.cos(topAngleinRadians)*...

Assignment - you appear to be trying to assign something to a method call (bottomAngle() = ...). This is not really something you can do, and I'm not really sure what you are trying to achieve by it.

Squaring - 10^2 does not square 10 into 100 in java, but is rather the XOR (exclusive OR) operator. You probably want to use Math.pow(ladderLength, 2) or simply ladderLength * ladderLength

Upvotes: 4

Rohit Jain
Rohit Jain

Reputation: 213193

Can't really understand the purpose of your return statement, but I would rather break the statement into 2-3 lines to make it more readable: -

public double bottomAngle() {

        topAngleinRadians = Math.toRadians(topAngle) ;


        double bottomAngle = Math.pow(ladderLength, 2) + Math.pow(floorLength, 2) - 
                             Math.pow(verticalHeight, 2);

        double denom = 2 * ladderLength * floorLength;
        double numerator = bottomAngle * Math.cos(topAngleinRadians);

        return numerator / denom ;
}

Note that, 3 ^ 2 does mean 3 squared in Java. You would need Math.pow method for that.

Also, you need to check why you were having bottomAngle() method call on LHS. I have assumed it to be a temp variable here.

As you can see, your code looks much more readable. And it becomes easy to find out compiler errors.

Upvotes: 1

speakingcode
speakingcode

Reputation: 1506

Your sytnax is incorrect. Parenthesis do not mean mutliplication, you need explicit * (multiplication operator). Also, you have some other mistakes:

Math.cos(topAngleinRadians)(bottomAngle() = ladderLength^2 + floorLength^2...

this looks like a method call i.e. bottomAngle() being set equal to some other expression, this is invalid also..

Upvotes: 0

Thomas Owens
Thomas Owens

Reputation: 116161

Without seeing your list of errors, you do have syntax errors:

return (Math.cos(topAngleinRadians)(bottomAngle() = ladderLength^2 + floorLength^2 - verticalHeight^2) / 2 * ladderLength * floorLength);

  • You have no operator between your call to Math.cos() and the next part of your expression.
  • You're also appear to be assigning values to a function call, which doesn't make sense.
  • The ^ operator is also not the exponential operator, but a bitwise exclusive OR operator. You're probably looking for Math.pow().

Those are just what I'm seeing right off the top. It might be helpful to read up about the Java operators and how they are evaluated.

Upvotes: 5

SLaks
SLaks

Reputation: 887195

Unlike algebraic notation, Java parentheses do not implicitly multiply.

You need to insert a * between )(.

Upvotes: 2

Related Questions