aspose file tools*
The moose likes Beginning Java and the fly likes using sin in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using sin in java" Watch "using sin in java" New topic
Author

using sin in java

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
im making a program that calculate trajectory and im going by this formula:

d = speed^2 sin(2 * angle)
----------------------
g

where d = distance and g = acceleration due to gravity

so far im looking at this:




the problem i am getting with my sin is that when i try to compile i get cannot find symbol variable sin.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
nathan gibson wrote:cannot find symbol variable sin.


You're supposed to supply an argument to Math.sin.

I guess this is what you want to do really,



I haven't checked the distance formula for type conversion errors. It's usually better to stick to double throughout in physical applications.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
im going through my program and im making my for loop to do my print statements.


the compiler says that it cannot find method size()

isnt this supposed to work?
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
nathan gibson wrote:im going through my program and im making my for loop to do my print statements.


the compiler says that it cannot find method size()

isnt this supposed to work?


Does your Catapult class have a size() method? From the code you posted above it doesn't look like it does.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Joe:

I think he meant to use CatapultInfo.

Nathan:

Remember to follow Java naming conventions and to code to an interface not an implementation.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
alright. thanks for pointing out my error there. im getting a little confused. the last thing i had to do was basically just like this, so im going about solving the problem in the same way. here is a piece of what im following:


im following what i did there and adapting it to work with my current assignment. since i already had that program working i know everything was correct,
atleast correct enough to do what i wanted it to do.

im using this:


when i compile i get an error that variable data record might not have been initialized. makes no sense.




Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
If you have a field, it has a default value, all the 0s which means 0 (for numbers) false for booleans and null for reference types.
If you have a local variable (which is likely from the error message) it does not have a default value. You must give it some value (even 0/false/null).

Maybe you have shadowed a field by declaring a local variable by mistake. In which case you would want to delete the declaration.

Why are you calling a Catapult object "dataRecord"?
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i tried initializing it to 0. that resulted in found int but expected Catapult. i looked all through the rest of the code and it wasnt anywhere except for where it was created and where it was used in the print statements. i keep going over my previous code looking for what the problem may be, but i dont understand because everything is virtually the same and one works while the other doesnt.



oh btw, as far as the naming goes, usually i dont pay naming alot of attention. i sometimes just use whatever was used in the example. the only time i fret over naming is when the readability is crucial.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

You initialize object references to null. Change line 22 to:

As for the naming conventions, the reason why I've been bringing it up is that other people have to read your code, especially if you decide to do this for a living. Java, moreso than some other languages, has a standardized set of coding conventions, which most people follow. Following the conventions means it's easier for other people to read your code, which will make it easier to help you. Readability is always crucial.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
thanks for the good advice John, its just that i get a little ahead of myself. also at my level only being in high school my teacher is glad for us to understand and doesnt really press the issues beside correct output. i will make sure to work on it, possibly try to find some documentation to educate me a little. as of now with program i have changed things quite a bit.
im now getting things to compile, but my output is like so:
Projectile Distance (feet)
MPH 25 deg 30 deg 35 deg 40 deg 45 deg 50 deg 55 deg
===============================================================================================================
0.00.00.00.00.00.00.00.0
0.00.00.00.00.00.00.00.0
0.00.00.00.00.00.00.00.0
0.00.00.00.00.00.00.00.0
0.00.00.00.00.00.00.00.0
0.00.00.00.00.00.00.00.0
0.00.00.00.00.00.00.00.0

as soon as i think im getting somewhere...




nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
by using get i have eliminated my previous problem. also, even though my output is wrong, it is correct in the point that it is putting the values in the right place. at this point i dont really know where to go. it has to be my methods, but im comparing them to previously done methods that work fine. this is all very confusing. if anyone can tell me what might be my problem it would be appreciated. i always get stuck at the end
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

I'm actually kind of surprised your Catapult class works at all. Line 37 should be giving you divide by zero errors. Some changes that will help:

1) Eliminate lines 18 and 29 of your Catapult class. You don't really need the acceleration variable. It gets defaulted to zero, and then line 29 sets accelerationDueToGravity to zero.

2) Eliminate line 26.

Here's how I might rewrite your Catapult class:

