Question Details

(solution) I wrote a program for polynomial using array in Java, but now the


I wrote a program for polynomial using array in Java, but now the professor ask us to implement arraylist instead. I don't know how to change it over, i tried get() and add() but the program doesn't work correctly no more. 

Polynomial.java is the original program, PolynomialA.java is the one that I supposed to use arraylist. I only made changes in addterm(), but it's already not working no more. 


/**

 

* A class represent a polynomial.

 

*

 

*/

 

public class Polynomial implements Quantity {

 

// the collection of terms

 

private Term terms;

 

// the max size of the collection

 

private int termSize = 100;

 

/**

 

* The constructor of this class.

 

*/

 

public Polynomial() {

 

terms = new Term[termSize];

 

}

 

/**

 

* Mathematically add the polynomial p to the current Polynomial.

 

*

 

* @param p

 

*

 

the polynomial to add

 

* @return a new Polynomial object

 

*/

 

public Polynomial add(Polynomial p) {

 

int coe;

 

int exp;

 

Polynomial newPolynomial = new Polynomial();

 

for (int i = 0; i < termSize; i++) {

 

// add terms of current polynomial to new polynomial

 

if (terms[i] != null) {

 

coe = terms[i].getCoefficient();

 

exp = terms[i].getExponent();

 

newPolynomial.addTerm(coe, exp);

 

}

 

// add terms of added polynomial to new polynomial

 

if (p.terms[i] != null) {

 

coe = p.terms[i].getCoefficient();

 

exp = p.terms[i].getExponent();

 

newPolynomial.addTerm(coe, exp);

 

}

 

}

 

return newPolynomial;

 

}

 

/**

 

* Add to the current Polynomial a term with coefficient and exponent.

 

*

 

* @param c

 

*

 

the coefficient of the term

 

* @param e

 

*

 

the exponent of the term

 

*/

 

public void addTerm(int c, int e) {

 

for (int i = 0; i < termSize; i++) {

 

// if term with exponent not exist

 

if (terms[i] == null) {

 

// no coefficients of zero are store

 

if (c == 0) {

 

return;

 

}

 

terms[i] = new Term(c, e);

 

return;

 

} // if term with exponent already exist

 

if (terms[i].getExponent() == e) {

 

int coe = terms[i].getCoefficient();

 

int newCoe = coe + c;

 

// no coefficients of zero are store

 

if (newCoe == 0) {

 

terms[i] = null;

 

return;

 

}

 

terms[i].setCoefficient(newCoe);

 

return;

 

}

 

}

 

}

 

/**

 

* Create a new Polynomial that possesses a copy of the content of the

 

* current Polynomial.

 

*

 

* @return the new Polynomial

 

*/

 

public Polynomial replicate() {

 

int coe;

 

int exp;

 

Polynomial newPolynomial = new Polynomial();

 

for (int i = 0; i < termSize; i++) {

 

if (terms[i] != null) {

 

coe = terms[i].getCoefficient();

 

exp = terms[i].getExponent();

 

newPolynomial.addTerm(coe, exp);

 

}

 

}

 

return newPolynomial;

 

}

 

/**

 

* Check if the Polynomial has the same coefficients and same exponents as

 

* current Polynomial.

 

*

 

* @param p

 

*

 

the Polynomial to compare to

 

* @return true if the Polynomial has the same coefficients and same

 

*

 

exponents as current Polynomial, otherwise return false

 

*/

 

public boolean equals(Polynomial p) {

 

// sort two Polynomial order by exponent

 

sort();

 

p.sort();

 

// compare each term in both polynomial

 

for (int i = 0; i < termSize; i++) {

 

if (terms[i] == null && p.terms[i] != null) {

 

return false;

 

}

 

if (terms[i] != null && p.terms[i] == null) {

 

return false;

 

}

 

if (terms[i] != null && p.terms[i] != null) {

 

if (terms[i].getCoefficient() != p.terms[i].getCoefficient()) {

 

return false;

 

}

 

if (terms[i].getExponent() != p.terms[i].getExponent()) {

 

return false;

 

} }

 

}

 

// all terms are equals

 

return true;

 

}

 

/**

 

* Evaluate this Polynomial on the value x.

 

*

 

* @param x

 

*

 

the value

 

* @return the evaluation

 

*/

 

public double evaluate(double x) {

 

double value = 0;

 

int coe;

 

int exp;

 

for (int i = 0; i < termSize; i++) {

 

if (terms[i] != null) {

 

coe = terms[i].getCoefficient();

 

exp = terms[i].getExponent();

 

value += coe * Math.pow(x, exp);

 

}

 

}

 

return value;

 

}

 

/**

 

* Get the coefficient associated with the term with the given exponent.

 

*

 

* @param e

 

*

 

the given exponent

 

* @return the coefficient associated with the term with the given exponent

 

*/

 

public int getCoefficient(int e) {

 

for (int i = 0; i < termSize; i++) {

 

if (terms[i] != null && terms[i].getExponent() == e) {

 

return terms[i].getCoefficient();

 

}

 

}

 

return 0;

 

}

 

/**

 

* Check if currently Polynomial has no terms.

 

*

 

* @return true if currently Polynomial has no terms, otherwise return false

 

*/

 

public boolean isEmpty() {

 

for (int i = 0; i < termSize; i++) {

 

if (terms[i] != null) {

 

return false;

 

}

 

}

 

return true;

 

}

 

/**

 

* Check if currently Polynomial has no available space for additional

 

* terms.

 

*

 

* @return true if currently Polynomial has no available space for

 

*

 

additional terms, otherwise return false

 

*/ public boolean isFull() {

 

for (int i = 0; i < termSize; i++) {

 

if (terms[i] == null) {

 

return false;

 

}

 

}

 

return true;

 

}

 

/**

 

* Get the number of terms with non-zero coefficients that are currently in

 

* the Polynomial.

 

*

 

* @return the number of terms with non-zero coefficients that are currently

 

*

 

in the Polynomial

 

*/

 

public int holding() {

 

int num = 0;

 

for (int i = 0; i < termSize; i++) {

 

if (terms[i] != null && terms[i].getCoefficient() != 0) {

 

num++;

 

}

 

}

 

return num;

 

}

 

/**

 

* Create a new Polynomial of new objects that has the same number of terms

 

* with the same exponents as does the current Polynomil, but the signs on

 

* the coefficients are switched.

 

*

 

* @return the new negate Polynomial

 

*/

 

public Polynomial negate() {

 

int coe;

 

int exp;

 

Polynomial newPolynomial = new Polynomial();

 

for (int i = 0; i < termSize; i++) {

 

if (terms[i] != null) {

 

coe = terms[i].getCoefficient();

 

exp = terms[i].getExponent();

 

newPolynomial.addTerm(-coe, exp);

 

}

 

}

 

return newPolynomial;

 

}

 

/**

 

* Multiply each term's coefficient by the value s.

 

*

 

* @param s

 

*

 

the value to multiply

 

*/

 

public void scalarMultiply(int s) {

 

int coe;

 

for (int i = 0; i < termSize; i++) {

 

if (terms[i] != null) {

 

// no coefficients of zero are store

 

if (s == 0) {

 

terms[i] = null;

 

} else {

 

coe = terms[i].getCoefficient();

 

terms[i].setCoefficient(coe * s);

 

} } } }

 

/**

 

* Create a String representation of the current Polynomial.

 

*

 

* @return the string representation of the current Polynomial

 

*/

 

public String toString() {

 

String str = "";

 

int coe;

 

int exp;

 

// sort the Polynomial order by exponent

 

sort();

 

if (isEmpty()) {

 

return "0";

 

} else {

 

// first term representation, ignore the sign of positive

 

// coefficient

 

coe = terms[0].getCoefficient();

 

exp = terms[0].getExponent();

 

if (coe < 0) {

 

str += "- (" + Math.abs(coe) + ")x^(" + exp + ")";

 

} else {

 

str += "(" + coe + ")x^(" + exp + ")";

 

}

 

// remaining terms representation

 

for (int i = 1; i < termSize; i++) {

 

if (terms[i] != null) {

 

coe = terms[i].getCoefficient();

 

exp = terms[i].getExponent();

 

if (coe < 0) {

 

str += " - (" + Math.abs(coe) + ")x^(" + exp + ")";

 

} else {

 

str += " + (" + coe + ")x^(" + exp + ")";

 

}

 

} } }

 

if (str.equals("")) {

 

return "0";

 

} else {

 

return str;

 

}

 

} } /*

 

* Sort the Polynomial order by exponent

 

*/

 

