Time remaining:
Write my own absolute value function in Java

Computer Science
Tutor: None Selected Time limit: 1 Day

I tried   //My abs() method
    public static double abs(double d) {
       
        double s = d;
        if (d < 0.0) return -s;
        if (d >= 0.0) return s;
    }

But I keep getting a compile time error 

Error: This method must return a result of type double

Mar 29th, 2015

you can try this code....

public static double abs(double d) {

double s = d;

        if (d < 0.0) 

            return -s;

        else

            return s;

    }

you are using two if statement ....thats y u getting an error..

another aletrnative code for absolute function  is :

 public static double abs(double a) {

        return (a <= 0.0) ? 0.0 - a : a;

    }


Mar 28th, 2015

Thanks a lot. It works.


Mar 28th, 2015

Did you know? You can earn $20 for every friend you invite to Studypool!
Click here to
Refer a Friend
...
Mar 29th, 2015
...
Mar 29th, 2015
Dec 3rd, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer