Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Square Root

  1. #1
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post

    Square Root

    Hi, I'm a beginner to Java coding and I am trying to come to understanding the code itself. I am attempting to create a small piece of software (BlueJ) which undertakes Pythagoras' theorem; working out x, y and z. So I have this line of code:
    Code:
        public int getx()
        {
            x = Math.sqrt((y*y)-(z*z));
        }
    What I am trying to achieve from this line of code is x=Square root(y^2-z^2). However, when I try to compile it directs me to the line: x = Math.sqrt((y*y)-(z*z));

    Please help

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Perhaps you need to declare your variable before using it.

    Compiler errors generally provide more than a line number when there's a problem. They can be confusing, but are still often helpful for determining the problem.

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    This is the whole code:

    Code:
    public class Pythagoras
    {
        private int x;
        private int y;
        private int z;
        
        public Pythagoras(int xlength, int ylength, int zlength)
        {
            x = xlength;
            y = ylength;
            z = zlength;
        }
        public int getx()
        {
            x = Math.sqrt((y*y)-(z*z));
            System.out.println("x = " + x);
        }
        public int gety()
        {
            y = Math.sqrt((z*z)-(x*x));
            System.out.println("y = " + y);
        }
        public int getz()
        {
            z = Math.sqrt((x*x)+(y*y));
            System.out.println("z = " + z);
        }
    }
    Can you see the problem?

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Yes, and the answer is still the same. x, y, and z are still not declared before use. Your functions (or "methods", I think Java calls them) have their own variable scope. So, x is not declared in getx(), y is not declared in gety(), etc.

    Have a Google for "variable scope" and you should find a load of resources to help understand the concept.

    By the way, also check out the "this" keyword. I'm a Java noob, but it should be something like: this.x = xlength; or this.z = Math.sqrt((x*x)+(y*y)); Use this to reference a class' own members and methods.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If I'm not mistaken, java requires the use of 'this' only when the context has been masked by a local scoped variable. So, using x within a method where x is a global variable is perfectly acceptable.
    The problem is simple - Math.sqrt is a double argument function, not an int function. You'll need to cast you're ints into doubles first. Even better, just change all of you're int references into doubles for simplicity. This function also returns a double, so you'll need to change you're method signature. I don't know the behaviour you want though, it looks like you're methods should redeclare the use of x, y, and z as returned values though, instead of altering the originals. Up to you, if you're calculations are correct, the Pythagoran theorem would say that it should be the same anyway. However, you'll need to get the sqrt of an absolute number - it will die if it ever hits a negative number.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    webguy08 (10-15-2008)

  • #6
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by webguy08 View Post
    Code:
    public class Pythagoras
    {
    ...
        public int getx()
        {
            x = Math.sqrt((y*y)-(z*z));
            System.out.println("x = " + x);
        }
        public int gety()
    ...
    }
    Can you see the problem?
    the return type of Math.sqrt is a double and since an int is of lower precision than a double you have to explicitly add a cast for the assignment to succeed. if you paid more attention to the the error message given by the compiler you could have easifly fixed the problem.
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #7
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Quote Originally Posted by shyam View Post
    the return type of Math.sqrt is a double and since an int is of lower precision than a double you have to explicitly add a cast for the assignment to succeed. if you paid more attention to the the error message given by the compiler you could have easifly fixed the problem.
    I only just started learning Java code so I didn't know what double was. The error message just stated "possible loss of precision", which doesn't mean much to me...

    So double is like int, but enables longer numbers to be used?

    How could I go about making the values in the constructor optional, so that users only have to enter 2 of the 3 values (either x, y, or z) rather than all 3?
    Last edited by webguy08; 10-15-2008 at 04:48 PM.

  • #8
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by webguy08 View Post
    So double is like int, but enables longer numbers to be used?
    you should review the primitive datatypes in java chapter...int is short for integer and double is used to store numbers with decimals
    Quote Originally Posted by webguy08 View Post
    How could I go about making the values in the constructor optional, so that users only have to enter 2 of the 3 values (either x, y, or z) rather than all 3?
    overload the constructor to have 2/3 parameters as you want
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #9
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Quote Originally Posted by shyam View Post
    you should review the primitive datatypes in java chapter...int is short for integer and double is used to store numbers with decimals

    overload the constructor to have 2/3 parameters as you want
    I knew what int was

    I don't understand what you mean by "overload" the constructor.

  • #10
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by webguy08 View Post
    I knew what int was
    good

    Quote Originally Posted by webguy08 View Post
    I don't understand what you mean by "overload" the constructor.
    maybe u missed the chapter on polymorphism...ok, here is an example

    Code:
    public class Pythagoras
    {
        private int x;
        private int y;
        private int z;
        
        public Pythagoras(int xlength, int ylength, int zlength)
        {
            x = xlength;
            y = ylength;
            z = zlength;
        }
        public Pythagoras(int xlen, int ylen) {
            this(xlen, ylen, 0);
        }
        public Pythagoras(int xlen) {
            this(xlen, 0, 0);
        }
    }
    notice that only the number of arguments to the constructor changes but the constructor name remains the same..(you could do that to methods as well, and instead of varying the number of arguments you can also change the type of arguments)
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    webguy08 (10-19-2008)

  • #11
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Oh yeh I forgot that I can create multiple constructors

    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •