import java.util.Scanner; /* ExponentiationTester.java ** Java application for testing a recursive method that raises a given ** base to a given (integer) power. ** ** Author: R. McCloskey and < lab team members > ** Known defects: ... */ public class ExponentiationTester { private static Scanner keyboard; private static int multCntr; // keeps track of # multiplications performed private static int depth; // keeps track of depth of recursion // Method of interest // ------------------ /* Returns x raised to the n-th power. ** Also increases the value of global variable multCntr by the number of ** multiplications done here. ** pre: n >= 0. */ public static double xToTheNRec(double x, int n) { double result; if (true) // replace 'true' by base case condition { result = 0; } // replace '0' by appropriate expression else { // recursive case // here goes a block of code that handles the two recursive cases, // ultimately assigning the correct number to 'result'. result = 0; } return result; } /* Does the same thing as the method above, but narrates its own ** execution by printing messages indicating the incoming parameter ** values and outgoing result. */ public static double xToTheNRecNarrated(double x, int n) { depth = depth + 1; println(depth, "Received x:" + x + "; n:" + n); double result; if (true) // replace 'true' by base case condition { result = 0; } // replace '0' by appropriate expression else { // recursive case // here goes a block of code that handles the two recursive cases, // ultimately assigning the correct number to 'result'. result = 0; } println(depth, "Returning " + result + " having received " + "x:" + x + "; n:" + n); depth = depth - 1; return result; } /* Returns the given number squared (and increments multCntr) */ private static double squareOf(double z) { multCntr++; return z*z; // Alternative to line above: // return Math.pow(z,2); } // Methods supporting the self-narration of xToTheNRecNarrated(). // -------------------------------------------------------------- /* Prints the given string, indented in accord with 'depth'. */ private static void print(int depth, String s) { printSpaces(3*depth); System.out.print(s); } /* Prints the given string, indented in accord with 'depth'; then ** skips to next line. */ private static void println(int depth, String s) { print(depth, s); System.out.println(); } /* Prints the specified number of spaces. */ private static void printSpaces(int num) { for (int i=0; i != num; i++) { System.out.print(' '); } } // main () method for testing // -------------------------- public static void main(String[] args) { final String basePrompt = "Enter a base (real number): "; final String exponentPrompt = "Enter an exponent (integer): "; keyboard = new Scanner(System.in); double base = Double.parseDouble(getResponse(basePrompt).trim()); int exponent = Integer.parseInt(getResponse(exponentPrompt).trim()); // Loop terminates when user enters a negative value for the exponent. while (exponent >= 0) { multCntr = 0; // reset the multiplication counter to zero //double result = xToTheNRec(base, exponent); depth = -1; double result = xToTheNRecNarrated(base, exponent); double correctResult = Math.pow(base,exponent); System.out.println("\nYour method produced " + result); System.out.println("Math.pow() produced " + correctResult); System.out.println("Your method performed " + multCntr + " multiplications.\n"); base = Double.parseDouble(getResponse(basePrompt).trim()); exponent = Integer.parseInt(getResponse(exponentPrompt).trim()); } } /* Displays the given prompt and returns the String entered at the keyboard ** in response. */ private static String getResponse(String prompt) { System.out.print(prompt); return keyboard.nextLine(); } }