• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method using data from two arraylists

 
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a difficulty with using a specific method; computing distances. I have made two arraylists. Both arraylists consist of objects (respectively orders and drivers) that have a latitude and longitude.
I will use a loop, in which I want to calculate the distances between one object of the first arraylist and all objects of the second arraylist. Then, proceeding with another object from the first arraylist (after removing one element) and calculating the distances with all other objects of the second arraylist again.

I thought about using a while loop, as you can see below.

It doesn't even compile. How should I refer to these objects to make the calculations work?
The errors I receive are:
Syntax error, insert "}" to complete MethodBody
Syntax error, insert "}" to complete ClassBody

The method I use:




 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that the complete code in the first code box? If so, you need a surrounding class and method. If not, you probably just need to add two braces to the end of your code.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it go wrong because of my placement of methods?

The first one is in the class with the main method. It's somewhere in the brackets of the main methods.
The second code is in the class Order.

Does this seem logical? Would it be better to transfer a method to another class or make a new class?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your methods look complicated; please explain what the formula for calculating length of an arc of a great circle is. I can see two possible errors in your method. One is using Mat#pow for squares. Avoid Math#pow for squares; use x * x instead. I shall let you find the other, which may be more serious.
The line in the first post is much too long. I shall break it so you can see the right way to post long lines. I also think you have too many things connected together by dots in that line.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is something wrong with your design; you appear to have non‑private fields. You should not be able to access fields as you are doing.
I think you should have a LatLong class, preferably immutable and calculate your distances between two LatLong instances.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried hard to find an error or potential error in the formulation, but could not find a new one. It computes the distances just fine (The ones I tried). I moved the method to a new class and market it final. With this new class I was able to sort the second arraylist based on the computed distances. So far, I am satisfied with the output I get.

If I understand you correctly, my style is not really appropriate. How could I improve it? Could I have written the while-loop more concise, instead of using all the dot-operators?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest (as I told you earlier) you create a Location or LatLong class which will encapsulate both latitude and longitude, up to ±90° (lat) or ±180° (long).
Use Customer#getLatLong() to return a LatLong object and do the calculation of distances in the LatLong class.
something.setDistance(something.getLatLong().distance(other.getLatLong()));

Your formula looked complicated because you were multiplying sin²Δlong by two cosines and not sin²Δlat, but when I looked on Wikipedia that seemed to be correct. Sorry for my mistake.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sander Hoovenaar wrote:If I understand you correctly, my style is not really appropriate. How could I improve it? Could I have written the while-loop more concise...


That's a mechanical solution, when I suspect that what you really need to do is think about this problem as a whole a bit more.

For example: What about a Position or MapReference class that includes latitude and longitude, and "knows" how to calculate the distance between two of its own objects?

That'll remove a lot of "noise" from your existing class, and you've already basically written the method.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic