CPSC 225 Intermediate Programming Spring 2025

Lab 3: Correctness

Due: Thu Feb 20 at the start of lab

Introduction

Together, this lab and lab 2 address ensuring program correctness. Lab 2 focused on testing code to uncover bugs. This lab focuses on two other aspects: support for reasoning about correctness (identifying and checking preconditions, postconditions, class invariants, and loop invariants) and preventing mistakes in the first place (defensive programming and clean code naming practices).

About Due Dates

Labs are due at the start of lab on the date listed. It is OK to hand in your files at the very beginning of lab, but you should not be spending part or all of a lab period finishing up the previous week's lab.

To be eligible for revise-and-resubmit, you must turn in something by the due date. Late work and extensions are generally not accepted/granted; see the posted late policy for more information.

About Collaboration and Getting Help

You may work with a partner if you wish on part 1 only. You must do part 2 yourself.

If you do part I with a partner, only one person needs to hand in RomanNumeral.java — be sure to include both teammates' names in the file!

Otherwise you may get help but you may not use other resources (friends, neighbors, websites, ChatGPT, etc) to produce answers. See the posted policy on academic integrity for more information.

Handin

To hand in your work:


Exercises

Setup


Part 1 — Reasoning About Correctness

Setup

RomanNumeral.java contains an implementation of a class to represent Roman numerals. It may have bugs.

Your job in this part is to add information (comments) and checks relevant to correctness — things which should have been incorporated from the beginning as the code was being developed.

Defining Correct Behavior

The public comments (for the class and its public methods) define what correct behavior is.

Reasoning About Correctness

Class invariants, loop invariants, other invariants, and postconditions all address reasoning about correctness — writing them down makes you consciously think about how you can know that your code is working correctly, and checking them helps in testing and debugging. These should be written as you are developing the code in the first place.

Since RomanNumeral is intended to work with Roman numerals in standard form, it is reasonable that numeral_'s value should always be in standard form.

Realistically, it's only necessary to check the class invariant at the end of methods that change numeral_ — those are the only ones that might break something. Balance not pasting an extra check with the risk that a future change to the method might change numeral_.

Algorithms involving converting to and from Roman numerals and doing arithmetic with Roman numerals can be complicated. Additional invariants (including pre- and postconditions for private helper methods) can help back up assumptions and verify reasoning about the correctness of those algorithms.

Loop invariants can be used to check intermediate stages of algorithms.

Running


Part 2 — Defensive Programming and Clean Code

Setup

Code Review

Criteria to address: