is possible to work of return statement with Math Class?
eg,

return Math.pow(2,2);

Recommended Answers

All 17 Replies

Yes, but you need to return the correct type. I think it is double.

I think the @Taywin meaning

Double myCalculation (double first, double second) {
   double someDouble = Math.pow(first, second);
   return someDouble;
}

couldn't you do

public double (double number1, double number2){
return (Math.pow(number1, number2));
}

Yes, you could.

but what if i just want return it inside the return statement?
because tutorial question require to do an expression with return a power. :(

@gahhon: I think the above examples, specially that of sirlink99's (maybe he forgot to give a name to his method) did exactly that...or maybe i don't understand your question...

In case you didn't understand sirlink99's example, here's it once again (with a method name, this time):

public double myPow(double number1, double number2) {
    return Math.pow(number1, number2);
}

The braces around the ((Math.pow(...)) is not necessary; don't know if they were confusing you...:)

oh, but why must be in double, cannot be integer type?
nowander i can't work with integer type.

Math.pow returns a double, but you can convert that to an int in your method and return the int, if that's what you want.

but how to convert directly from method?

Sorry, don't understand that question. Can you be more explicit?

which mean how can i convert to int in my method??

Here's an example of casting a double to int. You can use the same technique in your method

double d = 12.3;
int i = (int) d;
return (int)Math.pow(3,2);

this is working but how about this

public static int convert(double x, double y)

is possible to convert it to integer type, when return?

convert what to int?
you pass in two doubles and return one int. What is the relationship between the doubles and the returned value?

i am trying to ask is the double x and double y possible to convert into Int inside the bracket?

You can convert any double to an int anywhere you want by casting it (like in my previous example).
ps: A double can store values that have more precision than an int, or are larger than the largest possible int, so it's up to you to think about what the values are going to be and what result you expect from converting them.

oh okay thank you dude. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.