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 TypeMethodDescriptionvoidadd(NaturalNumber n) Addsntothis.booleanReports whetherthisis small enough to convert toint.booleanReports whethersis of the right form to convert to aNaturalNumber.voidCopiesntothis.voidDecrementsthis.Dividesthisbyn, returning the remainder.voidIncrementsthis.voidMultipliesthisbyn.voidpower(int p) Raisesthisto the powerp.voidroot(int r) Updatesthisto ther-th root of its incoming value.voidsetFromInt(int i) Sets the value ofthistoi, wheniis non-negative.voidSets the value ofthisto the number whose standard decimal representation as aStringiss, whenshas the appropriate form (i.e.,sis the result of the functiontoStringfor someNaturalNumber).voidSubtractsnfromthis.inttoInt()Reports the value ofthisas anint, whenthisis small enough.Methods inherited from interface java.lang.Comparable
compareToMethods inherited from interface components.naturalnumber.NaturalNumberKernel
divideBy10, isZero, multiplyBy10Methods inherited from interface components.standard.Standard
clear, newInstance, transferFrom
-
Method Details
-
setFromInt
Sets the value ofthistoi, wheniis non-negative.- Parameters:
i- the new value- Replaces:
this- Requires:
i >= 0- Ensures:
this = i
-
canConvertToInt
boolean canConvertToInt()Reports whetherthisis small enough to convert toint.- Returns:
- true iff
thisis small enough - Ensures:
canConvertToInt = (this <= Integer.MAX_VALUE)
-
toInt
int toInt()Reports the value ofthisas anint, whenthisis small enough.- Returns:
- the value
- Requires:
this <= Integer.MAX_VALUE- Ensures:
toInt = this
-
canSetFromString
Reports whethersis 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- theStringto be converted- Returns:
- true iff
sis of the right form - Ensures:
canConvertToNatural = there exists n: NATURAL (s = TO_STRING(n))
-
setFromString
Sets the value ofthisto the number whose standard decimal representation as aStringiss, whenshas the appropriate form (i.e.,sis the result of the functiontoStringfor someNaturalNumber).- Parameters:
s- theStringto be converted- Replaces:
this- Requires:
there exists n: NATURAL (s = TO_STRING(n))- Ensures:
s = TO_STRING(this)
-
copyFrom
Copiesntothis.- Parameters:
n-NaturalNumberto 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
Addsntothis.- Parameters:
n-NaturalNumberto add- Updates:
this- Ensures:
this = #this + n
-
subtract
Subtractsnfromthis.- Parameters:
n-NaturalNumberto subtract- Updates:
this- Requires:
this >= n- Ensures:
this = #this - n
-
multiply
Multipliesthisbyn.- Parameters:
n-NaturalNumberto multiply by- Updates:
this- Ensures:
this = #this * n
-
divide
Dividesthisbyn, returning the remainder.- Parameters:
n-NaturalNumberto divide by- Returns:
- remainder after division
- Updates:
this- Requires:
n > 0- Ensures:
#this = this * n + divide and 0 <= divide < n
-
power
Raisesthisto the powerp.- Parameters:
p- power to raise to- Updates:
this- Requires:
p >= 0- Ensures:
this = #this ^ (p)
-
root
Updatesthisto ther-th root of its incoming value.- Parameters:
r- root- Updates:
this- Requires:
r >= 2- Ensures:
this ^ (r) <= #this < (this + 1) ^ (r)
-