cse 2221 - naturalnumber

100
NaturalNumber 7 January 2019 OSU CSE 1

Upload: others

Post on 12-Apr-2022

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 2221 - NaturalNumber

NaturalNumber

7 January 2019 OSU CSE 1

Page 2: CSE 2221 - NaturalNumber

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

Page 3: CSE 2221 - NaturalNumber

Interfaces and Classes

7 January 2019 OSU CSE 3

NaturalNumber

NaturalNumber1L NaturalNumber2

implements implements

NaturalNumber-Kernel

extends

Standard

extends

Page 4: CSE 2221 - NaturalNumber

Interfaces and Classes

7 January 2019 OSU CSE 4

NaturalNumber

NaturalNumber1L NaturalNumber2

implements implements

NaturalNumber-Kernel

extends

Standard

extendsStandard has contracts

for three methods:clear

newInstancetransferFrom

Page 5: CSE 2221 - NaturalNumber

Interfaces and Classes

7 January 2019 OSU CSE 5

NaturalNumber

NaturalNumber1L NaturalNumber2

implements implements

NaturalNumber-Kernel

extends

Standard

extends

NaturalNumberKernelhas contracts for three

methods:multiplyBy10divideBy10isZero

Page 6: CSE 2221 - NaturalNumber

Interfaces and Classes

7 January 2019 OSU CSE 6

NaturalNumber

NaturalNumber1L NaturalNumber2

implements implements

NaturalNumber-Kernel

extends

Standard

extends

NaturalNumberhas contracts for 14 other

methods, e.g.,add

subtractetc.

Page 7: CSE 2221 - NaturalNumber

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

Page 8: CSE 2221 - NaturalNumber

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 8

This design goal in software engineering is

usually called single point of control over

change.

Page 9: CSE 2221 - NaturalNumber

The Kernel Interface

• The interface NaturalNumberKernelhas a minimal set of methods that are primitive in the NaturalNumbercomponent family– Separating these kernel (primary) methods

into their own interface identifies them as special in this regard

7 January 2019 OSU CSE 9

Page 10: CSE 2221 - NaturalNumber

The Kernel Interface

• The interface NaturalNumberKernelhas a minimal set of methods that are primitive in the NaturalNumbercomponent 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 of a component family.

Page 11: CSE 2221 - NaturalNumber

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

Page 12: CSE 2221 - NaturalNumber

Mathematical Model

• The value of a NaturalNumber variable is modeled as a non-negative integer

• Formally:NATURAL is integerexemplar n

constraint n >= 0

type NaturalNumber is modeled byNATURAL

7 January 2019 OSU CSE 12

Page 13: CSE 2221 - NaturalNumber

Mathematical Model

• The value of a NaturalNumber variable is modeled as a non-negative integer

• Formally:NATURAL is integerexemplar n

constraint n >= 0

type NaturalNumber is modeled byNATURAL

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.

Page 14: CSE 2221 - NaturalNumber

Mathematical Model

• The value of a NaturalNumber variable is modeled as a non-negative integer

• Formally:NATURAL is integerexemplar n

constraint n >= 0

type NaturalNumber is modeled byNATURAL

7 January 2019 OSU CSE 14

Second, we state that a NaturalNumbervariable has that

mathematical model.

Page 15: CSE 2221 - NaturalNumber

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

Page 16: CSE 2221 - NaturalNumber

No-argument Constructor

• A constructor with no parameters is called a no-argument constructor

• Ensures:this = 0

7 January 2019 OSU CSE 16

Page 17: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 17

Code State

NaturalNumber n = newNaturalNumber2();

Page 18: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 18

Code State

NaturalNumber n = newNaturalNumber2();

n = 0

Page 19: CSE 2221 - NaturalNumber

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

Page 20: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 20

Code State

k = 12345678909

NaturalNumber m = newNaturalNumber2(k);

Page 21: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 21

Code State

k = 12345678909

NaturalNumber m = newNaturalNumber2(k);

k = 12345678909m = 12345678909

Page 22: CSE 2221 - NaturalNumber

Constructor from int

• There is a constructor with one parameter int i

• Requires:i >= 0

• Ensures:this = i

