NaturalNumber 7 January 2019 OSU CSE 1 NaturalNumber The - - PowerPoint PPT Presentation

naturalnumber
SMART_READER_LITE
LIVE PREVIEW

NaturalNumber 7 January 2019 OSU CSE 1 NaturalNumber The - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

NaturalNumber

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

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

  • perators (e.g., +, –, *, ==, <, …) or literals

(e.g., 0, 1, 13, …) as with int variables

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

Interfaces and Classes

7 January 2019 OSU CSE 3

NaturalNumber NaturalNumber1L NaturalNumber2 implements implements NaturalNumber- Kernel extends Standard extends

slide-4
SLIDE 4

Interfaces and Classes

7 January 2019 OSU CSE 4

NaturalNumber NaturalNumber1L NaturalNumber2 implements implements NaturalNumber- Kernel extends Standard extends

Standard has contracts for three methods: clear newInstance transferFrom

slide-5
SLIDE 5

Interfaces and Classes

7 January 2019 OSU CSE 5

NaturalNumber NaturalNumber1L NaturalNumber2 implements implements NaturalNumber- Kernel extends Standard extends

NaturalNumberKernel has contracts for three methods: multiplyBy10 divideBy10 isZero

slide-6
SLIDE 6

Interfaces and Classes

7 January 2019 OSU CSE 6

NaturalNumber NaturalNumber1L NaturalNumber2 implements implements NaturalNumber- Kernel extends Standard extends

NaturalNumber has contracts for 14 other methods, e.g., add subtract etc.

slide-7
SLIDE 7

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

  • wn interface means that these highly reused

methods are described in exactly one place

7 January 2019 OSU CSE 7

slide-8
SLIDE 8

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

  • wn interface means that these highly reused

methods are described in exactly one place

7 January 2019 OSU CSE 8

This design goal in software engineering is usually called single point of control over change.

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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 10

The choice of kernel methods is a key decision by the designer

  • f a component family.
slide-11
SLIDE 11

The Enhanced Interface

  • The interface NaturalNumber has all
  • ther 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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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 13

First, we define the mathematical model we intend to use, including any constraints that limit the values it might have.

slide-14
SLIDE 14

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 14

Second, we state that a NaturalNumber variable has that mathematical model.

slide-15
SLIDE 15

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

slide-16
SLIDE 16

No-argument Constructor

  • A constructor with no parameters is called

a no-argument constructor

  • Ensures:

this = 0

7 January 2019 OSU CSE 16

slide-17
SLIDE 17

Example

7 January 2019 OSU CSE 17

Code State

NaturalNumber n = new NaturalNumber2();

slide-18
SLIDE 18

Example

7 January 2019 OSU CSE 18

Code State

NaturalNumber n = new NaturalNumber2(); n = 0

slide-19
SLIDE 19

Copy Constructor

  • There is a constructor with one parameter
  • f 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

slide-20
SLIDE 20

Example

7 January 2019 OSU CSE 20

Code State

k = 12345678909 NaturalNumber m = new NaturalNumber2(k);

slide-21
SLIDE 21

Example

7 January 2019 OSU CSE 21

Code State

k = 12345678909 NaturalNumber m = new NaturalNumber2(k); k = 12345678909 m = 12345678909

slide-22
SLIDE 22

Constructor from int

  • There is a constructor with one parameter

int i

  • Requires:

i >= 0

  • Ensures:

this = i

7 January 2019 OSU CSE 22

slide-23
SLIDE 23

Example

7 January 2019 OSU CSE 23

Code State

j = 13 NaturalNumber n = new NaturalNumber2(j);

slide-24
SLIDE 24

Example

7 January 2019 OSU CSE 24

Code State

j = 13 NaturalNumber n = new NaturalNumber2(j); j = 13 n = 13

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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 26

In other words, s must look like the result of converting some NaturalNumber value to a String ...

slide-27
SLIDE 27

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 27

... and the NaturalNumber value resulting from the constructor is what would have given you that String.

slide-28
SLIDE 28

Example

7 January 2019 OSU CSE 28

Code State

s = "265" NaturalNumber n = new NaturalNumber2(s);

slide-29
SLIDE 29

Example

7 January 2019 OSU CSE 29

Code State

s = "265" NaturalNumber n = new NaturalNumber2(s); s = "265" n = 265

slide-30
SLIDE 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 NaturalNumber

7 January 2019 OSU CSE 30

slide-31
SLIDE 31

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 31

Recall: n is called the receiver; for all instance methods, the corresponding distinguished formal parameter implicitly has the name this.

slide-32
SLIDE 32

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

slide-33
SLIDE 33

add

void add(NaturalNumber n)

  • Adds n to this.
  • Updates: this
  • Ensures:

this = #this + n

7 January 2019 OSU CSE 33

