This homework is necessary preparation for the lab. Make sure you type your code for prettyPrint and you bring the file to the lab so that you will not have to waste time entering your code during the lab.
- Using recursion, complete the body of the following
Statement instance method.
/** * Pretty prints {@code this} to the given stream {@code out} {@code offset} * spaces from the left margin using * {@link components.program.Program#INDENT_SIZE Program.INDENT_SIZE} spaces * for each indentation level. * * @param out * the output stream * @param offset * the number of spaces to be placed before every nonempty line * of output; nonempty lines of output that are indented further * will, of course, continue with even more spaces * @updates out.content * @requires out.is_open and 0 <= offset * @ensures <pre> * out.content = * #out.content * [this pretty printed offset spaces from the left margin * using Program.INDENT_SIZE spaces for indentation] * </pre> */ public void prettyPrint(SimpleWriter out, int offset) { switch (this.kind()) { case BLOCK: { // TODO - fill in case break; } case IF: { // TODO - fill in case break; } case IF_ELSE: { // TODO - fill in case break; } case WHILE: { // TODO - fill in case break; } case CALL: { // TODO - fill in case break; } default: { // this will never happen... break; } } }
Here is an example of what prettyPrint should output (with offset 0 from the left edge of the page):
IF next-is-not-enemy THEN turnleft WHILE true DO IF random THEN move turnback IF next-is-not-wall THEN move END IF infect move ELSE go-for-it WHILE next-is-empty DO END WHILE turnleft turnright END IF END WHILE turnright skip skip-again END IF
In lab you will be given implementations for the following two static methods needed to implement prettyPrint.
/** * Prints the given number of spaces to the given output stream. * * @param out * the output stream * @param numSpaces * the number of spaces to print * @updates out.content * @requires out.is_open and spaces >= 0 * @ensures out.content = #out.content * [numSpaces spaces] */ private static void printSpaces(SimpleWriter out, int numSpaces) {...}
/** * Converts c into the corresponding BL condition. * * @param c * the Condition to convert * @return the BL condition corresponding to c * @ensures toStringCondition = [BL condition corresponding to c] */ private static String toStringCondition(Condition c) {...}