I have the following code:
import java.util.*;
public class Final{
private double guess = 1;
public Final(double x){
root(x);
}
public double root(double b){
double rez = -1;
if(Math.abs(guess*guess - b) < 0.001){
rez = guess;
}
else{
guess = (2/guess + guess)/2;
rez = root(b);
}
return rez;
}
public double rooting(double d){
while(Math.abs(guess*guess - d) > 0.001){
System.out.println(guess);
guess = (2/guess + guess)/2;
}
return guess;
}
public static void main(String[] args){
System.out.println(new Final(2));
}
in other words, the two methods do the same job: they find the square root of a positive integer using the Newton's method. And when I call the methods inside the main method using the following code:
Final x = new Final();
x.root(2);
x.rooting(2);
they both work fine. However, when calling the methods inside the constructor, using such code as:
System.out.println(new Final(2));
what I get in the screen is: Final@9cab16.
Finally, I am aware that this is perhaps out of bounds of a practical question, but I would like to know what is going on.
Anybody with an answer is very much thanked!