Taking a square root in Java, answer appears as 0.0. Why?

OK, so I'm trying to make a program where you input a number and the program takes its square root. Yes, I realize I could do all this on a main but where is the fun in that? I know I'm close, but I'm just not seeing my error! Result shows no errors on either program, but whatever number I input the answer is 0.0

SUB

Code Java:

public class FunMath {
public FunMath() {
double number = 0;
}
public void enterNumber(double amount){
factor = number + amount;
}
public double giveResult(){
double result = Math.sqrt(factor);
return result;
}
private double factor;
private double number;
}

:-?

Main

Code Java:

import java.util.*;
public class FunMathTester {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
FunMath squareroot = new FunMath();
System.out.print("Enter the number you wish to take a square root of: ");
double number = in.nextDouble();
System.out.println(squareroot.giveResult());
}
}

:-?

Re: Taking a square root in Java, answer appears as 0.0. Why?

You never set number inside FunMath, but rather set a local variable which happens to have the same name as number inside your main method.

Re: Taking a square root in Java, answer appears as 0.0. Why?

Quote:

Originally Posted by

**helloworld922**
You never set number inside FunMath, but rather set a local variable which happens to have the same name as number inside your main method.

Could you elaborate? (This is my own program, not an assigned one)

Re: Taking a square root in Java, answer appears as 0.0. Why?

Code Java:

public class FunMath
{
public double number;
}
public class TestFunMath
{
public static void main(String[] args)
{
FunMath n = new FunMath();
double number = 5;
System.out.println(n.number); // prints out 0
System.out.println(number); // prints out 5
}
}

Notice how there's nothing relating the variable number inside of TestFunMath.main and the field number inside FunMath except for their name. What you're doing is setting number inside of TestFunMath.main to the value. Instead, you should be setting the field number in FunMath (a very important distinction).

To do that, you can either declare number in FunMath as public (as I did, which is the lazy way) or create a setter method which will set the private field number in FunMath (which is the better way).

Code Java:

// inside of FunMath
public void setNumber(double value)
{
number = value;
}

Re: Taking a square root in Java, answer appears as 0.0. Why?

Thank you for the assistance. I haven't had time to work on my programming recently, but after taking a look at your advice, I finally found my error!

Behold, the working program of a noobie! ;)

Code :

public class FunMath {
public FunMath() {
factor = 0;
}
public void setNumber(double initialNumber){
factor = initialNumber;
}
public double getResult(){
double newFactor = Math.sqrt(factor);
factor = newFactor;
return factor;
}
private double factor;
}

Main

Code :

import java.util.*;
public class FunMathTester {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
FunMath factor = new FunMath();
System.out.print("Enter the number you wish to take a square root of: ");
double initialNumber = in.nextDouble();
factor.setNumber(initialNumber);
System.out.println(factor.getResult());
}
}

Please criticize me if there is anything too criticize. I'd like to make more complex math programs going forward as efficient and easy to understand as possible!

Re: Taking a square root in Java, answer appears as 0.0. Why?

Well, though how you have it is perfectly fine, at least that I'm aware of, you could possibly pass a double parameter called number to your class.

Code java:

public class FunMath {
public FunMath(double initialNumber) {
setNumber(initialNumber);
}
public void setNumber(double initialNumber){
factor = initialNumber;
}
public double getResult(){
double newFactor = Math.sqrt(factor);
factor = newFactor;
return factor;
}
private double factor;
}

Code java:

import java.util.*;
public class FunMathTester {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number you wish to take a square root of: ");
double initialNumber = in.nextDouble();
FunMath factor = new FunMath(initialNumber);
System.out.println(factor.getResult());
}
}

Re: Taking a square root in Java, answer appears as 0.0. Why?

you can try :

class SquareRoot

{

public static void findSqrt(double n)

{

double a=Math.sqrt(n);

System.out.print("Square Root="+a);

}

}

as a simple code..

Re: Taking a square root in Java, answer appears as 0.0. Why?

Quote:

Originally Posted by

**haibhailie**
you can try :

class SquareRoot

{

public static void findSqrt(double n)

{

double a=Math.sqrt(n);

System.out.print("Square Root="+a);

}

}

as a simple code..

This post is almost a year old (which is ancient in internet time), and your answer does not really add much to the discussion. Please do not resurrect old posts that have already been answered. Also, please read this: http://www.javaprogrammingforums.com...n-feeding.html