David Barry

Ranch Hand

Posts: 89

posted 8 years ago

Hi everyone. I am trying to learn the sin() method for a program I am trying to write. It doesn't seem to be working for me. I have the code pasted below. I have checked the Java API and it seems rather vague. If somebody could help me see what I am doing in error here, I would appreciate it. Thanks

When this code tries to run, it give the following error: "cannot find symbol - method sin(double)"

When this code tries to run, it give the following error: "cannot find symbol - method sin(double)"

Campbell Ritchie

Marshal

Posts: 56585

172

David Barry

Ranch Hand

Posts: 89

Campbell Ritchie

Marshal

Posts: 56585

172

Campbell Ritchie

Marshal

Posts: 56585

172

posted 8 years ago

You can either write Math.sin(1.234) or do a static import; luckily the example quoted there is for the Math class.

David Barry

Ranch Hand

Posts: 89

posted 8 years ago

I believe I know have the sin() method figured out thanks to the wonderful insight of Campbell. However, I am now trying to implement the calculations, and I getting totally wrong answers. This program is to calculate distance of a projectile based on launch angles as well as (of course) launch velocities(speeds). The formula is as follows:

Here is the two classes:

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!!

Here is the two classes:

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!!

David Barry

Ranch Hand

Posts: 89

Ulf Dittmer

Rancher

Posts: 42972

73

David Barry

Ranch Hand

Posts: 89

Campbell Ritchie

Marshal

Posts: 56585

172

posted 8 years ago

That isn't really secret information. It says that in the documentation:

Here's a link to the Math class's documentation: java.lang.Math

you should really get into the habit of looking in the API documentation. I really don't see how people can write Java programs without consulting it regularly.

David Barry wrote:You are saying that Math.pow() and Math.sin() uses radians? How do you know that?

That isn't really secret information. It says that in the documentation:

public static double sin(double a)

Returns the trigonometric sine 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 sine of the argument.

Here's a link to the Math class's documentation: java.lang.Math

you should really get into the habit of looking in the API documentation. I really don't see how people can write Java programs without consulting it regularly.

posted 8 years ago

Go to the java API (here). in the bottom left panel, scroll down to find the Math class, and click on it. The main panel will then have details about the class and all the methods - one of which is the 'sin()' method. You should see this:

static double sin(double a)

Returns the trigonometric sine of an angle.

if you click on the blue 'sin', it will take you to details of the method. There, you will see this:

sin

(emphasis mine)

You can read about the pow() method there as well.

David Barry wrote:Ok. I really don't understand what you are saying here You are saying that Math.pow() and Math.sin() uses radians? How do you know that? Thank you for the tip; I am just looking for more clarification here.

Go to the java API (here). in the bottom left panel, scroll down to find the Math class, and click on it. The main panel will then have details about the class and all the methods - one of which is the 'sin()' method. You should see this:

static double sin(double a)

Returns the trigonometric sine of an angle.

if you click on the blue 'sin', it will take you to details of the method. There, you will see this:

sin

public static double sin(double a)

Returns the trigonometric sine 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:Returns:

a - an angle, in radians.

the sine of the argument.

(emphasis mine)

You can read about the pow() method there as well.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

David Barry

Ranch Hand

Posts: 89

posted 8 years ago

Thanks everybody for the tips and suggestions. However, when I go to use printf() in this output; I run into some trouble. Because when I do this: it prints out all of the values for the distance. I only want ones to go underneath this header:

"MPH 25 deg 30 deg 35 deg 40 deg 45 deg 50 deg 55 deg");

However, it prints the values underneath these and goes on in one big line until it runs out of values. I tried using "%n" but all that does is makes a vertical line of values. Can somebody help here?

"MPH 25 deg 30 deg 35 deg 40 deg 45 deg 50 deg 55 deg");

However, it prints the values underneath these and goes on in one big line until it runs out of values. I tried using "%n" but all that does is makes a vertical line of values. Can somebody help here?

Campbell Ritchie

Marshal

Posts: 56585

172

posted 8 years ago

The bit about %17.0f will print out a floating-point number taking 17 spaces, with 0 places after the decimal point. As you have noticed, %n inserts a new line. I think you will have to set up some sort of loop which prints out the values for 25 30 35 etc. Not sure I can help much more on this particular point, I am afraid.

And you did say earlier you had read the API, so people presumed you knew Math.sin() takes radians.

And you did say earlier you had read the API, so people presumed you knew Math.sin() takes radians.