# Help with method returning a double

• July 21st, 2011, 11:31 PM
Mike_Chase
Help with method returning a double
Hello Programmers,

This is my first post on my quest to self study Java more in depth since my recent graduation for my BSIT/Software Engineering Degree. I'm stuck on this exercise. I'm having trouble understanding why my method will not recognize that I am returning a double. Here is the requirements. And my code is below. I am using the acm libraries solely at this point and shouldn't use the Math libraries as this project is basically about reinventing the wheel. Finally, all my method projects to this point have been with int variable only. Thanks in advance.

Write a method raiseRealToPower that takes a floating-point value x and an integer
k and returns x^k. Implement your method so that it can correctly calculate the result
when k is negative, using the relationship
x^-k = 1 / x^k
Use your method to display a table of values of πk for all values of k from –4 to 4.
Code Java:

```import acm.program.*;   public class raiseRealToPower extends ConsoleProgram{   double PI = 3.1417; int p; double temp = 1.0;     public void run(){     for (int i = -4; i < 5; i++){   double result = power(PI, i); println("The result of PI to the power of" + i + " is : " + result);   }     }   private double power(double x, int k){ double num = x;   if (k<0) { p = -k;   } if (k == 0) { return 1.0;   }   for (int i = 1; i < p + 1; i++){ temp = num * temp;   }   if (k < 0) return (1/temp); if (k > 0) return temp;   }     }```
• July 21st, 2011, 11:37 PM
Junky
Re: Help with method returning a double

When posting code you should wrap it in code tags. Click the BB code link at the bottom of the page for more information.

"It doesn't work" which is basically the gist of your post provides us with zero information. Does your code compile? If not post the exact and full error message. Does it produces incorrect output? Then provide a sample of what the actual output is and the desired output should be.

In general the more relevant information you provide the easier it is for someone to spot your problem and guide you towards a solution.
• July 21st, 2011, 11:51 PM
Mike_Chase
Re: Help with method returning a double
Thanks for the quick reply. Ok, here goes. If there is anything else i need, I will post for you quickly. This is my first programming forum and I'm new to the format.

The error message during compiling is:
Severity and Description: This method must return a result of type double
Path: raiseIntToPower
Resource : raiseRealToPower.java
Location: line 23
Creation Time: 1311275429260
Id: 338

Code :

```import acm.program.*;   public class raiseRealToPower extends ConsoleProgram{   double PI = 3.1417; int p; double temp = 1.0;     public void run(){     for (int i = -4; i < 5; i++){   double result = power(PI, i); println("The result of PI to the power of" + i + " is : " + result);   }     }   private double power(double x, int k){ double num = x;   if (k<0) { p = -k;   } if (k == 0) { return 1.0;   }   for (int i = 1; i < p + 1; i++){ temp = num * temp;   }   if (k < 0) return (1/temp); if (k > 0) return temp;   }     }```
• July 21st, 2011, 11:55 PM
Junky
Re: Help with method returning a double
What happens when k == 0?
• July 22nd, 2011, 12:03 AM
Mike_Chase
Re: Help with method returning a double
It should return 1 as its answer since any number raised to the power of 0 = 1.
• July 22nd, 2011, 12:26 AM
Junky
Re: Help with method returning a double
You know that.
I know that.

But the compiler doesn't care about what happened earlier in the method. All it knows is that it can get to the end of your method, past the 2 if statements, and there is no return statement.
• July 22nd, 2011, 12:29 AM
Mike_Chase
Re: Help with method returning a double
Quote:

Originally Posted by Junky
You know that.
I know that.

But the compiler doesn't care about what happened earlier in the method. All it knows is that it can get to the end of your method, past the 2 if statements, and there is no return statement.

ok, I will refigure the logic. Any suggestions(or hints to think about?)on how to run the iteration from -4 to 4 without using the Math library and its absolute value, power methods?
• July 22nd, 2011, 12:35 AM
Junky
Re: Help with method returning a double
Quote:

Originally Posted by Mike_Chase
ok, I will refigure the logic.

Use an if/else statement rather than separate if statements.
Quote:

Any suggestions(or hints to think about?)on how to run the iteration from -4 to 4 without using the Math library and its absolute value, power methods?
I'm not sure what you are asking. What is wrong with how you have coded it? Other than magic numbers are frowned upon.

By the way you failed the very first requirement of the assignment: "Write a method raiseRealToPower "
• July 22nd, 2011, 01:03 AM
Mike_Chase
Re: Help with method returning a double
Thank you Junky for your advice, I have it working fine now. Here is my code now....
I was incorrectly coding the if else sequence. I put in some booleans to make it cleaner looking. Once the neg and zero are defined, like the switch statement, the last is the default and doesn't need an IF.

Code :

```import acm.program.*;   public class raiseRealToPower extends ConsoleProgram{   float PI = 3.1417f; float temp = 1.0f;     public void run(){     for (int i = -4; i < 5; i++){   float result = raiseRealToPower(PI, i); println("The result of PI to the power of " + i + " is : " + result);   } }   private float raiseRealToPower(float x, int k){   float num = x; int exp = k; boolean neg = exp < 0; boolean pos = exp > 0; boolean zero = exp == 0; float temp = 1f;   if (neg) {   for (int i = 1; i < -exp + 1; i++){ temp = temp * num; } return (1 / temp);   } else if (zero) { return temp;   } else { for (int i = 1; i < exp + 1; i++){ temp = temp * num; } return temp; } }   }```
• July 22nd, 2011, 01:09 AM
Mike_Chase
Re: Help with method returning a double
Quote:

Originally Posted by Junky
By the way you failed the very first requirement of the assignment: "Write a method raiseRealToPower "

Yes..no matter how trivial, I will pay strict attention to the requirements of the assignment. Notice I have changed my variables to float primitives as well. Thanks again Junky.