Divide and Conquer Algorithm to calculate $a^n$
I think you are looking for something along these lines.

public double exp(float a, int n) {
    if(n&lt;=0) return 1; // just for safety saying &lt;= instead of == to allow
    if(n==1) return a;
    float floor_exp = exp(a,floor(n/2))
    if(floor(n/2) &lt; ceil(n/2)){
        return floor_exp * floor_exp * a;
    }
    else{
        return floor_exp * floor_exp;
    }
}


Note that floor and ceil are not functions in Java but in Python. Use these for Java. Also this will only work for n >= 0. The result is incorrect for the other case.