NaturalNumber 7 January 2019 OSU CSE 1
NaturalNumber • The NaturalNumber component family allows you to manipulate natural numbers (i.e., non-negative integers) – Unlike an int variable, a NaturalNumber variable has no upper bound on its value – On the other hand, you need to call methods to do arithmetic; there are no nice built-in operators (e.g., + , – , * , == , < , …) or literals (e.g., 0 , 1 , 13 , …) as with int variables 7 January 2019 OSU CSE 2
Interfaces and Classes Standard extends NaturalNumber- Kernel extends NaturalNumber implements implements NaturalNumber1L NaturalNumber2 7 January 2019 OSU CSE 3
Interfaces and Classes Standard extends Standard has contracts NaturalNumber- for three methods: Kernel clear newInstance extends transferFrom NaturalNumber implements implements NaturalNumber1L NaturalNumber2 7 January 2019 OSU CSE 4
Interfaces and Classes Standard extends NaturalNumber- Kernel extends NaturalNumberKernel has contracts for three NaturalNumber methods: implements implements multiplyBy10 divideBy10 NaturalNumber1L NaturalNumber2 isZero 7 January 2019 OSU CSE 5
Interfaces and Classes NaturalNumber Standard has contracts for 14 other methods, e.g., extends add subtract NaturalNumber- etc. Kernel extends NaturalNumber implements implements NaturalNumber1L NaturalNumber2 7 January 2019 OSU CSE 6
The Standard Interface • The interface Standard has methods that are part of most (nearly all) OSU CSE component families – Separating the standard methods into their own interface means that these highly reused methods are described in exactly one place 7 January 2019 OSU CSE 7
The Standard Interface This design goal in software engineering is usually called single • The interface Standard has methods that point of control over are part of most (nearly all) OSU CSE change . component families – Separating the standard methods into their own interface means that these highly reused methods are described in exactly one place 7 January 2019 OSU CSE 8
The Kernel Interface • The interface NaturalNumberKernel has a minimal set of methods that are primitive in the NaturalNumber component family – Separating these kernel (primary) methods into their own interface identifies them as special in this regard 7 January 2019 OSU CSE 9
The Kernel Interface The choice of kernel methods is a key • The interface NaturalNumberKernel decision by the designer of a component family. has a minimal set of methods that are primitive in the NaturalNumber component family – Separating these kernel (primary) methods into their own interface identifies them as special in this regard 7 January 2019 OSU CSE 10
The Enhanced Interface • The interface NaturalNumber has all other methods that are convenient to have in the NaturalNumber component family – These secondary methods are often more “powerful” than the kernel methods and are introduced to make the component family readily usable in typical client code 7 January 2019 OSU CSE 11
Mathematical Model • The value of a NaturalNumber variable is modeled as a non-negative integer • Formally: NATURAL is integer exemplar n constraint n >= 0 type NaturalNumber is modeled by NATURAL 7 January 2019 OSU CSE 12
Mathematical Model First, we define the mathematical model • The value of a NaturalNumber variable we intend to use, including any is modeled as a non-negative integer constraints that limit the values it • Formally: might have. NATURAL is integer exemplar n constraint n >= 0 type NaturalNumber is modeled by NATURAL 7 January 2019 OSU CSE 13
Mathematical Model Second, we state that a • The value of a NaturalNumber variable NaturalNumber is modeled as a non-negative integer variable has that mathematical model. • Formally: NATURAL is integer exemplar n constraint n >= 0 type NaturalNumber is modeled by NATURAL 7 January 2019 OSU CSE 14
Constructors • There are four constructors for each implementation class • As always: – The name of the constructor is the name of the implementation class – Constructors differ only in their parameters – Each has its own contract (which is in the kernel interface NaturalNumberKernel ) 7 January 2019 OSU CSE 15
No-argument Constructor • A constructor with no parameters is called a no-argument constructor • Ensures: this = 0 7 January 2019 OSU CSE 16
Example Code State NaturalNumber n = new NaturalNumber2(); 7 January 2019 OSU CSE 17
Example Code State NaturalNumber n = new NaturalNumber2(); n = 0 7 January 2019 OSU CSE 18
Copy Constructor • There is a constructor with one parameter of the same type ( NaturalNumber n ), and it returns a copy of the parameter value so it is called a copy constructor • Ensures: this = n 7 January 2019 OSU CSE 19
Example Code State k = 12345678909 NaturalNumber m = new NaturalNumber2(k); 7 January 2019 OSU CSE 20
Example Code State k = 12345678909 NaturalNumber m = new NaturalNumber2(k); k = 12345678909 m = 12345678909 7 January 2019 OSU CSE 21
Constructor from int • There is a constructor with one parameter int i • Requires: i >= 0 • Ensures: this = i 7 January 2019 OSU CSE 22
Example Code State j = 13 NaturalNumber n = new NaturalNumber2(j); 7 January 2019 OSU CSE 23
Example Code State j = 13 NaturalNumber n = new NaturalNumber2(j); j = 13 n = 13 7 January 2019 OSU CSE 24
Constructor from String • There is a constructor with one parameter String s • Requires: there exists n: NATURAL (s = TO_STRING(n)) • Ensures: s = TO_STRING( this ) 7 January 2019 OSU CSE 25
Constructor from String In other words, s must look like • There is a constructor with one parameter the result of converting some String s NaturalNumber value to a String ... • Requires: there exists n: NATURAL (s = TO_STRING(n)) • Ensures: s = TO_STRING( this ) 7 January 2019 OSU CSE 26
Constructor from String ... and the NaturalNumber • There is a constructor with one parameter value resulting from the String s constructor is what would have given you that String . • Requires: there exists n: NATURAL (s = TO_STRING(n)) • Ensures: s = TO_STRING( this ) 7 January 2019 OSU CSE 27
Example Code State s = "265" NaturalNumber n = new NaturalNumber2(s); 7 January 2019 OSU CSE 28
Example Code State s = "265" NaturalNumber n = new NaturalNumber2(s); s = "265" n = 265 7 January 2019 OSU CSE 29
Methods for NaturalNumber • All the methods for NaturalNumber are instance methods , i.e., you call them as follows: n.methodName(arguments) where n is an initialized variable of type NaturalNumber 7 January 2019 OSU CSE 30
Methods for NaturalNumber • All the methods for NaturalNumber are instance methods , i.e., you call them as follows: n.methodName(arguments) where n is an initialized variable of type Recall: n is called the receiver ; NaturalNumber for all instance methods, the corresponding distinguished formal parameter implicitly has the name this . 7 January 2019 OSU CSE 31
Order of Presentation • The methods are introduced here starting with those you might expect to see as a client, and then proceeding to ones that might seem more surprising • Methods not discussed here: – setFromInt, canConvertToInt, toInt – setFromString, canSetFromString – increment, decrement 7 January 2019 OSU CSE 32
add void add(NaturalNumber n) • Adds n to this . • Updates: this • Ensures: this = # this + n 7 January 2019 OSU CSE 33
add void add(NaturalNumber n) • Adds n to this . • Updates: this • Ensures: this = # this + n The parameter mode called updates in a contract means the variable’s value might be changed by a call to the method. 7 January 2019 OSU CSE 34
add void add(NaturalNumber n) • Adds n to this . • Updates: this • Ensures: If this is an updates-mode this = # this + n parameter in any method, then the type in question is mutable . 7 January 2019 OSU CSE 35
add void add(NaturalNumber n) In an ensures clause, a # in front of a variable whose value might • Adds n to this . be changed is pronounced “old”; #this denotes the old, or • Updates: this incoming, value of this . • Ensures: this = # this + n 7 January 2019 OSU CSE 36
Example Code State m = 143 k = 70 m.add(k); 7 January 2019 OSU CSE 37
Example Code State m = 143 k = 70 m.add(k); m = 213 k = 70 7 January 2019 OSU CSE 38
subtract void subtract(NaturalNumber n) • Subtracts n from this . • Updates: this • Requires: this >= n • Ensures: this = # this - n 7 January 2019 OSU CSE 39
subtract void subtract(NaturalNumber n) • Subtracts n from this . Important! It could have been written as: • Updates: this # this = this + n • Requires: this >= n • Ensures: this = # this - n 7 January 2019 OSU CSE 40
subtract void subtract(NaturalNumber n) • Subtracts n from this . Or even as: • Updates: this this + n = # this • Requires: this >= n • Ensures: this = # this - n 7 January 2019 OSU CSE 41
Example Code State m = 143 k = 70 m.subtract(k); 7 January 2019 OSU CSE 42
Recommend
More recommend