Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Help with method returning a double

1. ## 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.
```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;

}

}```

2. ## 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.

3. ## 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

```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;

}

}```

4. ## Re: Help with method returning a double

What happens when k == 0?

5. ## Re: Help with method returning a double

It should return 1 as its answer since any number raised to the power of 0 = 1.

6. ## 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.

7. ## Re: Help with method returning a double

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?

8. ## Re: Help with method returning a double

Originally Posted by Mike_Chase
ok, I will refigure the logic.
Use an if/else statement rather than separate if statements.
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 "

9. ## The Following User Says Thank You to Junky For This Useful Post:

Mike_Chase (July 22nd, 2011)

10. ## 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.

```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;
}
}

}```

11. ## Re: Help with method returning a double

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.