S
Stefan Ram
Here is an algorithm for the determination of the leap-year
property of a year:
public static boolean
isLeapYear( final int yearNumber )
{ final boolean isQuad = yearNumber % 4 == 0;
final boolean isCentury = yearNumber % 100 == 0;
final boolean isQuadcentury = yearNumber % 400 == 0;
final boolean isCanceled = isCentury & !isQuadcentury;
final boolean isLeapYear = isQuad & !isCanceled;
return isLeapYear; }
But this will only handle years up to about 2^31-1 or so!
What will happen, when wants to use BigInteger for years?
The algorithm actually only uses the property of years to
be congruent to a number modulo another number:
public interface IsCongruentModuloIntInt
{ public boolean isCongruentModulo( int comparand, int modulus ); }
So there is no need to bind it to the special type »int« that
has many more features which are not needed here. The
following untested draft shows how the algorithm can be freed
from this binding.
public static boolean
( final IsCongruentModuloIntInt yearNumber )
{ final boolean isQuad = yearNumber.isCongruentModulo( 0, 4 );
final boolean isCentury = yearNumber.isCongruentModulo( 0, 100 );
final boolean isQuadcentury = yearNumber.isCongruentModulo( 0, 400 );
final boolean isCanceled = isCentury & !isQuadcentury;
final boolean isLeapYear = isQuad & !isCanceled;
result isLeapYear; }
If an algorithm needs more features of a year than just a
single feature, it can be written as follows stating exactly
which operations it needs (untested draft):
<T extends AddInt & IsPositive & DividedByInt >
public static int
difference( final T yearParameter )
{ if( yearParameter.isPositive() )yearParameter.addInt( - 1 )
quads = yearParameter.DividedByInt( 4 ); /* ... */ }
So each algorithm is generic to the maximum extend possible,
if it binds itself to the smallest interface it needs.
The interfaces »IsCongruentModuloIntInt« still is somewhat
bound to »int«, but the generalization achieved here
usually will be sufficient. A more general version could be:
public interface IsCongruentModulo<T,U,V>
{ public boolean isCongruentModulo( T comparand, U comparand1, V modulus ); }
An important part of this programming style was removed
in this posting for brevity: The JavaDoc-comments which
state what the meaning of this operation is.
property of a year:
public static boolean
isLeapYear( final int yearNumber )
{ final boolean isQuad = yearNumber % 4 == 0;
final boolean isCentury = yearNumber % 100 == 0;
final boolean isQuadcentury = yearNumber % 400 == 0;
final boolean isCanceled = isCentury & !isQuadcentury;
final boolean isLeapYear = isQuad & !isCanceled;
return isLeapYear; }
But this will only handle years up to about 2^31-1 or so!
What will happen, when wants to use BigInteger for years?
The algorithm actually only uses the property of years to
be congruent to a number modulo another number:
public interface IsCongruentModuloIntInt
{ public boolean isCongruentModulo( int comparand, int modulus ); }
So there is no need to bind it to the special type »int« that
has many more features which are not needed here. The
following untested draft shows how the algorithm can be freed
from this binding.
public static boolean
( final IsCongruentModuloIntInt yearNumber )
{ final boolean isQuad = yearNumber.isCongruentModulo( 0, 4 );
final boolean isCentury = yearNumber.isCongruentModulo( 0, 100 );
final boolean isQuadcentury = yearNumber.isCongruentModulo( 0, 400 );
final boolean isCanceled = isCentury & !isQuadcentury;
final boolean isLeapYear = isQuad & !isCanceled;
result isLeapYear; }
If an algorithm needs more features of a year than just a
single feature, it can be written as follows stating exactly
which operations it needs (untested draft):
<T extends AddInt & IsPositive & DividedByInt >
public static int
difference( final T yearParameter )
{ if( yearParameter.isPositive() )yearParameter.addInt( - 1 )
quads = yearParameter.DividedByInt( 4 ); /* ... */ }
So each algorithm is generic to the maximum extend possible,
if it binds itself to the smallest interface it needs.
The interfaces »IsCongruentModuloIntInt« still is somewhat
bound to »int«, but the generalization achieved here
usually will be sufficient. A more general version could be:
public interface IsCongruentModulo<T,U,V>
{ public boolean isCongruentModulo( T comparand, U comparand1, V modulus ); }
An important part of this programming style was removed
in this posting for brevity: The JavaDoc-comments which
state what the meaning of this operation is.