• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

pass four parameters to a method and return a value

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using a free pdf book (Think Java - How to Think Like a Computer Scientist) and a Dummies book to try to teach myself Java. Eclipse mars is my IDE, using Java 8. I have encountered a problem with one of the examples in the Think Java book. I have spent days debugging and modifying the code, reading books, and Googling articles in an attempt to find a solution. Part of the code is provided, and the rest has to be figured out. The program takes two points defined by x,y coordinates and calculates the distance between the points using a custom method. Four variable values (x1, x2, y1, and y2) are passed to the method. The method performs the calculation. The method is supposed to return the value of the distance. Returning the resulting value is where I am hitting a dead end. The compiler error indicates that the result cannot be resolved to a variable. Included below is the last working code which prints various values from within the method. I have been unable to pass return the distance from the method to be printed from main.



Here is the error code:
Description Resource Path Location Type
result cannot be resolved to a variable DistanceBetweenTwoPoints.java /06-02-Example/src line 30 Java Problem

Here is the output from the method when the println statement in main is commented out:
dx is 3.0
dy is 4.0
dsquared is 25.0
distance is 5.0

How can I successfully return the value from the method?

Any helpful assistance will be greatly appreciated.
 
Marshal
Posts: 8370
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will Klem wrote:Returning the resulting value is where I am hitting a dead end. The compiler error indicates that the result cannot be resolved to a variable.

You have the method "distance" which returns the result. So, think about it as your result being returned to line 28, from where you're calling it.
There are two options to solve that quickly:
1. on line 28 you do "double result = DistanceBetweenTwoPoints.distance(1.0, 2.0, 4.0, 6.0);" in this case "result" will hold the returned value
2. put line 28 inside line 30 print statement, it will return value and will print out without storing it.
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will Klem wrote:I am using a free pdf book (Think Java - How to Think Like a Computer Scientist) . . . to try to teach myself Java. Eclipse mars is my IDE, . . .

I have a copy of that book, too. I found it full of errors.

If you are a beginner you are probably better off not using an IDE.

Liutauras means to get rid of line 28 and change line 30 to read
System.out.println("Result = " + distance(x1, y1, x2, y2));
Or change the earlier line to
double result = distance(...);

The error message means you do not have a variable called result.
 
Will Klem
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liutauras Vilda and Campbell Ritchie,

Thanks for your rapid replies. Both options supplied by Liutauras worked properly:

/* this solution #1 works:
* double result = DistanceBetweenTwoPoints.distance(1.0, 2.0, 4.0, 6.0);
*
*/System.out.println("result = " + result);

*/ this solution #2 works:
* System.out.println(DistanceBetweenTwoPoints.distance(1.0, 2.0, 4.0, 6.0));
*/

I really appreciate having two options provided. This has taught me two more advanced concepts which may have taken me a long time to discover:
1) A method call can be assigned to a variable to obtain a return value.
2) A method call can be used as an argument s demonstrated with System.out.println().

Ritche,
I, too, have encountered errors in Think Java. Some I have figured out, and others I have just passed by.

Thanks to both of you for sharing your knowledge and experience with me.
 
Liutauras Vilda
Marshal
Posts: 8370
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the third concept taught by CR latest post that "class name not needed to add prior the method" as it would work also only with "double result = distance(....);" OR "System.out.println(distance(....));" Your both methods are static, and you're using it in the same class, so, writing class name becomes redundant.
 
Liutauras Vilda
Marshal
Posts: 8370
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think one other key thing you need to take from this is understanding scope. Your original code has a variable called result in the distance method, and you need to understand why this variable is not accessible inside the main method.
 
Liutauras Vilda
Marshal
Posts: 8370
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what Mike just mentioned, in order to understand that, you can start by reading this Oracle tutorial (<- link to read).

[edit] added quote

Oracle's tutorial wrote:Not all combinations of instance and class variables and methods are allowed:

Instance methods can access instance variables and instance methods directly.
Instance methods can access class variables and class methods directly.
Class methods can access class variables and class methods directly.
Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.

 
Will Klem
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To all who have replied since my last post:

Thanks for the welcome to the forum, additional information provided, and resources recommended. As a programming newbie, I find your support gratifying.
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic