Lab: Statement and Recursion
Objective
In this lab you will practice manipulating Statement values by
implementing and testing the static method countOfPrimitiveCalls.
Setup
To get started, import the project for this lab,
StatementCountPrimitiveCalls, from the
StatementCountPrimitiveCalls.zip file available at this
link. If you don't remember how to
do this, see the Setup
instructions
in an earlier lab.
Method
- Open the file
CountPrimitiveCalls.javain thesrcfolder and complete the body of the static methodcountOfPrimitiveCallsby pasting the code you wrote for the homework into the skeleton provided. - Open the
CountPrimitiveCallsTest.javaJUnit test fixture in thetestfolder and carefully review thecreateFromArgsmethod and its contract. One sample test case is provided as an example of how to testcountOfPrimitiveCalls(note that it uses the input filetest1.blin thedatafolder). If you have questions about the test fixture, make sure to ask an instructor. - Run the test fixture. If your implementation of
countOfPrimitiveCallsdoes not pass the test case provided, you need to debug your code. It might be helpful to create new (smaller) test cases that allow you to test each individual branch in theswitchstatement. Add the new test cases to the test fixture and test/debug your code until you believe it to be correct.
Additional Activities
-
Implement and test the following static method. You can copy and paste
countOfPrimitiveCallsand then update the contract and modify the header and the body./** * Reports the number of calls to a given instruction, {@code instr}, * in a given {@code Statement}. * * @param s * the {@code Statement} * @param instr * the instruction name * @return the number of calls to {@code instr} in {@code s} * @ensures countOfInstructionCalls = [number of calls to instr in s] */ public static int countOfInstructionCalls(Statement s, String instr) {...} -
Implement and test the following static method. You can copy and paste
countOfInstructionCallsand then update the contract and modify the header and the body./** * Refactors the given {@code Statement} by renaming every occurrence of * instruction {@code oldName} to {@code newName}. Every other statement is * left unmodified. * * @param s * the {@code Statement} * @param oldName * the name of the instruction to be renamed * @param newName * the new name of the renamed instruction * @updates s * @requires [newName is a valid IDENTIFIER] * @ensures <pre> * s = [#s refactored so that every occurrence of oldName is * replaced by newName] * </pre> */ public static void renameInstruction(Statement s, String oldName, String newName) {...}