Chapter 25 Exercises: Object-Oriented COBOL
Exercise 25.1: Basic Class Definition
Difficulty: Beginner
Define a Customer class with the following:
- Instance data: customer ID (PIC X(8)), name (PIC X(40)), email (PIC X(60)), membership level (PIC X(1) — 'B' basic, 'S' silver, 'G' gold)
- Methods: Initialize, GetName, GetMembershipLevel, UpgradeMembership
- A factory method: CreateCustomer that assigns a sequential ID
Write the complete CLASS-ID source. Compile if you have access to Micro Focus COBOL; otherwise, desk-check the syntax against the examples in Section 25.2.
Exercise 25.2: Method with Business Logic
Difficulty: Intermediate
Add a CalculateDiscount method to the Customer class from Exercise 25.1. The discount depends on membership level:
| Level | Discount |
|---|---|
| Basic | 0% |
| Silver | 5% |
| Gold | 10% |
The method should accept a purchase amount (PIC S9(7)V99) and return a discount amount (PIC S9(7)V99). Write the complete METHOD-ID block.
Exercise 25.3: Inheritance Hierarchy
Difficulty: Intermediate
Create a LoanAccount subclass that INHERITS from the BankAccount class shown in Section 25.7. Include:
- Additional instance data: loan principal, interest rate, monthly payment, remaining term
- Override the
Withdrawmethod to prevent withdrawals (loans don't support withdrawals — return error code 7) - Add a
MakePaymentmethod that reduces the principal and calculates interest - Add a
GetPayoffAmountmethod that returns the total remaining balance
Exercise 25.4: Interface Implementation
Difficulty: Intermediate
Define an Auditable interface with methods:
GetAuditTrail— returns a formatted string of recent operationsGetLastModified— returns a date (PIC X(10))GetModifiedBy— returns a user ID (PIC X(8))
Then modify the CheckingAccount class to IMPLEMENT both Printable and Auditable interfaces.
Exercise 25.5: Collection Class
Difficulty: Advanced
Extend the AccountCollection class from Section 25.12 to support:
Remove— remove an account by indexFindByNumber— search for an account by account number (iterate and compare)GetHighestBalance— find and return the account with the highest balanceApplyToAll— accept a method name as PIC X(30) and invoke it on every account in the collection
Exercise 25.6: OO Design Analysis
Difficulty: Advanced (analytical)
Consider MedClaim's claim processing system. Design (on paper) a class hierarchy for claims:
Claim (base)
├── MedicalClaim
│ ├── InpatientClaim
│ └── OutpatientClaim
├── DentalClaim
├── VisionClaim
└── PharmacyClaim
For each class, identify: 1. What instance data it adds beyond the parent 2. Which methods it overrides 3. Which new methods it introduces 4. What interfaces it should implement (Adjudicatable, Payable, Reportable)
Write the CLASS-ID and METHOD-ID declarations (not full implementations) for at least three classes in the hierarchy.
Exercise 25.7: OO vs. Procedural Comparison
Difficulty: Intermediate (analytical)
Take the BankAccount OO class hierarchy from this chapter and write an equivalent procedural COBOL program that handles checking, savings, and loan accounts using:
- A single record layout with a type code field
- EVALUATE statements for type-specific behavior
- Separate paragraphs for each operation
Compare the two approaches in terms of: - Lines of code - Ease of adding a new account type - Readability for a new developer - Compiler requirements
Write a 500-word analysis of which approach you prefer and why.
Exercise 25.8: Factory Pattern Deep Dive
Difficulty: Advanced
Create a complete AccountFactory class that:
- Accepts an account type code ('CK', 'SV', 'LN')
- Creates the appropriate subclass (CheckingAccount, SavingsAccount, LoanAccount)
- Returns it as a BankAccount object reference (polymorphism)
- Logs each creation to a factory audit trail (instance data in the factory)
- Enforces a maximum number of accounts per type
This exercise combines the Factory pattern, the FACTORY section, typed and untyped object references, and encapsulation.