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

  1. First carefully look through the file Program1PrettyPrint1.java, which is an extension for Program1 that overrides the ProgramSecondary implementation of the prettyPrint method, and make sure that you familiarize yourself with the methods provided. Note that it includes a main method used to test the implementation of prettyPrint.
  2. In Program1PrettyPrint1.java, complete the body of the prettyPrint instance method. Here are a few things to keep in mind:
    • The contract for prettyPrint is informal and vague. To see what the output of prettyPrint is supposed to look like, open the SampleProgram.bl file in the data folder. Except possibly for the order in which the two new instructions are listed, your implementation of prettyPrint must generate this output if called on the Program object corresponding to the SampleProgram BL program. The required indentation is defined as a constant, INDENT_SIZE, in the Program interface.
    • In your implementation of Program prettyPrint you can (and should) use Statement prettyPrint. (Program prettyPrint and Statement prettyPrint have the same name, but different parameters. In particular, the receivers are different: in one case it is a Program object and in the other it is a Statement object. 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 invoke prettyPrint on a Program receiver, Program prettyPrint will be executed; if you invoke prettyPrint on a Statement receiver, Statement prettyPrint will 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.
    • prettyPrint must restore the value of this.
    • No recursion is needed to solve this problem.
  3. When you have completed the implementation of prettyPrint, run the Program1PrettyPrint1 test program. You can use the SampleProgram.bl file in the data folder in your project as a test input. The main program generates two output files (both in the data folder): expected-output.txt that contains the correct output for the given program and actual-output.txt that is the output generated by your implementation. Eclipse provides a convenient way of comparing files. In the Package Explorer view, open the data folder 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

  1. Think about how you could automate testing of prettyPrint with JUnit. Write a JUnit test fixture to test Program prettyPrint.