• Post Reply Bookmark Topic Watch Topic
  • New Topic

issue with return statement  RSS feed

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






both of these compile and work if i comment out the final return otherwise i get error



how can i fix this?
 
Ranch Foreman
Posts: 919
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The void part means that the function cannot return any values.
As such you cannot have a return with a value in that method.
You could place most of the code in the main method in it's own method, but still make it part of the Distance class if you choose to.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use all that arithmetic to calculate Pythagoras: use this method instead.
Use the % tags and System.out.printf instead of decimal format; it is easier to use.
 
Ryan Gordon
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to do this program with a method public double calculateDistance(double x1, double x2, double y1, double y2)
Then be able to access it from my class with the main method, I was able to make the program run without doing that, but I have trouble trying to call methods. I keep getting stuck in this area.
 
Pete Letkeman
Ranch Foreman
Posts: 919
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me, this kind of looks like it's a homework assignment.
Please note that we do not provide direct answers to homework assignments on this site.
We prefer to give you hints and to point you in the correct general direction.

You could create a single method that does only this

then you call that method doing something like

At which point you could do more calculations on theDistance or print the value to the screen.

You can have any number or methods in your class defined at the same level at the main method as you are close to doing with
https://coderanch.com/t/685983/java/wrong-result
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Gordon wrote:I have to do this program with a method . . .
Do the instructions include writing your own logic for Pythagoras? You can implement that method with hypot(). It is usually better practice to use ready‑made methods which have been tested many times, but if you are told to calculate √(x² + y²) the hard way, you will have to do it the hard way.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!