Homework 4 : Abstraction Functions and Representation
Objective
- Utilize abstraction functions and representation invariants in ADT design.
- Fully specify a simple ADT.
Table Of Contents
Problems
- Abstraction Function and Representation Invariants. For each of the classes below, write the abstraction function and representation invariant.
- Abstraction Equivalence. Below are several snapshots of an
IntQueue2
object’s internal state at different points in a program. Which snapshots are equivalent to each other at the abstract level? In other words, partition the snapshots into groups based on which are identical to each other from the client’s perspective.
Rep Exposure. Below are signatures for various methods, computed properties, and initializers. For each, state and justify in 1-2 sentences whether such a method, property, or initializer could possibly expose the representation of an ADT containing that definition, given the information available. Explain any assumptions you made.
public func solveEquations(x: Int, y: Int, z: Int) -> Int
public func decode(slowly: Boolean) -> [String]
public String[] decode(boolean slowly)
(in Java)private func myBirthday() -> Date
public func makeIterator() -> Iterator<Element>
(assume an Iteratorobject is a generic object that allows you to iterate over the elements of the ADT) public var description : String
public init(cards : [Card])
- Specification. An interval is a compact representation of a contiguous set of numbers. Below is a class called
Interval
that implements intervals of Natural numbers (ie, intervals containing only 0 and positive integers). The concrete representation of an interval is given by two propertieslo
andhi
.
/**
A representation of a set of contiguous natural numbers.
**Abstract State**:
*/
public class Interval {
public let lo : Int
public let hi : Int
// Abstraction Function:
// Representation Invariant:
/**
Check the rep invariant.
**Effects**: nothing if this satisfies rep invariant;
otherwise aborts execution
*/
private func checkRep() {
...
}
/// -Returns: number of values in this interval
public var count : Int {
...
}
/**
Creates a new interval containing all integers from lo up to, and including, hi.
**Requires**:
**Modifies**:
**Effects**: Creates new interval for the given range.
- Parameter lo:
- Parameter hi:
*/
public init(lo: Int, hi: Int) {
self.lo = lo
self.hi = hi
}
/**
Determine whether a value x is contained in the interval.
**Requires**:
**Modifies**:
**Effects**:
- Parameter x:
- Returns:
*/
public func contains(_ x: Int) -> Bool {
...
}
/**
Compute the integer in this interval closest to the given
value x.
**Requires**:
**Modifies**:
**Effects**:
- Parameter value:
- Returns:
*/
public func clamp(_ x : Int) -> Int {
if x < lo {
return lo
} else if x > hi {
return hi
} else {
return x
}
}
}
(Aside: The Swift range operators, such as 0..<n
and 0...n
create Swift Range
objects capturing the same basic concept as our Interval
objects.)
Please complete the following steps for this class.
Add a suitable Rep Invariant for an
Interval
. There are several reasonable choices here. Your exact choice may impact how you answer some or all of the following parts.Regardless of the rep invariant you chose, complete the
checkRep()
method and insert calls tocheckRep()
wherever it should be checked in the code.Add a suitable Abstract State for an
Interval
, as well as any Abstract Invariants that restrict the valid abstract values forIntervals
.Add a suitable Abstraction Function for
Interval
.Complete the full specifications for
init
,contains
, andclamp
. (You can just fill in the necessary parts in the skeleton I provide above. There may be multiple correct ways to do this.)Write the getter for the
count
computed property.
Submission
Submit a paper copy of your answers at the beginning of class on the due date.
If you type your solution, you may use any format you like, e.g. plain text, LaTeX, Word, etc. If you’d like to use Markdown, here is a simple template file that you may use to get started.
When answering these questions, clarity is more important than length. The most credit will be given for the most concise, elegant, and complete answers.
Grading Criteria
All homework questions are graded out of five points, roughly as follows:
- 5: The solution is clear and correct. This solution would easily find a home in a textbook on the subject.
- 4: The solution contains a few minor mistakes, but they are of little significance overall.
- 3: The solution hits on the main points, but has at least one major gap in correctness or explanation.
- 2: The solution contains several mistakes, but parts of it are salvageable.
- 1: The solution misses the core concepts of the question.
- 0: No attempt is made at solving the problem.