private void sort() {

 

for (int i = 0; i < termSize; i++) {

 

for (int j = 0; j < termSize; j++) {

 

if (terms[i] != null && terms[j] != null

 

&& terms[i].getExponent() > terms[j].getExponent()) {

 

Term temp = terms[i];

 

terms[i] = terms[j];

 

terms[j] = temp;

 

}

 

}

 

}

 

}

 


Solution details:

Pay using PayPal (No PayPal account Required) or your credit card . All your purchases are securely protected by .
SiteLock

About this Question

STATUS

Answered

QUALITY

Approved

DATE ANSWERED

Sep 13, 2020

EXPERT

Tutor

ANSWER RATING

GET INSTANT HELP/h4>

We have top-notch tutors who can do your essay/homework for you at a reasonable cost and then you can simply use that essay as a template to build your own arguments.

You can also use these solutions:

  • As a reference for in-depth understanding of the subject.
  • As a source of ideas / reasoning for your own research (if properly referenced)
  • For editing and paraphrasing (check your institution's definition of plagiarism and recommended paraphrase).
This we believe is a better way of understanding a problem and makes use of the efficiency of time of the student.

NEW ASSIGNMENT HELP?

Order New Solution. Quick Turnaround

Click on the button below in order to Order for a New, Original and High-Quality Essay Solutions. New orders are original solutions and precise to your writing instruction requirements. Place a New Order using the button below.

WE GUARANTEE, THAT YOUR PAPER WILL BE WRITTEN FROM SCRATCH AND WITHIN A DEADLINE.

Order Now