Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[JAVA] Your assignment is to write a class called Polynomial. A polynomial is a

ID: 3849334 • Letter: #

Question

[JAVA] Your assignment is to write a class called Polynomial. A polynomial is a function of the following form:

f(n) = ck nk + ck-1 nk-1 + … + c1 n + c0

c0, c1, c2, …, ck are called coefficients. We call k the degree of the polynomial. For our purposes, we will assume that the coefficients are all integers (positive, negative, or 0). The coefficient ck should be nonzero.

I have placed a file named hw1.jar in the D2L Homework assignment 1 dropbox folder. It contains 2 source files called Polynomial.java and TestHW1.java. You may use the code in Polynomial.java as a starting point for your program, or if you prefer you can write your own code from scratch. The TestHW1 class tests the methods of the Polynomial class on a few cases. Please upload the Polynomial.java file once you have completed it.

I recommend that you store the coefficients of a Polynomial object in an instance variable which is an array of integers.

You should write the following methods for the Polynomial class:

a. (1/2 point) A constructor. It is passed 1 parameter: an array of integers which represents the coefficients of the polynomial.

b. (1 point) A method called simplify. It is a void method which is passed 0 parameters. It ensures that the polynomial’s kth coefficient is not 0. If a polynomial is created which has no non-zero coefficients, then it should be represented as an array of length 0. The simplify method should be called within the constructor.

c. (2 points) A toString method. As is usually the case in Java, toString is passed 0 parameters and returns a String representation of an object. Examples of the kind of string that toString should return for polynomials can be found below.

d. (1/2 point) A degree method. It returns an integer, which is the degree of the polynomial.

e. (1 point) An evaluate method. It is passed 1 parameter x, which is an integer. It should return an integer, which represents f(x), the value of the polynomial f(n) when n is equal to x.

As I said, I have written some test code in the file TestHW1.java. You may use this to your own code; however, you may wish to write your own main method which tests your Polynomial class more thoroughly. I may use different examples than the ones in TestHW1 when I grade your assignment. Here are the contents of TestHW1.java:

package hw1;

public class TestHW1 { public static void main(String[] args) {   int coeffs1[] = {1, -1, 2};   // f(n) = n^2 - n + 2   int coeffs2[] = {2, 0, 1, 0}; // f(n) = 2n^3 + n   int coeffs3[] = {0, 0, 1, 0}; // f(n) = n (after simpliciation)   int coeffs4[] = { };          // f(n) = 0   Polynomial p1 = new Polynomial(coeffs1);   Polynomial p2 = new Polynomial(coeffs2);   Polynomial p3 = new Polynomial(coeffs3);   Polynomial p4 = new Polynomial(coeffs4);     System.out.println("Polynomial 1: " + p1);   System.out.println("Polynomial 2: " + p2);   System.out.println("Polynomial 3: " + p3);   System.out.println("Polynomial 4: " + p4);

System.out.println(" Values for f(n) = " + p1);   for (int i=-6; i<=6; i+=3)    System.out.println("f(" + i + ") = " + p1.evaluate(i));   System.out.println(" Values for f(n) = " + p2);   for (int i=-6; i<=6; i+=3)     System.out.println("f(" + i + ") = " + p2.evaluate(i));   System.out.println(" Values for f(n) = " + p3);   for (int i=-6; i<=6; i+=3)    System.out.println("f(" + i + ") = " + p3.evaluate(i));   System.out.println(" Values for f(n) = " + p4);   for (int i=1; i<100; i+=50)    System.out.println("f(" + i + ") = " + p4.evaluate(i)); } }


Note that in Java, an array can be declared and initialized in one statement, by placing { } around the array’s initial contents. For example::
int x[] = {1, 2, 3, 4};
is the same as:
int x[] = new int[4]; for (int i=0; i<4; i++)    x[i] = i+1;

Also note the effects of the simplify method: the coefficients {0, 0, 1, 0} should be changed by the simplify method to {1, 0}.

Here is sample output which my own solution produces when I run the above main method.

Polynomial 1: n^2 - n + 2 Polynomial 2: 2n^3 + n Polynomial 3: n Polynomial 4: 0

Values for f(n) = n^2 - n + 2 f(-6) = 44 f(-3) = 14 f(0) = 2 f(3) = 8 f(6) = 32

Values for f(n) = 2n^3 + n f(-6) = -438 f(-3) = -57 f(0) = 0 f(3) = 57 f(6) = 438

Values for f(n) = n f(-6) = -6 f(-3) = -3 f(0) = 0 f(3) = 3 f(6) = 6

Values for f(n) = 0 f(1) = 0 f(51) = 0

Explanation / Answer

