Lab: Program Pretty Print
Objective
In this lab you will practice manipulating Program values by
implementing and testing the Program prettyPrint secondary method.
Setup
To get started, import the project for this lab, ProgramPrettyPrint,
from the ProgramPrettyPrint.zip file available at this
link. If you don't remember how to do this,
see the Setup
instructions
in an earlier lab.
Method
- First carefully look through the file
Program1PrettyPrint1.java, which is an extension forProgram1that overrides theProgramSecondaryimplementation of theprettyPrintmethod, and make sure that you familiarize yourself with the methods provided. Note that it includes amainmethod used to test the implementation ofprettyPrint. - In
Program1PrettyPrint1.java, complete the body of theprettyPrintinstance method. Here are a few things to keep in mind:- The contract for
prettyPrintis informal and vague. To see what the output ofprettyPrintis supposed to look like, open theSampleProgram.blfile in thedatafolder. Except possibly for the order in which the two new instructions are listed, your implementation ofprettyPrintmust generate this output if called on theProgramobject corresponding to the SampleProgram BL program. The required indentation is defined as a constant,INDENT_SIZE, in thePrograminterface. - In your implementation of
ProgramprettyPrintyou can (and should) useStatementprettyPrint. (ProgramprettyPrintandStatementprettyPrinthave the same name, but different parameters. In particular, the receivers are different: in one case it is aProgramobject and in the other it is aStatementobject. This is an example of overloading, i.e., two methods that have the same name, but differ in their parameters—the number or the type or the order of the parameters. The bottom line is that, if in your code you invokeprettyPrinton aProgramreceiver,ProgramprettyPrintwill be executed; if you invokeprettyPrinton aStatementreceiver,StatementprettyPrintwill be executed instead. See the SW1 Concepts of Object-Oriented Programming slides (Slide 23, in particular) for the definition of overloading and how it is different from overriding. prettyPrintmust restore the value ofthis.- No recursion is needed to solve this problem.
- The contract for
- When you have completed the implementation of
prettyPrint, run theProgram1PrettyPrint1test program. You can use theSampleProgram.blfile in thedatafolder in your project as a test input. The main program generates two output files (both in thedatafolder):expected-output.txtthat contains the correct output for the given program andactual-output.txtthat is the output generated by your implementation. Eclipse provides a convenient way of comparing files. In the Package Explorer view, open thedatafolder and select both output files (click on the first one and then CTRL-click on the second one to select them at the same time). Then right-click on one of them and select Compare With > Each Other from the contextual menu. Eclipse will open a text compare window where the two files are shown side-by-side and any differences are highlighted and linked across the two views. Fix your code to remove any discrepancies between the correct output and your output.
Additional Activities
- Think about how you could automate testing of
prettyPrintwith JUnit. Write a JUnit test fixture to testProgramprettyPrint.