• Post Reply Bookmark Topic Watch Topic
  • New Topic

coordinate conversion project  RSS feed

 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Im trying to create a code right now that will take a table of rectangular coordinates and convert them to Polar coordinates in the constructor. I will eventually calculate the total distance between all points in the calculateDistancePolar method but for now I am using it to test.

It doesnt like line 29 and 31 of the class Polar and I cannot figure out why.





 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. If you posted an error message I wouldn't need to dowload JSci to compile your example.
2. You could have posted SSCCE so I wouldn't need to dowload JSci to compile your example.

And I stop complaining and start answering

The problem is: in line 29 you have
polarCoords is of type double[] so polarCoords[ row ] is of type double.
You can't invoke length on double. A variable of type double does not have any methods or fields thus the error (which you didn't post) java: double cannot be dereferenced
The problem in line 31 is similiar: java: array required, but double found

Read what compiler is trying to tell you. The first message might not be co obvious to decipher but the second one IMO is self-explanatory.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't try doing arithmetic on pairs found in arrays like that; it is very error‑prone. Create an XYPair class and put the two coordinates in that. Are you aware of the two Math class methods used for polar coordinates: 1 2?
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't try doing arithmetic on pairs found in arrays like that; it is very error‑prone. Create an XYPair class and put the two coordinates in that. Are you aware of the two Math class methods used for polar coordinates: 1 2?


Thanks, ya I have given up on doing it that way... I got some help from the teacher and he showed me this way but I dont know how to use either constructor. How do I set the x and y using the getters and setters of these classes using a test class? Ive never see a constructor with inputs like (Cartesian cartesian) or (Polar polar), so the inputs are the classes themselves?

At the bottom I put the scribbling of me starting to try and figure out how to use these classes. Thank you

Update: I figured out how to set the x and the y using cart.setX() and setY().... but ultimately I am trying to set up a class that will be able to find the distance between two points at a time and add up the total distance between all the 10 points from top down of the 10 coords. How is using getRho() and polar.rho going to be different values? Do i need to call the constructor each time I set new values to x and y? Im confused, any help is appreciated





 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor can take any object type - it's a parameter like anything else.

So, if you have a Cartesian object, you can pass it in to the Polar constructor to make a Polar object. And vice versa. The input to Polar is an object of type Cartesian.

You sometimes also pass a second Cartesian object into a Cartesian object...for example, if you want the distance between two. Assume I have two points:


The Cartesian class may have a distance method, that takes a Cartesian object - after all, you need two points to compute a distance:



--update---
i looked at your source a little closer...you have this method:



So...assuming you have two Polar objects polar1 and poloar2, you would get the distance by doing this:


 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou for your help.

Ok I have this, I posted the errors it is giving to me. It looks like to me it isnt liking the null I am inputting in to the Cartesian objects. What does it want to see for an input type? And is there any other reason why my for loop to add up the distances wouldnt work?





fred rosenberger wrote:A constructor can take any object type - it's a parameter like anything else.

So, if you have a Cartesian object, you can pass it in to the Polar constructor to make a Polar object. And vice versa. The input to Polar is an object of type Cartesian.

You sometimes also pass a second Cartesian object into a Cartesian object...for example, if you want the distance between two. Assume I have two points:


The Cartesian class may have a distance method, that takes a Cartesian object - after all, you need two points to compute a distance:



--update---
i looked at your source a little closer...you have this method:



So...assuming you have two Polar objects polar1 and poloar2, you would get the distance by doing this:


 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paul spriesterbach wrote: . . . It looks like to me it isnt liking the null I am inputting in to the Cartesian objects. . . .
No, it is usually a bad idea to let nulls run free. You are telling the JVM, “Here isn't an object, call the getY method on it,” and the JVM tells you in no uncertain terms that it can't do anything on non‑existent objects.
Try new Polar(new Cartesian(123.45, 678.90))
You may have to give your classes constructors which take x and y or ρ and Θ as parameters directly.
Why haven't you used both methods I told you about earlier? The one you haven't used would help in the distance between two points problem, I think.
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is how I initially made my constructors, but the instructor wanted us to make the constructors like this. Here is what I have now. When I am making the instances of the objects what do I put in there instead of null to get this to work? I tried Polar polar, and Cartesian cartesian and a bunch of other stuff but none of it was working.







Campbell Ritchie wrote:
paul spriesterbach wrote: . . . It looks like to me it isnt liking the null I am inputting in to the Cartesian objects. . . .
No, it is usually a bad idea to let nulls run free. You are telling the JVM, “Here isn't an object, call the getY method on it,” and the JVM tells you in no uncertain terms that it can't do anything on non‑existent objects.
Try new Polar(new Cartesian(123.45, 678.90))
You may have to give your classes constructors which take x and y or ρ and Θ as parameters directly.
Why haven't you used both methods I told you about earlier? The one you haven't used would help in the distance between two points problem, I think.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use Math.pow(x, 2). Use x * x
Don't use Pythagoras when there already is a method which does that for you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!