// Java CODE, AP Computer Science A (11 Oct 2016)
// 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(-1);
}
// 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;
}
}
}
// NAME: ________________________________________________________
// GIVEN NMath, ZMath, and QMath (on a separate page):
// PART I - QUIZ
// 10 points each: Below each println statement, write the value the statement outputs.
class MathClass {
public static void main(String[] args) {
NMath nMath = new NMath(0);
QMath qMath = new QMath();
System.out.println(qMath.abs(qMath.NOT_NATURAL));
// ANSWER:_______________
System.out.println(nMath.tri(3));
// ANSWER:_______________
System.out.println(qMath.tri(100));
// ANSWER:_______________
System.out.println(qMath.fac(5));
// ANSWER:_______________
System.out.println(qMath.fac(-5));
// ANSWER:_______________
System.out.println(nMath.tri(-4));
// ANSWER:_______________
System.out.println(qMath.tri(-5));
// ANSWER:_______________
}
}
/* PART II – BONUS QUESTION
// 5 BONUS POINTS: Which executes faster: nMath.tri or qMath.tri?
// 5 BONUS POINTS: Why? (Use the other side of the paper if necessary.)
*/
// JavaScript CODE, AP Computer Science Principles (11 Oct 2016):
// Math with Natural Numbers
function NMath(notNatural) {
this.NOT_NATURAL = notNatural;
}
NMath.prototype = {
ZERO_FACTORIAL: 1,
// Triangle Number
tri: function(n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else if (n == 0) {
return 0;
}
else {
return n + this.tri(n - 1);
}
},
// Factorial
fac: function(n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else if (n == 0) {
return this.ZERO_FACTORIAL;
}
else {
return n * this.fac(n - 1);
}
},
}
// Math with Whole Numbers
function ZMath() {
NMath.call(this, -1);
};
ZMath.prototype = Object.create(NMath.prototype);
// Absolute Value
ZMath.prototype.abs = function(n) {
return (n < 0) ? -n : n;
}
// Math with Rational Numbers
function QMath() {
ZMath.call(this);
}
QMath.prototype = Object.create(ZMath.prototype);
// Triangle Number
QMath.prototype.tri = function(n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else {
return (n*(n+1))/2;
}
}
// NAME: ________________________________________________________
// GIVEN NMath, ZMath, and QMath (on a separate page) and the following:
var nMath = new NMath(0);
var qMath = new QMath();
// PART I - QUIZ
// 10 points each: Below each statement, write the value the statement outputs.
console.log(qMath.abs(qMath.NOT_NATURAL));
console.log(nMath.tri(3));
console.log(qMath.tri(100));
console.log(qMath.fac(5));
console.log(qMath.fac(-5));
console.log(nMath.tri(-4));
console.log(qMath.tri(-5));
/* PART II – BONUS QUESTION
// 5 BONUS POINTS: Which executes faster: nMath.tri or qMath.tri?
// 5 BONUS POINTS: Why?
*/