Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

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

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question 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
    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
    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());
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by helloworld922 View Post
    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)

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

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

    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).

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

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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!
     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

    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!

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.
     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;
    	}

    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());
    	}
     
    	}

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    My Mood
    Inspired
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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..

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

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

    Quote Originally Posted by haibhailie View Post
    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
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. JAVA MAGIC SQUARE
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 28th, 2012, 12:42 AM
  2. Java Calculator Square Root Function
    By laser1992 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 3rd, 2011, 09:34 AM
  3. Square root not working
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 06:25 PM
  4. Finding square root
    By Tracy22 in forum The Cafe
    Replies: 1
    Last Post: October 18th, 2010, 06:18 PM
  5. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM