Reading Contracts Flashcards
What is used to model each program type in Java (i.e. int double, String, NaturalNumber, etc.)?
A mathematical type - every variable of a certain program type has a value from its mathematical model’s mathematical space/domain
Mathematical type of boolean
boolean (true or false)
Mathematical type of char
character (‘A’, ‘?’, ‘z’, …)
Mathematical type of int
integer (-2147483648 through 2147483647)
Mathematical type of double
real (about 15 significant digits)
Mathematical type of String
string of character
Mathematical type of NaturalNumber
integer (non-negative)
Mathematical type of Queue<T>.</T>
String of T
Mathematical type of Sequence<T></T>
String of T
Mathematical type of Stack<T></T>
String of T
Mathematical type of Set<T></T>
finite set of T
Mathematical type of Map<K, V>
There are 2 ways to define the mathematical type of Map<K, V>:
1. finite set of ordered pairs of (K, V), with “function property” (i.e. the condition that no 2 pairs in the set have the same K value)
2. finite partial function from K to V
What constitutes a method contract? (Only name the parts)
A precondition (requires clause) and a postcondition (ensures clause)
Define precondition (requires clause)
A precondition (requires clause) is the part of a contract of a method that defines the responsibility of the program “calling” (using) that method (client code)
Define postcondition (ensures clause)
A postcondition (ensures clause) is the second part of a method that defines the responsibility of the program that implements that method (implementation code in the method body)
What does a contract mean/define/state?
When a method is called, if the precondition of its contract is true, then the method will terminate (i.e. return to the calling program) and when the method “does” return/terminate, the postcondition of its contract will be true.
If the precondition is not true, then the method may do “anything” - it can perform as intended, or it may not (including (not) terminate).
Set Notation (Mathematical)
Curly braces {a, b, …}; the set with no elements { } is called the empty set
String Notation (Mathematical)
Angle brackets <a, b, …>; the string with no elements < > is called the empty string; <x> is not the same as the element x by itself.</x>
Tuple Notation (Mathematical)
Parentheses (a, b, …); a tuple “may” contain different program/mathematical types of elements, order of an element identifies which element is which type/item, but the order itself may be arbitrary.
‘*’ symbol (Mathematical)
It can mean either of the two:
1. Concatenation between two “strings”
2. Multiply between two integers
Entries of a string (Mathematical)
The entries of a string are all the unique bits/items/elements of the string, taken as a set (entries of a string are considered by taking unique elements of the string without any duplicates and sorting them in any order. For instance, if you have a string of integers, and there are three occurrences of the integer 2, take 2 as only one entry of the string and not the n occurrences of the same integer as separate (n) entries of the string).
Perms of a string (Mathematical)
The perms of a string (permutation) means a string with all the same elements, in possibly a different order.
Rev() of a string (Mathematical)
Reverse of the string.
Substring notation (Mathematical)
s[x, y); the syntax s[x, y) means the substring of s that begins at position x and ends at position y - 1 (before y).
If x >= y, then s[x, y) = < > (empty string)
Universal quantification
Universal quantification is the part of a method contract that defines something (main-assertion) is true for every (for all) combination of values (quantified-variables) that satisfy a certain property (restriction-assertion).
Explain the Java representation of universal quantification
for all quantified-variables
where (restriction-assertion)
(main-assertion)
Existential quantification
Existential quantification is the part of a method contract that states something (assertion) is true for some (there exists) combination of values (quantified-variables).
Explain the Java representation of existential quantification
there exists quantified-variables
(assertion)
Is this universal or existential quantification: “Every non-empty string has positive length.” Also, show a Java representation.
Universal;
for all s: string of T
where (s /= < >)
(|s| > 0)
Is this universal or existential quantification: “Some positive integer is odd.” Also, show a Java representation.
Existential;
there exists n, k: integer
(n > 0 and n = k * 2 + 1)
|x| notation (Mathematical)
It can mean 3 things:
- If x is a Set, |x| is the size of the set x.
- If x is a String, |x| is the length of the string x.
- If x is a NUMERIC value, |x| is the absolute value.