Time remaining:
Write my own absolute value function in Java

label Computer Science
account_circle Unassigned
schedule 1 Day
account_balance_wallet $5

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

Oct 18th, 2017

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

Studypool's Notebank makes it easy to buy and sell old notes, study guides, reviews, etc.
Click to visit
The Notebank
...
Oct 18th, 2017
...
Oct 18th, 2017
Oct 19th, 2017
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