Exercises — Chapter 9: Copybooks and Code Reuse

Exercise 9.1: Basic COPY Statement (Beginner)

Create a copybook called STU-REC that defines a student record with the following fields: - Student ID (10 characters) - Last name (25 characters) - First name (20 characters) - Major code (4 characters) with 88-level conditions for at least 5 majors - GPA (PIC 9V99) - Enrollment date (PIC 9(8) in YYYYMMDD format) - Student status (1 character) with 88-levels for Active, Graduated, Withdrawn, Suspended - FILLER (20 bytes for future expansion)

Then write a program that uses COPY STU-REC in the FILE SECTION and reads a sequential file of student records, displaying each student's name and GPA.

Deliverables: STU-REC.cpy, STU-LIST.cbl

Exercise 9.2: REPLACING Phrase (Beginner)

Using the IO-STATUS copybook pattern from Section 9.3 (with :PREFIX: replacement tokens), write a program that opens three files: - A student input file - A valid output file - A reject output file

Use COPY with REPLACING to create separate FILE STATUS fields for each file. Check status after every OPEN operation.

Deliverables: IO-STATUS.cpy, STU-VALID.cbl

Exercise 9.3: Multiple Copybooks in One Program (Intermediate)

Create a set of three copybooks for a simple order processing system: 1. ORD-REC — Order record (order number, customer ID, order date, total amount, status) 2. CUST-REC — Customer record (customer ID, name, address, phone, credit limit) 3. WS-COMMON — Common working-storage fields (date fields, counters, flags)

Write a program called ORD-PROC that uses all three copybooks and reads an order file, displaying a summary of each order with the record count at the end.

Deliverables: ORD-REC.cpy, CUST-REC.cpy, WS-COMMON.cpy, ORD-PROC.cbl

Exercise 9.4: Parameterized Copybook Design (Intermediate)

Design a parameterized copybook called ADDR-FIELDS that defines an address group (street, city, state, ZIP) with a :TAG: replacement token. Write a program that copies it four times to create: - HOME-ADDR fields - WORK-ADDR fields - MAIL-ADDR fields - SHIP-ADDR fields

Then populate each address from hard-coded values and display them with labels.

Deliverables: ADDR-FIELDS.cpy, ADDR-DEMO.cbl

Exercise 9.5: Nested Copybooks (Intermediate)

Create a nested copybook structure for an employee record: - EMP-REC (main) — includes sub-copybooks via COPY - EMP-HDR — Employee ID, hire date, department - EMP-NAME — Last name, first name, middle initial - EMP-ADDR — Use the ADDR-FIELDS copybook from Exercise 9.4 with REPLACING - EMP-PAY — Salary, pay grade, deduction total

Write a program that uses COPY EMP-REC and populates an employee record from hard-coded data, then displays it.

Deliverables: EMP-REC.cpy, EMP-HDR.cpy, EMP-NAME.cpy, EMP-PAY.cpy, EMP-DEMO.cbl

Exercise 9.6: Copybook for Multiple Record Types (Advanced)

Design a copybook set for a file that contains three record types (header, detail, trailer) used in an inventory system: - INV-HDR-REC — Header with file date, warehouse code, record count placeholder - INV-DTL-REC — Detail with item number, description, quantity on hand, unit price, reorder point - INV-TRL-REC — Trailer with record count, total value hash

Write a program that reads the multi-type file, routes each record to the appropriate processing paragraph using EVALUATE, and validates the trailer control totals.

Deliverables: INV-HDR-REC.cpy, INV-DTL-REC.cpy, INV-TRL-REC.cpy, INV-PROC.cbl

Exercise 9.7: Copybook Versioning Simulation (Advanced)

Simulate a copybook version change: 1. Create PROD-REC-V1 with a product record containing: product ID, name, price, category, and 40 bytes of FILLER. 2. Create PROD-REC-V2 that adds a 30-byte description field carved from FILLER (leaving 10 bytes). 3. Write Program A that uses V1 and writes sample records to a file. 4. Write Program B that uses V2 and reads the same file, displaying the new description field (which will contain spaces for records written by Program A).

Verify that V1-written records are readable by V2 programs. Document what would happen if V2 changed the record length.

Deliverables: PROD-V1.cpy, PROD-V2.cpy, PROD-WRITE.cbl, PROD-READ.cbl

Exercise 9.8: Building a Complete Copybook Library (Capstone)

Design and build a complete copybook library for a small library management system: - BOOK-REC — Book record (ISBN, title, author, publisher, year, genre, copies available) - MEMB-REC — Member record (member ID, name, phone, email, membership type, status) - LOAN-REC — Loan record (loan ID, member ID, ISBN, checkout date, due date, return date, status) - IO-STATUS — Generic file status (parameterized with :PREFIX:) - WS-DATES — Common date fields - CON-CODES — Return code constants

Then write a program called LOAN-PROC that reads a loan transaction file (using all relevant copybooks), validates each loan against the book and member records (using random reads on indexed files), and produces a summary report.

Deliverables: All 6 .cpy files plus LOAN-PROC.cbl

Reflection Questions

  1. What is the maximum number of REPLACING pairs you have used in a single COPY statement? When does it become too many?

  2. How would you handle a situation where two different copybooks define a field with the same name? What strategies prevent this?

  3. If a copybook change requires recompiling 200 programs, how would you manage the testing and deployment? What risks exist?

  4. Compare COBOL copybooks to header files in C or modules in Python. What are the strengths and limitations of each approach?