Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using recursion, complete the body of the following static method. [JAVA] /** *

ID: 3858862 • Letter: U

Question

Using recursion, complete the body of the following static method. [JAVA]

/**

* Refactors the given {@code Statement} so that every IF_ELSE statement

* with a negated condition (NEXT_IS_NOT_EMPTY, NEXT_IS_NOT_ENEMY,

* NEXT_IS_NOT_FRIEND, NEXT_IS_NOT_WALL) is replaced by an equivalent

* IF_ELSE with the opposite condition and the "then" and "else" BLOCKs

* switched. Every other statement is left unmodified.

*

* @param s

*            the {@code Statement}

* @updates s

* @ensures <pre>

* s = [#s refactored so that IF_ELSE statements with "not"

*   conditions are simplified so the "not" is removed]

* </pre>

*/

public static void simplifyIfElse(Statement s) {

    switch (s.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: {

            // nothing to do here...can you explain why?

            break;

        }

        default: {

            // this will never happen...can you explain why?

            break;

        }

    }

}

Explanation / Answer

private static String toStringCondition(Condition c) {
        String result;
        switch (c) {
            case NEXT_IS_EMPTY: {
                result = "next-is-empty";
                break;
            }
            case NEXT_IS_NOT_EMPTY: {
                result = "next-is-not-empty";
                break;
            }
            case NEXT_IS_ENEMY: {
                result = "next-is-enemy";
                break;
            }
            case NEXT_IS_NOT_ENEMY: {
                result = "next-is-not-enemy";
                break;
            }
            case NEXT_IS_FRIEND: {
                result = "next-is-friend";
                break;
            }
            case NEXT_IS_NOT_FRIEND: {
                result = "next-is-not-friend";
                break;
            }
            case NEXT_IS_WALL: {
                result = "next-is-wall";
                break;
            }
            case NEXT_IS_NOT_WALL: {
                result = "next-is-not-wall";
                break;
            }
            case RANDOM: {
                result = "random";
                break;
            }
            case TRUE: {
                result = "true";
                break;
            }
            default: {
                // this will never happen...
                result = "";
                break;
            }
        }
        return result;
    }

---------------------------------------------------------------------
private static void simplifyIfElse(Statement s,

Map<String, Statement> context, Sequence<Integer> cp) {

final int dummy = 0;

switch (s.kind()) {

case BLOCK: {

for (int i = 0; i < s.lengthOfBlock(); i++) {

Statement child = s.removeFromBlock(i);

simplifyIfElse(child, context, cp);

s.addToBlock(i, child);

}

break;

}

case IF: {

Statement b = s.newInstance();

Condition c = s.disassembleIf(b);

cp.add(cp.length(), conditionalJump(c).byteCode());

int jump = cp.length();

cp.add(cp.length(), dummy);

simplifyIfElse(b, context, cp);

cp.replaceEntry(jump, cp.length());

s.assembleIf(c, b);

break;

}

case IF_ELSE: {

Statement b = s.newInstance();

Statement b2 = s.newInstance();

Condition c = s.disassembleIfElse(b, b2);

cp.add(cp.length(), conditionalJump(c).byteCode());

int jump = cp.length();

cp.add(cp.length(), dummy);

simplifyIfElse(b, context, cp);

cp.replaceEntry(jump, cp.length() + 2);

cp.add(cp.length(), Instruction.JUMP.byteCode());

int jump2 = cp.length();

cp.add(cp.length(), dummy);

simplifyIfElse(b2, context, cp);

cp.replaceEntry(jump2, cp.length());

s.assembleIfElse(c, b, b2);

break;

}

case WHILE: {

Statement b = s.newInstance();

Condition c = s.disassembleWhile(b);

cp.add(cp.length(), conditionalJump(c).byteCode());

int jump = cp.length();

cp.add(cp.length(), dummy);

simplifyIfElse(b, context, cp);

cp.add(cp.length(), Instruction.JUMP.byteCode());

cp.add(cp.length(), jump - 1);

cp.replaceEntry(jump, cp.length() + 2);

s.assembleWhile(c, b);

break;

}

case CALL: {

String instruction = s.disassembleCall();

cp.add(cp.length(), Instruction.valueOf(instruction).byteCode());

s.assembleCall(instruction);

break;

}

default: {

// this will never happen...

break;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote