Tutorial 4: Data and Abstraction

Unit 1 ยท Section 4

Objectives

Data representation encodes meaning in bits, records, objects, or tables. An abstraction exposes useful operations while hiding representation. An invariant is a condition that valid data must maintain.

Stack abstraction: push, pop, peek
Invariant: pop returns the most recently pushed item

Exercises

  1. Represent a date in two different ways.
  2. Define an abstraction for a queue.
  3. State an invariant for a course enrollment record.

Self-check

  1. Why hide representation?
  2. What is an invariant?
  3. What operations define a queue?

Self-Check Quiz

1. Does abstraction eliminate implementation?

AnswerNo. It hides implementation details behind a stable interface.

2. What does an invariant describe?

AnswerA rule that must remain true for every valid state.

Homework

  1. Design a student-record abstraction.
  2. Specify operations, hidden fields, and invariants.
  3. Explain how changing representation should preserve clients.
Sample answerThe abstraction may expose enroll, withdraw, and status operations while hiding storage. An invariant prevents duplicate enrollment and requires a valid course code. Clients depend on operations, so internal fields can change without changing client code.