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