slide-34
SLIDE 34

add

void add(NaturalNumber n)

  • Adds n to this.
  • Updates: this
  • Ensures:

this = #this + n

7 January 2019 OSU CSE 34

The parameter mode called updates in a contract means the variable’s value might be changed by a call to the method.

slide-35
SLIDE 35

add

void add(NaturalNumber n)

  • Adds n to this.
  • Updates: this
  • Ensures:

this = #this + n

7 January 2019 OSU CSE 35

If this is an updates-mode parameter in any method, then the type in question is mutable.

slide-36
SLIDE 36

add

void add(NaturalNumber n)

  • Adds n to this.
  • Updates: this
  • Ensures:

this = #this + n

7 January 2019 OSU CSE 36

In an ensures clause, a # in front

  • f a variable whose value might

be changed is pronounced “old”; #this denotes the old, or incoming, value of this.

slide-37
SLIDE 37

Example

7 January 2019 OSU CSE 37

Code State

m = 143 k = 70 m.add(k);

slide-38
SLIDE 38

Example

7 January 2019 OSU CSE 38

Code State

m = 143 k = 70 m.add(k); m = 213 k = 70

slide-39
SLIDE 39

subtract

void subtract(NaturalNumber n)

  • Subtracts n from this.
  • Updates: this
  • Requires:

this >= n

  • Ensures:

this = #this - n

7 January 2019 OSU CSE 39

slide-40
SLIDE 40

subtract

void subtract(NaturalNumber n)

  • Subtracts n from this.
  • Updates: this
  • Requires:

this >= n

  • Ensures:

this = #this - n

7 January 2019 OSU CSE 40

Important! It could have been written as: #this = this + n

slide-41
SLIDE 41

subtract

void subtract(NaturalNumber n)

  • Subtracts n from this.
  • Updates: this
  • Requires:

this >= n

  • Ensures:

this = #this - n

7 January 2019 OSU CSE 41

Or even as: this + n = #this

slide-42
SLIDE 42

Example

7 January 2019 OSU CSE 42

Code State

m = 143 k = 70 m.subtract(k);

slide-43
SLIDE 43

Example

7 January 2019 OSU CSE 43

Code State

m = 143 k = 70 m.subtract(k); m = 73 k = 70

slide-44
SLIDE 44

multiply

void multiply(NaturalNumber n)

  • Multiplies this by n.
  • Updates: this
  • Ensures:

this = #this * n

7 January 2019 OSU CSE 44

slide-45
SLIDE 45

Example

7 January 2019 OSU CSE 45

Code State

m = 143 k = 70 m.multiply(k);

slide-46
SLIDE 46

Example

7 January 2019 OSU CSE 46

Code State

m = 143 k = 70 m.multiply(k); m = 10010 k = 70

slide-47
SLIDE 47

divide

NaturalNumber divide(NaturalNumber n)

  • Divides this by n, returning the remainder.
  • Updates: this
  • Requires:

n > 0

  • Ensures:

#this = n * this + divide and 0 <= divide < n

7 January 2019 OSU CSE 47

slide-48
SLIDE 48

Example

7 January 2019 OSU CSE 48

Code State

m = 143 k = 70 NaturalNumber r = m.divide(k);

slide-49
SLIDE 49

Example

7 January 2019 OSU CSE 49

Code State

m = 143 k = 70 NaturalNumber r = m.divide(k); m = 2 k = 70 r = 3

slide-50
SLIDE 50

power

void power(int p)

  • Raises this to the power p.
  • Updates: this
  • Requires:

p >= 0

  • Ensures:

this = #this ^ (p)

7 January 2019 OSU CSE 50

slide-51
SLIDE 51

power

void power(int p)

  • Raises this to the power p.
  • Updates: this
  • Requires:

p >= 0

  • Ensures:

this = #this ^ (p)

7 January 2019 OSU CSE 51

Note: 0 ^ (0) = 1 by definition

  • f the ^ operator.
slide-52
SLIDE 52

Example

7 January 2019 OSU CSE 52

Code State

m = 143 k = 4 m.power(k);

slide-53
SLIDE 53

Example

7 January 2019 OSU CSE 53

Code State

m = 143 k = 4 m.power(k); m = 418161601 k = 4

slide-54
SLIDE 54

root

void root(int r)

  • Updates this to the r-th root of its incoming

value.

  • Updates: this
  • Requires:

r >= 2

  • Ensures:

this ^ (r) <= #this < (this + 1) ^ (r)

7 January 2019 OSU CSE 54

slide-55
SLIDE 55

Example

7 January 2019 OSU CSE 55

Code State

m = 143 k = 2 m.root(k);

slide-56
SLIDE 56

Example

7 January 2019 OSU CSE 56

Code State

