Raising a number to an integral power I'm currently learning Java along with the Stanford online "Programming Methodology" course and, while it is awesome, I do lack any kind of human feedback.

This exercise was to create a method that took a floating-point value x and an integer k and returned x to the power of k. The method needed to also be able to correctly calculate the result when k is negative using the relationship \$x^{-k} = \frac{1}{x^k}\$.

/*
 * File: RealPower.java
 * ---------------------
 * This Method takes a floating-point value 'x'
 * and an integer value 'k' and returns 'x to the power
 * of k'. Method is written to correctly calculate
 * even when 'k' is negative.
 * Program displays a table of values generated using
 * the raiseRealToPower method. where x = PI 
 * and y is generated by 'i' variable in 'for' loop.
 * ---------------------
 * coded by Dartias
 */

import acm.program.*;

public class RealPower extends ConsoleProgram {

    public void run() {

        println("This program raises displays PI to the power of -4 through 4.");

/*
 * assigns 'x' the named constant value stored in PI        
 */


        double x = PI;

/*
 * this loop uses the raiseToRealPower method to generate the
 * values for powers of -4 through 0.
 */

        for(int i = -4; i &lt;= 0; i++){

            println(x + " to the power of "+ i + " is " + raiseRealToPower( x, i));
        }

/*
 * this loop uses the raiseToRealPower method to generate the
 * values for powers 1 through 4.
 */

        for(int i = 1; i &lt;= 4; i++){

            println(x + " to the power of "+ i + " is " + raiseRealToPower( x, i));
        }

    }


/*
 * this method calculates x to the power of k.
 * in the event that 'k' is a negative number
 * the method uses the Math.abs method to convert it
 * to a positive number and then uses the formula
 * x(power of -k) = (1/x(power of k)) to ensure the
 * negative exponential powers generate correctly.  
 */

private double raiseRealToPower(double x, int k){

    double total = x;

    if(k &gt;=0){
        for(int i = 1; i &lt; k; i++){
            total *= x;
        }} else {
            k = Math.abs(k);
            for(int i = 1; i &lt; k; i++){
                total *= x;
            }
            total = (1/total);
        }
    return total;
    }

/*
 * defines the named constant PI and assigns it a value.
 */

private static final double PI = 3.14159265;

}
Your function fails to calculate \$x^0\$ correctly. I would expect it to return Double.NaN when \$x = 0\$, and 1 otherwise.