Package components.naturalnumber
Interface NaturalNumber
- All Superinterfaces:
Comparable<NaturalNumber>
,NaturalNumberKernel
,Standard<NaturalNumber>
- All Known Implementing Classes:
NaturalNumber1L
,NaturalNumber2
,NaturalNumber3
,NaturalNumber4
,NaturalNumberSecondary
NaturalNumberKernel
enhanced with secondary methods.-
Field Summary
Fields inherited from interface components.naturalnumber.NaturalNumberKernel
RADIX
-
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(NaturalNumber n) Addsn
tothis
.boolean
Reports whetherthis
is small enough to convert toint
.boolean
Reports whethers
is of the right form to convert to aNaturalNumber
.void
Copiesn
tothis
.void
Decrementsthis
.Dividesthis
byn
, returning the remainder.void
Incrementsthis
.void
Multipliesthis
byn
.void
power
(int p) Raisesthis
to the powerp
.void
root
(int r) Updatesthis
to ther
-th root of its incoming value.void
setFromInt
(int i) Sets the value ofthis
toi
, wheni
is non-negative.void
Sets the value ofthis
to the number whose standard decimal representation as aString
iss
, whens
has the appropriate form (i.e.,s
is the result of the functiontoString
for someNaturalNumber
).void
Subtractsn
fromthis
.int
toInt()
Reports the value ofthis
as anint
, whenthis
is small enough.Methods inherited from interface java.lang.Comparable
compareTo
Methods inherited from interface components.naturalnumber.NaturalNumberKernel
divideBy10, isZero, multiplyBy10
Methods inherited from interface components.standard.Standard
clear, newInstance, transferFrom
-
Method Details
-
setFromInt
Sets the value ofthis
toi
, wheni
is non-negative.- Parameters:
i
- the new value- Replaces:
this
- Requires:
i >= 0
- Ensures:
this = i
-
canConvertToInt
boolean canConvertToInt()Reports whetherthis
is small enough to convert toint
.- Returns:
- true iff
this
is small enough - Ensures:
canConvertToInt = (this <= Integer.MAX_VALUE)
-
toInt
int toInt()Reports the value ofthis
as anint
, whenthis
is small enough.- Returns:
- the value
- Requires:
this <= Integer.MAX_VALUE
- Ensures:
toInt = this
-
canSetFromString
Reports whethers
is of the right form to convert to aNaturalNumber
. Note that this is an instance method and needs to be called with a distinguished argument even though the corresponding parameter (this
) is not going to be used. This method should be a static method but currently in Java static methods cannot be declared in interfaces.- Parameters:
s
- theString
to be converted- Returns:
- true iff
s
is of the right form - Ensures:
canConvertToNatural = there exists n: NATURAL (s = TO_STRING(n))
-
setFromString
Sets the value ofthis
to the number whose standard decimal representation as aString
iss
, whens
has the appropriate form (i.e.,s
is the result of the functiontoString
for someNaturalNumber
).- Parameters:
s
- theString
to be converted- Replaces:
this
- Requires:
there exists n: NATURAL (s = TO_STRING(n))
- Ensures:
s = TO_STRING(this)
-
copyFrom
Copiesn
tothis
.- Parameters:
n
-NaturalNumber
to copy from- Replaces:
this
- Ensures:
this = n
-
increment
void increment()Incrementsthis
.- Updates:
this
- Ensures:
this = #this + 1
-
decrement
void decrement()Decrementsthis
.- Updates:
this
- Requires:
this > 0
- Ensures:
this = #this - 1
-
add
Addsn
tothis
.- Parameters:
n
-NaturalNumber
to add- Updates:
this
- Ensures:
this = #this + n
-
subtract
Subtractsn
fromthis
.- Parameters:
n
-NaturalNumber
to subtract- Updates:
this
- Requires:
this >= n
- Ensures:
this = #this - n
-
multiply
Multipliesthis
byn
.- Parameters:
n
-NaturalNumber
to multiply by- Updates:
this
- Ensures:
this = #this * n
-
divide
Dividesthis
byn
, returning the remainder.- Parameters:
n
-NaturalNumber
to divide by- Returns:
- remainder after division
- Updates:
this
- Requires:
n > 0
- Ensures:
#this = this * n + divide and 0 <= divide < n
-
power
Raisesthis
to the powerp
.- Parameters:
p
- power to raise to- Updates:
this
- Requires:
p >= 0
- Ensures:
this = #this ^ (p)
-
root
Updatesthis
to ther
-th root of its incoming value.- Parameters:
r
- root- Updates:
this
- Requires:
r >= 2
- Ensures:
this ^ (r) <= #this < (this + 1) ^ (r)
-