Calculate exponent when not allowed to use Math.pow?
<p>I am a beginner in java and I am doing my practiceit exercises online. I am stuck at this question:</p>

<blockquote>
  <p>Write a method named pow2 (a variation of the previous pow exercise)
  that accepts a real number base and an integer exponent as parameters
  and returns the base raised to the given power. Your code should work
  for both positive and negative exponents. For example, the call
  pow2(2.0, -2) returns 0.25. Do not use Math.pow in your solution.</p>
</blockquote>

<p>This is what i have came up with.</p>

<pre><code>public double pow2(double x,int y){
    double total=1;
    for(int i=1;i&lt;=y;i++){
        total*=x;
    }
    return total;
}
</code></pre>

<p>But the problem is when I try to call <code>pow(2.0, -2)</code>, it returns me 1.0 instead. How should I approach doing this question? Another inquiry - I dont understand how it even returns me 1.0. Shouldn't it have been the result of 2*(-2)*(-2)?</p>