m = 143 k = 2 m.root(k); m = 11 k = 2

slide-57
SLIDE 57

Example

7 January 2019 OSU CSE 57

Code State

m = 144 k = 2 m.root(k); m = 12 k = 2

slide-58
SLIDE 58

copyFrom

void copyFrom(NaturalNumber n)

  • Copies n to this.
  • Replaces: this
  • Ensures:

this = n

7 January 2019 OSU CSE 58

slide-59
SLIDE 59

copyFrom

void copyFrom(NaturalNumber n)

  • Copies n to this.
  • Replaces: this
  • Ensures:

this = n

7 January 2019 OSU CSE 59

The parameter mode called replaces in a contract means the variable’s value might be changed by a call to the method, but the new value is independent

  • f the old value.
slide-60
SLIDE 60

copyFrom

void copyFrom(NaturalNumber n)

  • Copies n to this.
  • Replaces: this
  • Ensures:

this = n

7 January 2019 OSU CSE 60

If this is a replaces-mode parameter in any method, then the type in question is mutable.

slide-61
SLIDE 61

Example

7 January 2019 OSU CSE 61

Code State

m = 143 k = 70 m.copyFrom(k);

slide-62
SLIDE 62

Example

7 January 2019 OSU CSE 62

Code State

m = 143 k = 70 m.copyFrom(k); m = 70 k = 70

slide-63
SLIDE 63

compareTo

int compareTo(NaturalNumber n)

  • Compares n to this, returning a negative

number if this < n, 0 if this = n, and a positive number if this > n

  • Ensures:

compareTo = [a negative number, zero, or a positive integer as this is less than, equal to, or greater than n]

7 January 2019 OSU CSE 63

slide-64
SLIDE 64

Example

7 January 2019 OSU CSE 64

Code State

m = 143 k = 70 int comp = m.compareTo(k);

slide-65
SLIDE 65

Example

7 January 2019 OSU CSE 65

Code State

m = 143 k = 70 int comp = m.compareTo(k); m = 143 k = 70 comp = 1

slide-66
SLIDE 66

Example

7 January 2019 OSU CSE 66

Code State

m = 143 k = 70 int comp = m.compareTo(k); m = 143 k = 70 comp = 1 Though here the result of the method is 1, it could be any positive int, so don’t assume it is 1.

slide-67
SLIDE 67

multiplyBy10

void multiplyBy10(int k)

  • Multiplies this by 10 and adds k.
  • Updates: this
  • Requires:

0 <= k < 10

  • Ensures:

this = 10 * #this + k

7 January 2019 OSU CSE 67

slide-68
SLIDE 68

multiplyBy10

void multiplyBy10(int k)

  • Multiplies this by 10 and adds k.
  • Updates: this
  • Requires:

0 <= k < 10

  • Ensures:

this = 10 * #this + k

7 January 2019 OSU CSE 68

This is a kernel method.

slide-69
SLIDE 69

Example

7 January 2019 OSU CSE 69

Code State

m = 143 d = 7 m.multiplyBy10(d);

slide-70
SLIDE 70

Example

7 January 2019 OSU CSE 70

Code State

m = 143 d = 7 m.multiplyBy10(d); m = 1437 d = 7

slide-71
SLIDE 71

divideBy10

int divideBy10()

  • Divides this by 10 and returns the

remainder.

  • Updates: this
  • Ensures:

#this = 10 * this + divideBy10 and 0 <= divideBy10 < 10

7 January 2019 OSU CSE 71

slide-72
SLIDE 72

divideBy10

int divideBy10()

  • Divides this by 10 and returns the

remainder.

  • Updates: this
  • Ensures:

#this = 10 * this + divideBy10 and 0 <= divideBy10 < 10

7 January 2019 OSU CSE 72

This is a kernel method.

slide-73
SLIDE 73

Example

7 January 2019 OSU CSE 73

Code State

m = 1437 int r = m.divideBy10();

slide-74
SLIDE 74

Example

7 January 2019 OSU CSE 74

Code State

m = 1437 int r = m.divideBy10(); m = 143 r = 7

slide-75
SLIDE 75

isZero

boolean isZero()

  • Reports whether this is zero.
  • Ensures:

isZero = (this = 0)

7 January 2019 OSU CSE 75

slide-76
SLIDE 76

isZero

boolean isZero()

  • Reports whether this is zero.
  • Ensures:

isZero = (this = 0)

7 January 2019 OSU CSE 76

This is a kernel method.

slide-77
SLIDE 77

Example

7 January 2019 OSU CSE 77

Code State

m = 143 boolean z = m.isZero();

slide-78
SLIDE 78

Example

7 January 2019 OSU CSE 78

Code State

m = 143 boolean z = m.isZero(); m = 143 z = false

slide-79
SLIDE 79

clear