7 January 2019 OSU CSE 22

Page 23: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 23

Code State

j = 13

NaturalNumber n = newNaturalNumber2(j);

Page 24: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 24

Code State

j = 13

NaturalNumber n = newNaturalNumber2(j);

j = 13n = 13

Page 25: CSE 2221 - NaturalNumber

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

Page 26: CSE 2221 - NaturalNumber

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

Page 27: CSE 2221 - NaturalNumber

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 NaturalNumbervalue resulting from the

constructor is what would have given you that String.

Page 28: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 28

Code State

s = "265"

NaturalNumber n = newNaturalNumber2(s);

Page 29: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 29

Code State

s = "265"

NaturalNumber n = newNaturalNumber2(s);

s = "265"n = 265

Page 30: CSE 2221 - NaturalNumber

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 typeNaturalNumber

7 January 2019 OSU CSE 30

Page 31: CSE 2221 - NaturalNumber

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 typeNaturalNumber

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.

Page 32: CSE 2221 - NaturalNumber

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

Page 33: CSE 2221 - NaturalNumber

add

void add(NaturalNumber n)

• Adds n to this.• Updates: this• Ensures:this = #this + n

7 January 2019 OSU CSE 33

Page 34: CSE 2221 - NaturalNumber

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.

Page 35: CSE 2221 - NaturalNumber

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.

Page 36: CSE 2221 - NaturalNumber

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 of a variable whose value might be changed is pronounced “old”;

#this denotes the old, or incoming, value of this.

Page 37: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 37

Code Statem = 143k = 70

m.add(k);

Page 38: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 38

Code Statem = 143k = 70

m.add(k);

m = 213k = 70

Page 39: CSE 2221 - NaturalNumber

subtract

void subtract(NaturalNumber n)

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

this >= n

• Ensures:this = #this - n

7 January 2019 OSU CSE 39

Page 40: CSE 2221 - NaturalNumber

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

Page 41: CSE 2221 - NaturalNumber

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

Page 42: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 42

Code Statem = 143k = 70

m.subtract(k);

Page 43: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 43

Code Statem = 143k = 70

m.subtract(k);

m = 73k = 70

Page 44: CSE 2221 - NaturalNumber

multiply

void multiply(NaturalNumber n)

• Multiplies this by n.• Updates: this• Ensures:this = #this * n

7 January 2019 OSU CSE 44

Page 45: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 45

Code Statem = 143k = 70

m.multiply(k);

Page 46: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 46

Code Statem = 143k = 70

m.multiply(k);

m = 10010k = 70

Page 47: CSE 2221 - NaturalNumber

divide

NaturalNumber divide(NaturalNumber n)

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

n > 0

• Ensures:#this = n * this + divide and0 <= divide < n

7 January 2019 OSU CSE 47

Page 48: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 48

Code Statem = 143k = 70

NaturalNumber r = m.divide(k);

Page 49: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 49

Code Statem = 143k = 70

NaturalNumber r = m.divide(k);

m = 2k = 70r = 3

Page 50: CSE 2221 - NaturalNumber

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

Page 51: CSE 2221 - NaturalNumber

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 of the ^ operator.

Page 52: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 52

Code Statem = 143k = 4

m.power(k);

Page 53: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 53

Code Statem = 143k = 4

m.power(k);

m = 418161601k = 4

Page 54: CSE 2221 - NaturalNumber

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

Page 55: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 55

Code Statem = 143k = 2

m.root(k);

Page 56: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 56

Code Statem = 143k = 2

m.root(k);

m = 11k = 2

Page 57: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 57

Code Statem = 144k = 2

m.root(k);

m = 12k = 2

Page 58: CSE 2221 - NaturalNumber

copyFrom

void copyFrom(NaturalNumber n)

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

7 January 2019 OSU CSE 58

Page 59: CSE 2221 - NaturalNumber

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

of the old value.

Page 60: CSE 2221 - NaturalNumber

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.

Page 61: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 61

Code Statem = 143k = 70

m.copyFrom(k);

Page 62: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 62

Code Statem = 143k = 70

m.copyFrom(k);

m = 70k = 70

Page 63: CSE 2221 - NaturalNumber

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