public class SpPolynomial {

private ArrayList<Polynomial> polynomialarraylist = new ArrayList<Polynomial>();

/**
* Constructor to get values of an arraylist of integers
* @param arraylist that contains the integer values used for the polynomials
*/
public SparsePolynomial(ArrayList<Integer> arrayList)
{
//MODIFIDED: polynomialarraylist
//EFFECT: constructs the arraylist of polynomials based off the arraylist of integers
insertIntoPolynomialArray(arrayList);
}

/**
* Converts the elements of the integer array into polynomials
* @param arrayList that contains the polynomials contents
*/
private void insertIntoPolynomialArray(ArrayList<Integer> arrayList)
{
//MODIFIED: polynomialarray
//EFFECT: inputs the values of the arrayList into the polynomial array based on the position of the digits
for(int i = 0; i < arrayList.size(); i++)
{
Polynomial polynomial = new Polynomial(arrayList.get(i), arrayList.get(i+1));
polynomialarraylist.add(polynomial);
System.out.println("coef" + arrayList.get(i));
System.out.println("degree" + arrayList.get(i+1));
i++;
}
}


/**
*
*/
@Override
public String toString()
{
String result = "";
sort();
if (getDegree(0) == 0)
return "" + getCoefficient(0);
if (getDegree(0) == 1)
return getCoefficient(0) + "x + " + getCoefficient(0);
result = getCoefficient(0) + "x^" + getDegree(0);

for (int j = 1; j < polynomialarraylist.size(); j++)
{
if(j > polynomialarraylist.size())
{
break;
}
if
(getCoefficient(j) == 0) continue;
else if
(getCoefficient(j) > 0) result = result+ " + " + ( getCoefficient(j));
else if
(getCoefficient(j) < 0) result = result+ " - " + (-getCoefficient(j));
if(getDegree(j) == 1) result = result + "x";
else if (getDegree(j) > 1) result = result + "x^" + getDegree(j);
}
return result;


}

/**
* Sorts array
* @param array to sort
*/
private void sort()
{
ArrayList<Polynomial> temp = polynomialarraylist;
ArrayList<Polynomial> temp2 = new ArrayList<Polynomial>();
int polydegreemain = polynomialarraylist.get(0).degree();
temp2.add(polynomialarraylist.get(0));
for(int i = 1; i < polynomialarraylist.size(); i++)
{
if(i > polynomialarraylist.size())
{
break;
}
int polydegreesecondary = polynomialarraylist.get(i).degree();

if(polydegreemain < polydegreesecondary)
{
temp.set(i-1, polynomialarraylist.get(i));
temp.set(i, temp2.get(0));
}

}

polynomialarraylist = temp;
}
/**
* Makes object hashable
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime
* result
+ ((polynomialarraylist == null) ? 0 : polynomialarraylist
.hashCode());
return result;
}

/**
* Checks for equality of two objects
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SparsePolynomial other = (SparsePolynomial) obj;
if (polynomialarraylist == null) {
if (other.polynomialarraylist != null)
return false;
} else if (!polynomialarraylist.equals(other.polynomialarraylist))
return false;
return true;
}

public boolean equals(SparsePolynomial Sparse)
{
if(this == Sparse)
{
return true;
}
else
{
return false;
}
}

public SparsePolynomial add(SparsePolynomial other)
{

ArrayList<Polynomial> thisPolynomial = createPolynomial();
SparsePolynomial newSparsePolynomial;
ArrayList<Polynomial> otherPolynomial = other.createPolynomial();
Polynomial oldsum = new Polynomial();
Polynomial newsum = new Polynomial();
for(int i = 0; i < thisPolynomial.size();i++)
{
if(thisPolynomial.size() == 1)
{
newsum = thisPolynomial.get(i);
oldsum = newsum;
break;
}
if(i == 0)
{
newsum = thisPolynomial.get(i).add(thisPolynomial.get(i+1));
oldsum = newsum;
i++;
}
else
{
newsum = oldsum.add(thisPolynomial.get(i));
oldsum = newsum;
}
}
for(int i = 0; i < otherPolynomial.size(); i++)
{   
newsum = oldsum.add(otherPolynomial.get(i));
oldsum = newsum;
}

ArrayList<Integer> ints = new ArrayList<Integer>();

for(int i = 0; i < oldsum.degree()+1; i++)
{
ints.add(oldsum.coefficient(i));
ints.add(i);
}
newSparsePolynomial = new SparsePolynomial(ints);

return newSparsePolynomial;
}

public SparsePolynomial subtract(SparsePolynomial other)
{
ArrayList<Polynomial> thisPolynomial = createPolynomial();

ArrayList<Polynomial> otherPolynomial = other.createPolynomial();

Polynomial olddifference = new Polynomial();
Polynomial newdifference = new Polynomial();
for(int i = 0; i < thisPolynomial.size()+1;i++)
{
if(i == 0)
{
newdifference = thisPolynomial.get(i).subtract(thisPolynomial.get(i+1));
olddifference = newdifference;
i++;
}
else
{
newdifference = olddifference.subtract(thisPolynomial.get(i));
olddifference = newdifference;
}
}
for(int i = 0; i < otherPolynomial.size(); i++)
{

newdifference = olddifference.add(otherPolynomial.get(i));
olddifference = newdifference;

}

ArrayList<Polynomial> polyarray = createArrayListOfPolynomialsFromPolynomials(olddifference);

ArrayList<Integer> ints = new ArrayList<Integer>();

for(int i = 0; i < polyarray.size(); i++)
{
ints.add(polyarray.get(i).coefficient(polyarray.get(i).degree()));
ints.add(polyarray.get(i).degree());
}

SparsePolynomial newSparsePolynomial = new SparsePolynomial(ints);

return newSparsePolynomial;
}


private int getDegree(int index)
{
int degree;

degree = polynomialarraylist.get(index).degree();

return degree;
}

private int getCoefficient(int index)
{
int coefficient;

coefficient = polynomialarraylist.get(index).coefficient(polynomialarraylist.get(index).degree());

return coefficient;
}

private ArrayList<Polynomial> createPolynomial()
{
Polynomial polynomial = null;
ArrayList<Polynomial> polynomialArray = new ArrayList<Polynomial>();
for(int i = 0; i < polynomialarraylist.size(); i++)
{
polynomial = new Polynomial(getCoefficient(i), getDegree(i));
polynomialArray.add(polynomial);
}
return polynomialArray;
}
Polynomial class

public class Polynomial {
// Overview: ...
private int[] terms;
private int degree;

// Constructors
public Polynomial() {
// Effects: Initializes this to be the zero polynomial
terms = new int[1];
degree = 0;
}

public Polynomial(int constant, int power) {
// Effects: if n < 0 throws IllegalArgumentException else
// initializes this to be the polynomial c*x^n
if(power < 0){
throw new IllegalArgumentException("Polynomial(int, int) constructor");
}

if(constant == 0) {
terms = new int[1];
degree = 0;
return;
}

terms = new int[power+1];

for(int i=0; i<power; i++) {
terms[i] = 0;
}

terms[power] = constant;
degree = power;
}

private Polynomial(int power) {
terms = new int[power+1];
degree = power;
}

// Methods
public int degree() {
// Effects: Returns the degree of this, i.e., the largest exponent
// with a non-zero coefficient. Returns 0 is this is the zero polynomial
return degree;
}

public int coefficient(int degree) {
// Effects: Returns the coefficient of the term of this whose exponent is degree
if(degree < 0 || degree > this.degree) {
return 0;
}
else {
return terms[degree];
}
}

public Polynomial subtract(Polynomial other) throws NullPointerException {
// Effects: if other is null throws a NullPointerException else
// returns the Polynomial this - other
return add(other.minus());
}

public Polynomial minus() {
// Effects: Returns the polynomial - this
Polynomial result = new Polynomial(degree);
for(int i=0; i<=degree; i++) {
result.terms[i] = -this.terms[i];
}
return result;
}

public Polynomial add(Polynomial other) throws NullPointerException {
// Effects: If other is null throws NullPointerException else
// returns the Polynomial this + other
Polynomial larger, smaller;
if (degree > other.degree){
larger = this;
smaller = other;
}
else {
larger = other;
smaller = this;
}

int newDegree = larger.degree;

if (degree == other.degree) {
for(int k = degree; k > 0 ; k--) {
if (this.terms[k] + other.terms[k] != 0) {
break;
}
else {
newDegree --;
}
}
}


Polynomial newPoly = new Polynomial(newDegree);
int i;
for (i=0; i <= smaller.degree && i <= newDegree; i++){
newPoly.terms[i] = smaller.terms[i] + larger.terms[i];
}

for(int j=i; j <= newDegree; j++) {
newPoly.terms[j] = larger.terms[j];
}

return newPoly;
}

public Polynomial multiply(Polynomial other) throws NullPointerException {
// Effects: If other is null throws NullPointerException else
// returns the Polynomial this * other
if ((other.degree == 0 && other.terms[0] == 0) ||
(this.degree==0 && this.terms[0] == 0)) {
return new Polynomial();
}

Polynomial newPoly = new Polynomial(degree + other.degree);

newPoly.terms[degree + other.degree] = 0;

for(int i=0; i<=degree; i++) {
for (int j=0; j<= other.degree; j++) {
newPoly.terms[i+j] = newPoly.terms[i+j] + this.terms[i] * other.terms[j];
}
}
return newPoly;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote