• Post Reply Bookmark Topic Watch Topic
  • New Topic

Point to Point returning  RSS feed

 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So the purpose of this code is to be able to insert the coordinates of one point in, and the coordinates of another point, and get the distance between the two. Currently I'm having a problem in the StartingVal method. I need to return the val's in it, but I have multiple that I need to return. How would I go about doing this? I'm currently getting an error that says "Sphere.java:13: error: cannot find symbol" In other words, I'm not returning the values that I need to. Any help would be great!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The numbers val1 val2 etc. have no existence inside the main method.
Your arithmetic in line 21 is probably incorrect.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you're given something like this:

**** EXAMPLE RUN ****

Hello User!
I am a program designed to
help you figure out the distance
between two points.
Let's gather some basic data, shall we?

Degrees of latitude 1? 36
Minutes of latitude 1? 7.2

The bolded text in the example above is something that the user of the program is supposed to type in. You're not supposed to write code like this:

Think about it for a minute: How is your program going to calculate the distance for any other point besides the one that has latitude of 36 degrees and 7.2 minutes if you do this? That doesn't make sense, does it?

Go back over your notes again and find where you were taught how to get input from the user.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your prompts all ask for a latitude. A point is defined by its latitude and longitude.  This is probably a copy-paste error on your part. That's one of the worst sins in programming. In this case, the mistake is pretty benign but if you get used to doing this all the time, you can create all sorts of havoc if you don't remember to modify copied code that does something more than just display text on the console.  When you find yourself copy-pasting code, then you should stop and ask yourself, "Can I put this repetitive stuff in a method and just pass in the values that change as parameters?"
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:

Think about it for a minute: How is your program going to calculate the distance for any other point besides the one that has latitude of 36 degrees and 7.2 minutes if you do this? That doesn't make sense, does it?

Go back over your notes again and find where you were taught how to get input from the user.


I understand this, I'll go back through and add a scanner into the program. But will this help resolve my issue of getting the correct values to val1, val2, etc.?
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Also, your prompts all ask for a latitude. A point is defined by its latitude and longitude.

Oops, thank you. I'll try and put it all into a method.
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is just the snippit of the StartingVal method. Is this correct? I have a feeling that I need to go back through and correct some things (a lot of things) in it.

 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell already told you what's wrong with your valX variables. To reiterate, they are declared as local variables in the StartingVal() method so they only exist in there.  Once you exit from that method, they will cease to exist.

Read what I said previously about what you should do to avoid writing repetitive code (like code that you copy/paste and modify slightly).

Here's an example:
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand what you're saying now Junilo, thank you for your help. I'll come back here if I have any more questions about this code.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niko Fricker wrote:So this is just the snippit of the StartingVal method. Is this correct? I have a feeling that I need to go back through and correct some things (a lot of things) in it.

Well, it won't compile so it's obviously wrong.

By convention, method names and variable names in Java start with a lowercase letter so StartingVal() should be changed to [tt]startingVal()[tt].

A Java method can return only one value at a time. You can return four separate double values all at once unless you put them inside something like an array of a collection, which you might not have learned about yet. I guess for now, you should just call that method as many times as you need to get values. Since you need four doubles, you'll need to call it four separate times and assign the return value to a different variable each time. The example I gave you previously should give you an idea of what you need to do.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, your prompts are still off.  You'd prompt for degrees and minutes of latitude and then degrees and minutes of longitude.  You still have your prompts all mixed up in that latest snippet of code you posted.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not be using static functions.
You really should have two classes: Sphere and LatLon (I call it that because Java already has a Point class).
The LatLon class would have instance variables lat and lon, plus instance methods getLat() and getLon().
The Sphere class would have an instance variable called radius. It would also have an instance method (i.e., non-static) called getDistance(), that takes two LatLon instances as arguments, which computes the distance between the two points and returns that value. It would also have a getPoint() method that creates a new LatLon based on user inputs.

Then your main() method might look something like this:

 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two other comments:

1. There's a Math.toRadians(degrees) method in the standard API, so you don't need to write your own.

2. Your distance calculation isn't correct, it uses plane trigonometry instead of spherical trigonometry. So it will be approximately correct for points which are close together but less and less correct as the points get farther apart.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And another problem hinted at in my first post is that you do not specify the radius of the sphere. Given two points defined bu (lat,lon) pairs, the distance between those points differs greatly for a sphere of radius 1079 miles (moon) and a sphere of radius 3959 miles (earth).
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shou‍ld be able to find the algorithm here. Beware: there is a mistake in that code.
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright guys, I sadly has to turn in my code incomplete because of deadlines, but I really do want to figure this out and make it work.
Can someone look over my code and tell me why I'm getting the error method LatLon in class SphereDistance cannot be applied to given types;.
It's been eating away at me, I'm hoping another pair of eyes looking at it will help.

I've basically rewritten all of it.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got four pairs of lat and long? If one number is degrees and the other minutes, then:-
  • 1: Make this clear in your prompts and variable names.
  • 2: I still think the arithmetic where you divide by 60 is wrong.
  • Don't call Math#sin twice. Try this instead:-
     
    Niko Fricker
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The reason that there are so many is so you can put in the degrees *space* minutes. And then it would turn that into decimal and place the value in the lat1, lat2, etc.
     
    Junilu Lacar
    Sheriff
    Posts: 11481
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Niko Fricker wrote:Can someone look over my code and tell me why I'm getting the error method LatLon in class SphereDistance cannot be applied to given types;.

    Because on line 10, you call method LatLon() without passing in any arguments.  On line 24, you declare the method to take one parameter, a Scanner object.  With that error, Java is basically saying there's a mismatch between how you declared the method and how you're actually using the method. Essentially, you're trying to put a square peg in a round hole.
     
    Junilu Lacar
    Sheriff
    Posts: 11481
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    By convention, method and variable names in Java start with a lowercase letter. Capitalized names are used for classes, interfaces, and enums. That's why it's main() and not Main(). Your LatLon() method should be latLon() by convention. Stylistically, the name "latLon" is not a good name because it doesn't really tell you what the method is for. The name is cryptic and it is not a good reflection of what's going on inside of it. Something like promptForCoordinates() or inputCoordinates() is more in line with what you're trying to do in that method.
     
    Junilu Lacar
    Sheriff
    Posts: 11481
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You had a nice start in your main() method, keeping it relatively free from lots of implementation details and just making calls out to other methods.

    You didn't do so well with that LatLon() method though. I thought you were getting it with that example of promptForString() that I gave before but I guess the idea hasn't quite clicked yet.

    Here's a smaller example that you can hopefully use to draw an analogy to what you need to do in your program:


    Again, the problem you have in your code is that you declared those lat/long variables as local variables to that method. Local variables cannot be accessed from outside of the methods where they are declared. Once you exit from the method, those local variables are gone forever. 

    See the difference in the example above. I prompt for the value and return the value entered to the main method. That's where the value the user entered is assigned to a variable that I can then use in a calculation later on (see the last argument to the call to System.out.printf() on line 8)

    Likewise, the method you write to prompt the user for a point's coordinates should only do that for one point coordinate at a time.  If you need four point coordinates, then you'd call that method, say you name it promptForPointCoordinate(), four times. Like this:

    Does that make sense?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!