Package components.naturalnumber
Class NaturalNumber4
java.lang.Object
components.naturalnumber.NaturalNumberSecondary
components.naturalnumber.NaturalNumber4
- All Implemented Interfaces:
NaturalNumber
,NaturalNumberKernel
,Standard<NaturalNumber>
,Comparable<NaturalNumber>
NaturalNumber
represented as a Sequence<Integer>
with
implementations of primary methods.
Execution-time performance of all methods implemented in this class is O(1),
except the constructors from String
(which is O(|s
|)) and
from NaturalNumber
(which is O(log n
)).
- Mathematical Definitions:
HAS_ONLY_DIGITS ( s: string of integer ): boolean satisfies if s = empty_string then HAS_ONLY_DIGITS (s) = true else for all a: string of integer, k: integer where (s = a * <k>) (HAS_ONLY_DIGITS (s) = (HAS_ONLY_DIGITS (a) and 0 <= k < 10)) IS_WELL_FORMED_RADIX_REPRESENTATION ( s: string of integer ): boolean is s = empty_string or there exists k: integer, a: string of integer (s = <k> * a and 1 <= k < 10 and HAS_ONLY_DIGITS (a)) NUMERICAL_VALUE ( s: string of integer ): integer satisfies if s = empty_string then NUMERICAL_VALUE (s) = 0 else for all a: string of integer, k: integer where (s = a * <k>) (NUMERICAL_VALUE (s) = NUMERICAL_VALUE (a) * 10 + k) STRING_OF_DIGITS ( n: NATURAL ): string of integer if n = 0 then STRING_OF_DIGITS (n) = empty_string else STRING_OF_DIGITS (n) = STRING_OF_DIGITS (n/10) * <n mod 10>
- Representation Invariant (concrete invariant of $this):
IS_WELL_FORMED_RADIX_REPRESENTATION ($this.digits)
- Abstraction Relation (interpretation mapping between $this and this):
this = NUMERICAL_VALUE ($this.digits)
-
Field Summary
Fields inherited from interface components.naturalnumber.NaturalNumberKernel
RADIX
-
Constructor Summary
ConstructorDescriptionNo-argument constructor.NaturalNumber4
(int i) Constructor fromint
.Constructor fromNaturalNumber
.Constructor fromString
. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
clear()
Resetsthis
to an initial value.final int
Dividesthis
by 10 and reports the remainder.final boolean
isZero()
Reports whetherthis
is zero.final void
multiplyBy10
(int k) Multipliesthis
by 10 and addsk
.final NaturalNumber
Returns a new object with the same dynamic type asthis
, having an initial value.final void
transferFrom
(NaturalNumber source) Setsthis
to the incoming value ofsource
, and resetssource
to an initial value; the declaration notwithstanding, the dynamic type ofsource
must be the same as the dynamic type ofthis
.Methods inherited from class components.naturalnumber.NaturalNumberSecondary
add, canConvertToInt, canSetFromString, compareTo, copyFrom, decrement, divide, equals, hashCode, increment, multiply, power, root, setFromInt, setFromString, subtract, toInt, toString
-
Constructor Details
-
NaturalNumber4
public NaturalNumber4()No-argument constructor. -
NaturalNumber4
Constructor fromint
.- Parameters:
i
-int
to initialize from
-
NaturalNumber4
Constructor fromString
.- Parameters:
s
-String
to initialize from
-
NaturalNumber4
Constructor fromNaturalNumber
.- Parameters:
n
-NaturalNumber
to initialize from
-
-
Method Details
-
newInstance
Description copied from interface:Standard
Returns a new object with the same dynamic type asthis
, having an initial value. If the typeT
has a no-argument constructor, then the value of the new returned object satisfies the contract of the no-argument constructor forT
. IfT
does not have a no-argument constructor, then the value of the new returned object satisfies the contract of the constructor call that was used to initializethis
.- Returns:
- new object "like"
this
with an initial value
-
clear
Description copied from interface:Standard
Resetsthis
to an initial value. If the typeT
has a no-argument constructor, thenthis
satisfies the contract of the no-argument constructor forT
. IfT
does not have a no-argument constructor, thenthis
satisfies the contract of the constructor call that was used to initialize#this
. -
transferFrom
Description copied from interface:Standard
Setsthis
to the incoming value ofsource
, and resetssource
to an initial value; the declaration notwithstanding, the dynamic type ofsource
must be the same as the dynamic type ofthis
. If the typeT
has a no-argument constructor, thensource
satisfies the contract of the no-argument constructor forT
. IfT
does not have a no-argument constructor, thensource
satisfies the contract of the constructor call that was used to initialize#source
.- Parameters:
source
- object whose value is to be transferred
-
multiplyBy10
Description copied from interface:NaturalNumberKernel
Multipliesthis
by 10 and addsk
.- Parameters:
k
- theint
to be added
-
divideBy10
Description copied from interface:NaturalNumberKernel
Dividesthis
by 10 and reports the remainder.- Returns:
- the remainder
-
isZero
Description copied from interface:NaturalNumberKernel
Reports whetherthis
is zero.- Returns:
- true iff
this
is zero
-