Search code examples
javamathsqrt

Java, why is Math.sqrt() returning 0


I'm trying to check if a number is a square, and if a number is triangular. The issue is happening at sqrt(num) which is returning 0 for all numbers I test.
I'm using an online compiler, tried several compilers, so it's not a compiling issue. Tried to declare num as a double and as an int, same results.
I'm new to Java, but not new to programming, I searched online, checked my code several times, everything looks fine, it even worked as expected before adding the variables for checking triangular number, but after declaring the variables checkTri and checkTriSqr, this started to happen. I'm sure this have nothing to do with declaring these variables (almost sure), could anyone please help me understand what's going on here?

import static java.lang.Math.sqrt;
import static java.lang.Math.round;

public class Parent{

public static void main(String[] args){

    class Number
    {
        public int num ;
        double numSqr = sqrt(num );
        double roundNumSqr =  round(numSqr) ;
        double checkTri = 8 * num + 1 ;
        double checkTriSqr = sqrt(checkTri) ;

        public void prinTest()
        {
            System.out.println(num);
            System.out.println(numSqr);
            System.out.println(roundNumSqr);
            System.out.println(checkTri);
            System.out.println(checkTriSqr);
        }

        public boolean isSqr()
        {
            if (numSqr == roundNumSqr)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public boolean isTriangular(){

            if (checkTriSqr * checkTriSqr == checkTri )
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    Number number = new Number();

    number.num = 350;
    number.prinTest();
    System.out.println(number.isSqr());
    System.out.println(number.isTriangular());
 }
}

EDIT: The following screen shot is from the tutorial I'm following, concerning declaring classes within methods!

enter image description here


Solution

  • You can modify in this way and compare with technology you have worked on .

    import static java.lang.Math.sqrt;
    import static java.lang.Math.round;
    
    
    public class Number {
    
        public int num = 0;
    
        public void prinTest() {
    
            System.out.println(this.num);
            System.out.println(this.getSqrt(this.num));
            System.out.println(this.getCheckTri());
        }
    
        private double getSqrt(double value) {
            return sqrt(value);
        }
    
        public boolean isSqr() {
            if (this.getSqrt(this.num) == round(this.getSqrt(this.num))) {
                return true;
            } else {
                return false;
            }
        }
    
        private double getCheckTri() {
    
            return 8 * this.num + 1;
        }
    
        public boolean isTriangular() {
    
            if (this.getSqrt(this.getCheckTri()) * this.getSqrt(this.getCheckTri()) == this.getCheckTri()) {
                return true;
            } else {
                return false;
            }
        }
    
        public static void main(String[] args) {
    
            Number number = new Number();
    
            number.num = 49;
            number.prinTest();
            System.out.println(number.isSqr());
            System.out.println(number.isTriangular());
    
        }
    }
    

    You should read some basic tutorials as you have added class inside main method,which means you need more time to check out the syntax.