Reading Contracts Flashcards

1
Q

What is used to model each program type in Java (i.e. int double, String, NaturalNumber, etc.)?

A

A mathematical type - every variable of a certain program type has a value from its mathematical model’s mathematical space/domain

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Mathematical type of boolean

A

boolean (true or false)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Mathematical type of char

A

character (‘A’, ‘?’, ‘z’, …)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Mathematical type of int

A

integer (-2147483648 through 2147483647)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Mathematical type of double

A

real (about 15 significant digits)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Mathematical type of String

A

string of character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Mathematical type of NaturalNumber

A

integer (non-negative)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Mathematical type of Queue<T>.</T>

A

String of T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Mathematical type of Sequence<T></T>

A

String of T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Mathematical type of Stack<T></T>

A

String of T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Mathematical type of Set<T></T>

A

finite set of T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Mathematical type of Map<K, V>

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What constitutes a method contract? (Only name the parts)

A

A precondition (requires clause) and a postcondition (ensures clause)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define precondition (requires clause)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Define postcondition (ensures clause)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does a contract mean/define/state?

A

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).

17
Q

Set Notation (Mathematical)

A

Curly braces {a, b, …}; the set with no elements { } is called the empty set

18
Q

String Notation (Mathematical)

A

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>

19
Q

Tuple Notation (Mathematical)

A

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.

20
Q

‘*’ symbol (Mathematical)

A

It can mean either of the two:
1. Concatenation between two “strings”
2. Multiply between two integers

21
Q

Entries of a string (Mathematical)

A

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).

22
Q

Perms of a string (Mathematical)

A

The perms of a string (permutation) means a string with all the same elements, in possibly a different order.

23
Q

Rev() of a string (Mathematical)

A

Reverse of the string.

24
Q

Substring notation (Mathematical)

A

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)

25
Q

Universal quantification

A

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).

26
Q

Explain the Java representation of universal quantification

A

for all quantified-variables
where (restriction-assertion)
(main-assertion)

27
Q

Existential quantification

A

Existential quantification is the part of a method contract that states something (assertion) is true for some (there exists) combination of values (quantified-variables).

28
Q

Explain the Java representation of existential quantification

A

there exists quantified-variables
(assertion)

29
Q

Is this universal or existential quantification: “Every non-empty string has positive length.” Also, show a Java representation.

A

Universal;

for all s: string of T
where (s /= < >)
(|s| > 0)

30
Q

Is this universal or existential quantification: “Some positive integer is odd.” Also, show a Java representation.

A

Existential;

there exists n, k: integer
(n > 0 and n = k * 2 + 1)

31
Q

|x| notation (Mathematical)

A

It can mean 3 things:

  1. If x is a Set, |x| is the size of the set x.
  2. If x is a String, |x| is the length of the string x.
  3. If x is a NUMERIC value, |x| is the absolute value.