This isn't necessarily the 'best' way, but it's probably useful to compare with what you have, and see where you can make some changes.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
alright, i used your advice on my class. no i have output, but its pretty out there. i dont think there is any logical reason im getting these numbers.
Projectile Distance (feet)
MPH 25 deg 30 deg 35 deg 40 deg 45 deg 50 deg 55 deg
===============================================================================================================
-35.12610286321984-35.12610286321984-35.12610286321984-35.12610286321984-35.12610286321984-35.12610286321984-35.12610286321984-35.12610286321984
-63.761405434647365-63.761405434647365-63.761405434647365-63.761405434647365-63.761405434647365-63.761405434647365-63.761405434647365-63.761405434647365
233.1148257100906233.1148257100906233.1148257100906233.1148257100906233.1148257100906233.1148257100906233.1148257100906233.1148257100906
-407.4943481085837-407.4943481085837-407.4943481085837-407.4943481085837-407.4943481085837-407.4943481085837-407.4943481085837-407.4943481085837
478.7443357730334478.7443357730334478.7443357730334478.7443357730334478.7443357730334478.7443357730334478.7443357730334478.7443357730334
-343.19189676030794-343.19189676030794-343.19189676030794-343.19189676030794-343.19189676030794-343.19189676030794-343.19189676030794-343.19189676030794
-37.01938370383488-37.01938370383488-37.01938370383488-37.01938370383488-37.01938370383488-37.01938370383488-37.01938370383488-37.01938370383488


what i did notice about these, even though they are wrong, is that the numbers across are repeating over and over again. this shouldn't be happening. i think this might have something to do with my array iteration, but im not really sure. thinks obviously arent being cycled through like they should. im supposed to have a different number for each.

and im pretty sure that my calculations are just plain wrong.

can anyone point me to why my numbers might not be calculating every time?



John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

You're repeating the same value seven times (lines 33-36).

John.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Although I haven't looked at your code in any detail, I bet there's a good chance that you are using degrees as your units and you didn't notice from the API documentation that the Math.trig functions require their inputs to be in radians.

If that's the case, there's another handy method in the Math class which will convert to radians for you.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
John de Michele wrote:Nathan:

You're repeating the same value seven times (lines 33-36).

John.


thats the only value i have, in theory the array is supposed to be gone through and all of the numbers in it plugged into the method, different numbers should equal different results

but i suppose i havent thought through this correctly.

@ paul, yes, i think that may be my one of my problems.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
im getting some better output in terms of realistic numbers now, still having the problem with iteration though.

Projectile Distance (feet)
MPH 25 deg 30 deg 35 deg 40 deg 45 deg 50 deg 55 deg
===============================================================================================================
101.4581063794493101.4581063794493101.4581063794493101.4581063794493101.4581063794493101.4581063794493101.4581063794493101.4581063794493
180.40088427029016180.40088427029016180.40088427029016180.40088427029016180.40088427029016180.40088427029016180.40088427029016180.40088427029016
282.8797244748864282.8797244748864282.8797244748864282.8797244748864282.8797244748864282.8797244748864282.8797244748864282.8797244748864
402.5599474526674402.5599474526674402.5599474526674402.5599474526674402.5599474526674402.5599474526674402.5599474526674402.5599474526674
535.478994670088535.478994670088535.478994670088535.478994670088535.478994670088535.478994670088535.478994670088535.478994670088
668.0762026352801668.0762026352801668.0762026352801668.0762026352801668.0762026352801668.0762026352801668.0762026352801668.0762026352801
791.8021141874283791.8021141874283791.8021141874283791.8021141874283791.8021141874283791.8021141874283791.8021141874283791.8021141874283
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

Unfortunately, you're not iterating like you think you're doing. If you look at lines 33-36, you can see that you're printing the same value seven times per iteration through the loop. What you really want is a number of objects equal to the number of angles * the number of speeds you're testing. Then you'll want to think about how you'd print multiple rows of results. Hint: you'll need more than one loop.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
alright, this is a wild guess, is this a possibility?

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

Nope, that's not it. Try putting one loop into another. Also, you don't need to use String.valueOf(). The println() method knows how to deal with primitives.

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
nathan gibson wrote:alright, this is a wild guess . . .
Don't guess, not even a tame guess.

If it ain't working, get a large sheet of paper, a sharp pencil and a vary large eraser. Write down what you think the stages in going through your loop are. You should end up with lots of little bits of writing and a few arrows. If it starts to look like a road map of Brummagem or a plate of spaghetti, you had better get the eraser out . . .
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i have been looking at the program and am convinced that this may work, but cant try it because im getting getting a compiler error that im not sure how to fix. if anyone has any advice it is appreciated.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
im trying to see if this will work, i think it is the closest i may have been as yet.

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

Sorry, that won't work. The speed and angle arguments aren't arrays.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
but i have the speed and angle in an array list...
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

Not according to line 1 of your Catapult listing at 10:51 am. You're passing doubles to your constructor. Even if you were passing ArrayList objects, you can't access the elements directly. You'd need to use the get() method to extract the value.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
alright, i talked to my teacher and im starting over. i kind of over thought it and drifted out. i am figuring it out a little more now and just have a simple question:

with this code why cannot find variable speed?

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i got this figured out for now. im going to let this thread die as it is getting rather lengthy. thank you guys for all the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using sin in java