/** Boston University, Department of Computer Science CS 112 Lab 01: Recursion Teaching Fellow: Diane H. Theriault, deht@cs.bu.edu */ import java.io.*; public class PowerRunner { public static void main(String[] args) { System.out.println("Hello CS112! This program computes x^n."); System.out.println("Please enter x:"); int x = getNumberFromKeyboard(); System.out.println("Please enter n:"); int n = getNumberFromKeyboard(); if(x < 0 || n < 0) { System.out.println("This program only works with non-negative integers"); return; //exit } else { System.out.println("Computing " + x + "^" + n); PowerComputer computer = new PowerComputer(); double myResult = computer.exponentiate(x, n); double javaResult = Math.pow(x,n); System.out.println("My result: " + myResult + ". Java's result: " + javaResult); int iterations = computer.getNumIterations(); System.out.println("Iterations needed: "+ iterations); } } public static int getNumberFromKeyboard() { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = null; int answer = -1; try{ s = in.readLine(); if(s == null || s.length() == 0) { System.out.println("String from keyboard is empty"); return -1; } answer = Integer.parseInt(s); } catch(java.io.IOException exception) { System.out.println("Problem reading the line"); } catch(java.lang.NumberFormatException exception) { System.out.println("Unable to convert this string to an integer: " + s); } return answer; } }