import java.util.Scanner; /* ParseIntRecTester.java ** Java application for testing a recursive method that translates a ** decimal integer numeral into the corresponding int value. ** ** CMPS 144L ** Authors: R. McCloskey and < STUDENTS' NAMES > ** Known defects: ... */ public class ParseIntRecTester { private static final int INDENT_FACTOR = 2; private static final char DOUBLE_QUOTE = '\"'; private static Scanner keyboard; private static int depth; // main() method for testing purposes // ---------------------------------- public static void main(String[] args) { String firstPrompt = "Enter an integer numeral (empty string to quit): "; String secondPrompt = "Enter another integer numeral (empty string to quit): "; keyboard = new Scanner(System.in); String numeral = getResponse(firstPrompt).trim(); while (numeral.length() != 0) { int number; number = parseIntRec(numeral); //alternative to line above (to get narration): // depth = -1; // number = parseIntRecNarrated(numeral); System.out.println("\nNumeral " + doubleQuoted(numeral) + " translates to integer " + number + "\n"); numeral = getResponse(secondPrompt).trim(); } System.out.println("Goodbye."); } // method of interest // ------------------ /* Given a decimal integer numeral (i.e., a String that is a sequence ** of digit characters), returns the int value that the numeral represents. ** In other words, it does the same thing as Integer.parseInt(). ** ** Uses recursion! ** ** Example: "465" maps to 465, "3" maps to 3. ** ** pre: N > 0 && Character.isDigit(numeral.charAt(i)) for all i in [0..N), ** where N = numeral.length() */ public static int parseIntRec(String numeral) { int result = -1; final int N = numeral.length(); if (true) { // replace 'true' by base case condition result = 0; // replace '0' by an appropriate expression } else { // recursive case // Here goes a block of code that includes a recursive call // and that ultimately assigns the correct value to 'result' } return result; } /* Has same goal as the method above, but it narrates its own execution by ** printing messages indicating the value received via its formal parameter ** and the value that it is returning. */ public static int parseIntRecNarrated(String numeral) { depth = depth + 1; printlnIndented("Received " + doubleQuoted(numeral)); int result = -1; int N = numeral.length(); 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 includes a recursive call // and that ultimately assigns the correct value to 'result' } printlnIndented("Returning " + result + " having received " + doubleQuoted(numeral)); depth = depth - 1; return result; } /* Given a digit character, returns the corresponding int value. ** E.g., '4' maps to 4, '7' maps to 7. ** (Call this method in the base case of parseIntRec().) ** ** pre: Character.isDigit(digit) */ private static int digitToInt(char digit) { return digit - '0'; } /* Given a string of length one whose lone character is a digit, ** returns the corresponding int value. ** E.g., "4" maps to 4, "7" maps to 7. ** (One could call this method in the base case of parseIntRec().) ** ** pre: Character.isDigit(s.charAt(0)) */ private static int digitToInt(String s) { return digitToInt(s.charAt(0)); } // Methods supporting the self-narration of parseIntRecNarrated(). // --------------------------------------------------------------- /* Returns the string obtained by placing double quotes around the ** given string. */ private static String doubleQuoted(String s) { return DOUBLE_QUOTE + s + DOUBLE_QUOTE; } /* Prints the given string, indented in accord with the ** value of 'depth'; then skips to next line. */ private static void printlnIndented(String s) { printSpaces(INDENT_FACTOR * depth); System.out.println(s); } /* Prints the specified number of spaces. */ private static void printSpaces(int num) { for (int i=0; i != num; i++) { System.out.print(' '); } } /* 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(); } }