// Math with Natural Numbers
class NMath {
final long NOT_NATURAL;
static final long ZERO_FACTORIAL = 1;
NMath(long notNatural) {
this.NOT_NATURAL = notNatural;
}
// Triangle Number
long tri(long n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else if (n == 0) {
return 0;
}
else {
return n + this.tri(n - 1);
}
}
// Factorial
long fac(long n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else if (n == 0) {
return NMath.ZERO_FACTORIAL;
}
else {
return n * this.fac(n - 1);
}
}
}
// Math with Whole Numbers
class ZMath extends NMath {
ZMath() {
super(-666);
}
// Absolute Value
long abs(long n) {
return (n < 0) ? -n : n;
}
}
// Math with Rational Numbers
class QMath extends ZMath {
QMath() {
super();
}
// Triangle Number
long tri(long n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else {
return (n*(n+1))/2;
}
}
}
// Quiz: What output does the following code produce?
class MathClass {
public static void main(String[] args) {
NMath nMath = new NMath(-1);
QMath qMath = new QMath();
System.out.println(qMath.abs(qMath.NOT_NATURAL));
System.out.println(nMath.tri(5));
System.out.println(qMath.tri(10));
System.out.println(qMath.fac(3));
System.out.println(qMath.fac(-3));
System.out.println(nMath.tri(-1));
System.out.println(qMath.tri(-1));
}
}