Main:
import java.util.Scanner; public class Main { public static void main(String[] meow) { Scanner scan = new Scanner(System.in); Double d = scan.nextDouble(); // User must enter data PowerGenerator pg = new PowerGenerator(d); System.out.println(pg.next()); // 1.0 System.out.println(pg.next()); System.out.println(pg.next()); System.out.println(pg.next()); System.out.println(pg.next()); System.out.println(pg.next()); System.out.println(pg.next()); // d^6 } }
PowerGenerator:
public class PowerGenerator { private double factor; private int exponent; private double result = 1; public PowerGenerator(double aFactor) { factor = aFactor; exponent = 0; } public double next() { // Three different examples for writing this; two are // commented out // double returnValue = result; // result = result*factor; // return returnValue; double returnValue = Math.pow(factor, exponent); exponent = exponent + 1; return returnValue; // return Math.pow(factor, exponent++); } }