public class Poly { private String variableName; private long [] coeffs; private long denominator;// Constructorspublic Poly ( String polyVariableName, long [] polyCoeffs, long polyDenominator ) { variableName = polyVariableName; coeffs = polyCoeffs; denominator = polyDenominator; }// Debug quality printing. Takes a polynomial and displays its coefficents.public String debugPrint () { String returnValue = "("; returnValue = returnValue + coeffs [0] + "*" + variableName + "^0"; for (int i = 1; i < coeffs.length; i++) { returnValue = returnValue + " + " + coeffs [i] + "*" + variableName + "^" + i; }; returnValue = returnValue + ")/"; returnValue = returnValue + denominator; return returnValue; }// Special-case multiplication. Multiplication by a coefficient.public static Poly mulByCoeff(Poly in, long mulBy) { Poly returnValue = new Poly ( in.variableName, new long [in.coeffs.length], in.denominator ); for (int i = 0; i < in.coeffs.length; i++) { returnValue.coeffs[i] = mulBy * in.coeffs [i]; }; returnValue = reduce(returnValue); return returnValue; }// Special-case multiplication. Division by a coefficient.public static Poly divByCoeff(Poly in, long divBy) { Poly returnValue = new Poly ( in.variableName, new long [in.coeffs.length], in.denominator ); for (int i = 0; i < in.coeffs.length; i++) { returnValue.coeffs[i] = in.coeffs [i] / divBy; }; returnValue = reduce(returnValue); return returnValue; }// Special-case multiplication. Multiplication by "x", variableNamepublic static Poly mulByx(Poly in) { Poly returnValue = new Poly ( in.variableName, new long [in.coeffs.length + 1], in.denominator); returnValue.coeffs[0] = 0; for (int i = 0; i < in.coeffs.length; i++) { returnValue.coeffs[i+1] = in.coeffs[i]; }; return returnValue; }// Addition of two polynomialspublic static Poly polyAdd (Poly A, Poly B) { // Determine length of coefficient array int lengthC = 0; if ( A.coeffs.length == B.coeffs.length ) { lengthC = A.coeffs.length; } else if ( A.coeffs.length > B.coeffs.length) { lengthC = A.coeffs.length; } else { lengthC = B.coeffs.length; }; // Determine common denominator long denomC = A.denominator * B.denominator; // Create return value Poly C = new Poly ( A.variableName, new long [lengthC], denomC); for (int i = 0; i < C.coeffs.length; i++) { C.coeffs[i] = B.denominator * A.coeffs[i] + A.denominator * B.coeffs[i]; }; C = reduce(C); return C; }// Start of utility functions called by tickable functions.////////////////// Function to calculate the Highest Common Factor (Greatest Common Divisor) of 2 integers.public static long hcf(long m, long n) { if (m == 0) return n; if (n == 0) return m; return hcf(n,m%n); }// Function to reduce a polynomial to its simplest terms.public static Poly reduce(Poly in) { Poly returnValue = new Poly ( in.variableName, new long [in.coeffs.length], in.denominator ); long divisor = in.denominator; for (int i = 0; i < in.coeffs.length; i++) { divisor = hcf(in.coeffs[i],divisor); }; for (int i = 0; i < in.coeffs.length; i++) { returnValue.coeffs[i] = in.coeffs [i] / divisor; }; returnValue.denominator = in.denominator / divisor; return returnValue; }// Function to truncate 0 elements from the end of the arraypublic static Poly truncate(Poly in) { int top = 0; for ( i = 0; i < in.coeffs.length; i++ ) { if (in.coeffs[i] != 0) { top = i; } else continue; }; Poly returnValue = new Poly ( in.variableName, new long [top+1], in.denominator ); for ( int i = 0; i < top+1; i++ ) { returnValue.coeffs[i] = in.coeffs[i]; }; return returnValue; }// Start of Main methodpublic static void main(String[] args) { String a = "x"; long[] b = {2,4,8}; long c = 2; Poly test1 = new Poly(a,b,c); String d = "x"; long[] e = {16,32,64}; long f = 4; Poly test2 = new Poly(d,e,f); System.out.printf("Polynomial 1 is " + test1.debugPrint() + "%n"); System.out.printf("Polynomial 2 is " + test2.debugPrint() + "%n"); System.out.printf("HCF of 12 and 16 is " + hcf(12,16) + "%n"); System.out.printf("Simplified polynomial 1 is " + reduce(test1).debugPrint() + "%n"); System.out.printf("Polynomial 1 multiplied by 4 is " + mulByCoeff(test1,4).debugPrint() + "%n"); System.out.printf("Polynomial 1 divided by 4 is " + divByCoeff(test1,4).debugPrint() + "%n"); System.out.printf("Polynomial 1 multiplied by x is " + mulByx(test1).debugPrint() + "%n"); System.out.printf("Polynomial 1 added to Polynomial 2 is " + polyAdd(test1,test2).debugPrint() + "%n"); } }