void clear()

  • Resets this to an initial value.
  • Clears: this
  • Ensures:

this = 0

7 January 2019 OSU CSE 79

slide-80
SLIDE 80

clear

void clear()

  • Resets this to an initial value.
  • Clears: this
  • Ensures:

this = 0

7 January 2019 OSU CSE 80

This is a standard method.

slide-81
SLIDE 81

clear

void clear()

  • Resets this to an initial value.
  • Clears: this
  • Ensures:

this = 0

7 January 2019 OSU CSE 81

The parameter mode called clears in a contract means the variable’s value is reset to an initial value by a call to the method.

slide-82
SLIDE 82

clear

void clear()

  • Resets this to an initial value.
  • Clears: this
  • Ensures:

this = 0

7 January 2019 OSU CSE 82

If this is a clears-mode parameter in any method, then the type in question is mutable.

slide-83
SLIDE 83

clear

void clear()

  • Resets this to an initial value.
  • Clears: this
  • Ensures:

this = 0

7 January 2019 OSU CSE 83

The ensures clause is redundant in this case because this is a clears- mode parameter.

slide-84
SLIDE 84

Example

7 January 2019 OSU CSE 84

Code State

m = 143 m.clear();

slide-85
SLIDE 85

Example

7 January 2019 OSU CSE 85

Code State

m = 143 m.clear(); m = 0

slide-86
SLIDE 86

newInstance

NaturalNumber newInstance()

  • Returns a new object with the same

implementation as this, having an initial value.

  • Ensures:

newInstance = 0

7 January 2019 OSU CSE 86

slide-87
SLIDE 87

newInstance

NaturalNumber newInstance()

  • Returns a new object with the same

implementation as this, having an initial value.

  • Ensures:

newInstance = 0

7 January 2019 OSU CSE 87

This is a standard method.

slide-88
SLIDE 88

newInstance

NaturalNumber newInstance()

  • Returns a new object with the same

implementation as this, having an initial value.

  • Ensures:

newInstance = 0

7 January 2019 OSU CSE 88

This is similar to a constructor; the difference is that you don’t need to know the name of any implementation class to call this method.

slide-89
SLIDE 89

Example

7 January 2019 OSU CSE 89

Code State

m = 143 NaturalNumber k = m.newInstance();

slide-90
SLIDE 90

Example

7 January 2019 OSU CSE 90

Code State

m = 143 NaturalNumber k = m.newInstance(); m = 143 k = 0

slide-91
SLIDE 91

transferFrom

void transferFrom(NaturalNumber n)

  • Sets this to the incoming value of n, and

resets n to an initial value; n must be of the same implementation as this.

  • Replaces: this
  • Clears: n
  • Ensures:

this = #n

7 January 2019 OSU CSE 91

slide-92
SLIDE 92

transferFrom

void transferFrom(NaturalNumber n)

  • Sets this to the incoming value of n, and

resets n to an initial value; n must be of the same implementation as this.

  • Replaces: this
  • Clears: n
  • Ensures:

this = #n

7 January 2019 OSU CSE 92

This is a standard method.

slide-93
SLIDE 93

transferFrom

void transferFrom(NaturalNumber n)

  • Sets this to the incoming value of n, and

resets n to an initial value; n must be of the same implementation as this.

  • Replaces: this
  • Clears: n
  • Ensures:

this = #n

7 January 2019 OSU CSE 93

This is similar to copyFrom but is always more efficient, so it should be used if you don’t really need a duplicate.

slide-94
SLIDE 94

Example

7 January 2019 OSU CSE 94

Code State

m = 143 k = 70 m.transferFrom(k);

slide-95
SLIDE 95

Example

7 January 2019 OSU CSE 95

Code State

m = 143 k = 70 m.transferFrom(k); m = 70 k = 0

slide-96
SLIDE 96

Whoa! It Clears n?

  • Did you notice that transferFrom

changes the value of its argument? How can it do this? Didn’t we say that this can’t happen?

– It can’t for arguments of Java’s primitive types

  • There is a crucial difference between

Java’s primitive types and all other types, that allows this behavior for other types

– Details coming soon...

7 January 2019 OSU CSE 96

slide-97
SLIDE 97

toString

String toString()

  • Returns the string representation of this.
  • Ensures:

toString = [the string representation of this]

7 January 2019 OSU CSE 97

slide-98
SLIDE 98

Example

7 January 2019 OSU CSE 98

Code State

m = 143 String s = m.toString();

slide-99
SLIDE 99

Example

7 January 2019 OSU CSE 99

Code State

m = 143 String s = m.toString(); m = 143 s = "143"

slide-100
SLIDE 100

Resources

  • OSU CSE Components API:

NaturalNumber

– http://cse.osu.edu/software/common/doc/

7 January 2019 OSU CSE 100