Page 64: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 64

Code Statem = 143k = 70

int comp = m.compareTo(k);

Page 65: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 65

Code Statem = 143k = 70

int comp = m.compareTo(k);

m = 143k = 70comp = 1

Page 66: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 66

Code Statem = 143k = 70

int comp = m.compareTo(k);

m = 143k = 70comp = 1

Though here the result of the method is 1, it could be any

positive int, so don’t assume it is 1.

Page 67: CSE 2221 - NaturalNumber

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

Page 68: CSE 2221 - NaturalNumber

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.

Page 69: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 69

Code Statem = 143d = 7

m.multiplyBy10(d);

Page 70: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 70

Code Statem = 143d = 7

m.multiplyBy10(d);

m = 1437d = 7

Page 71: CSE 2221 - NaturalNumber

divideBy10

int divideBy10()

• Divides this by 10 and returns the remainder.

• Updates: this• Ensures:#this = 10 * this + divideBy10 and0 <= divideBy10 < 10

7 January 2019 OSU CSE 71

Page 72: CSE 2221 - NaturalNumber

divideBy10

int divideBy10()

• Divides this by 10 and returns the remainder.

• Updates: this• Ensures:#this = 10 * this + divideBy10 and0 <= divideBy10 < 10

7 January 2019 OSU CSE 72

This is a kernel method.

Page 73: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 73

Code State

m = 1437

int r = m.divideBy10();

Page 74: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 74

Code State

m = 1437

int r = m.divideBy10();

m = 143r = 7

Page 75: CSE 2221 - NaturalNumber

isZero

boolean isZero()

• Reports whether this is zero.• Ensures:isZero = (this = 0)

7 January 2019 OSU CSE 75

Page 76: CSE 2221 - NaturalNumber

isZero

boolean isZero()

• Reports whether this is zero.• Ensures:isZero = (this = 0)

7 January 2019 OSU CSE 76

This is a kernel method.

Page 77: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 77

Code State

m = 143

boolean z = m.isZero();

Page 78: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 78

Code State

m = 143

boolean z = m.isZero();

m = 143z = false

Page 79: CSE 2221 - NaturalNumber

clear

void clear()• Resets this to an initial value.• Clears: this• Ensures:this = 0

7 January 2019 OSU CSE 79

Page 80: CSE 2221 - NaturalNumber

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.

Page 81: CSE 2221 - NaturalNumber

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.

Page 82: CSE 2221 - NaturalNumber

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.

Page 83: CSE 2221 - NaturalNumber

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.

Page 84: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 84

Code State

m = 143

m.clear();

Page 85: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 85

Code State

m = 143

m.clear();

m = 0

Page 86: CSE 2221 - NaturalNumber

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

Page 87: CSE 2221 - NaturalNumber

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.

Page 88: CSE 2221 - NaturalNumber

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.

Page 89: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 89

Code State

m = 143

NaturalNumber k =m.newInstance();

Page 90: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 90

Code State

m = 143

NaturalNumber k =m.newInstance();

m = 143k = 0

Page 91: CSE 2221 - NaturalNumber

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

Page 92: CSE 2221 - NaturalNumber

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.

Page 93: CSE 2221 - NaturalNumber

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.

Page 94: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 94

Code Statem = 143k = 70

m.transferFrom(k);

Page 95: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 95

Code Statem = 143k = 70

m.transferFrom(k);

m = 70k = 0

Page 96: CSE 2221 - NaturalNumber

Whoa! It Clears n?

• Did you notice that transferFromchanges 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

Page 97: CSE 2221 - NaturalNumber

toString

String toString()

• Returns the string representation of this.• Ensures:toString = [the string

representation of this]

7 January 2019 OSU CSE 97

Page 98: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 98

Code State

m = 143

String s =m.toString();

Page 99: CSE 2221 - NaturalNumber

Example

7 January 2019 OSU CSE 99

Code State

m = 143

String s =m.toString();

m = 143s = "143"

Page 100: CSE 2221 - NaturalNumber

Resources

• OSU CSE Components API: NaturalNumber– http://cse.osu.edu/software/common/doc/

7 January 2019 OSU CSE 100