// Ryan Kightlinger ========== Part A ========== degree() // returns the degree of a polynomial. getCoefficient(power) // returns the coefficient of the xpower term. changeCoefficient(newCoefficient, power) // Replaces the coefficient of the xpower term with newCoefficient addPolynomial(polynomial) // Adds the two polynomials together subractPolynomial(polynomial) // Subtracts the two polynomials ========== PART B ========== // ******************************************************** // Interface polynomialInterface for the ADT list. // ********************************************************* public interface PolynomialInterface { public int degree(); // Gets the degree value of the polynomial // Pre-Condition: the degree of the polynomial must be a real number // Post-Condition: returns the degree of specific polynomial public int getCoefficient(int power); // Gets the coefficient value of the polynomial // Pre-Condition: the coefficient of the polynomial must be a real number // Post-Condition: returns the coefficient value of the polynomial public int changeCoefficient(int newCoefficient,int power); // Changes the coefficient value // Pre-Condition: none // Post-Condition: returns the new value for the coefficient public int addPolynomial(Polynomial x); // calls the add polynomial method // Pre-Condition: none // Post-Condtion: adds the two polynomials together public subtractPolynomial(Polynomial x); // calls the subtract polynomial method // Pre-Condition: none // Post-Condition: subtracts the two polynomials from each other } ======== Part C ======== // Ryan Kightlinger // October 10, 2002 // This program will add and subtract two different polynomials // Get the necessary Java library methods import java.awt.*; import java.applet.*; import java.util.*; public class Poly extends Applet { // defining the two polynomial arrays int[] adata = {9,0,-1,7,0,4}; // 4x^5 + 0x^4 + 7x^3 - x^2 + 0x + 9 int[] bdata = {2,-3,4,-2,6,0,-3}; // -3x^7 + 0x^6 + 6x^5 -2x^4 + 0x^3 + 4x^2 -3x + 2 int[] adddata = new int[100]; // holds the data for adding the arrays int[] subtractdata = new int[100] // holds the data for subtracting the arrays // Defines 2 new polynomials Polynomial a = new Polynomial(); Polynomial b = new Polynomial(); // calls changeCoefficient method for (int i=0; i < 5; i++){ a.changeCoefficient(adata[i],i); b.changeCoefficient(bdata[i],i); } // calls addPolynomial method for (int i=0; i < 100; i++){ adddata[i] = adata[i].addPolynomial(bdata[i];) } // calls subtractPolynomial method for (int i=0; i < 100; i++){ subtractdata[i] = adata[1].subtractPolynomial(bdata[i]); } // ******************************************************** // Interface polynomialInterface for the ADT list. // ********************************************************* public interface PolynomialInterface { public int degree(); // Gets the degree value of the polynomial // Pre-Condition: the degree of the polynomial must be a real number // Post-Condition: returns the degree of specific polynomial public int getCoefficient(int power); // Gets the coefficient value of the polynomial // Pre-Condition: the coefficient of the polynomial must be a real number // Post-Condition: returns the coefficient value of the polynomial public int changeCoefficient(int newCoefficient,int power); // Changes the coefficient value // Pre-Condition: none // Post-Condition: returns the new value for the coefficient public int addPolynomial(Polynomial x); // calls the add polynomial method // Pre-Condition: none // Post-Condtion: adds the two polynomials together public subtractPolynomial(Polynomial x); // calls the subtract polynomial method // Pre-Condition: none // Post-Condition: subtracts the two polynomials from each other } } // ------------------------------------- // The Polynomial CLASS // ------------------------------------- class Polynomial implements PolynomialInterface { private int[] data = new int[100]; { // initializes the arrays to 0 for (int i=0; i < 100; i++){ int[i] = 0; } } public int getCoefficient(int power) { return data[power]; } public int changeCoefficient(int newCoefficient,int power) { int temp = newCoefficient; int temp2 = power; newCoefficient = temp2; power = temp; return newCoefficient,power ; } public Polynomial addPolynomial(Polynomial x) { Polynomial z; for (int i=0; i < 100; i++){ z.data[i] = data[i] + x.data[i]; return z; } } public Polynomial subtractPolynomial(Polynomial x) { Polynomial y; for (int i=0; i < 100; i++){ y.data[i] = data[i] - x.data[i]; return y; } } }