:(I need help fast - Design and implement a class for polynomials

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: :(I need help fast - Design and implement a class for polynomials

1. Registered User
Join Date
Mar 2006
Posts
7

## :(I need help fast - Design and implement a class for polynomials

Question: Design and implement a class for polynomials

The polynomial anxn + an-1xn-1 + …+ a1x + a0x will be implemented as a linked list. Each node will contain an int value for the power of x and an int value for the corresponding coefficient. The class operations should include addition, subtraction, multiplication, and evaluation of a polynomial. Overload the operators +, -, and * for addition, subtraction, and multiplication. Evaluation of a polynomial is implemented as a method named evaluate that has one argument of type int. The evaluate method returns the value obtained by plugging in its argument for x and performing the indicated operations. Include four constructors:

a default constructor;
a copy constructor;
a constructor with a single argument of type int that produces the polynomial that has only one constant term that is equal to the constructor argument; (In the above notation the polynomial produced by this constructor is of the simple form consisting of only a0) and
a constructor with two arguments of type int that produces the one-term polynomial whose coefficient and exponent are given by the two arguments. (In the above notation the polynomial produced by this constructor is of the slightly more complicated form amxm)
Include a method to input a polynomial. When the user inputs a polynomial, the user types in the following: anx^n + an-1x^n-1 + … + a0 . However, if a coefficient ai is zero, the user may omit the term aix^i. For example, the polynomial 3x4 + 7x2 + 5 can be input as 3x^4 + 7x^2 + 5. It could also be input as 3x^4 + 0x^3 + 7x^2 + 0x^1 + 5. If a coefficient is negative, a minus sign is used in place of a plus sign, as in the following examples: 3x^4 - 7x^3 + 2x^1 – 8, and -7x^4 + 5x^2 – 9. A minus sign at the front of the polynomial, as in the second of the above two examples, applies only to the first coefficient; it does not negate the entire polynomial. To simplify input, you can assume that polynomials are always entered one per line and that there will always be a constant term a0. If there is no constant term, the user enters zero for the constant term, as in the following: 12x^8 + 3x^2 + 0. Polynomials are output in the same format. In the case of output, the terms with zero coefficients are not output. Include a toString method and define it so that System.out.println will output polynomials in this format. Also be sure to include equals and clone methods. Write a suitable test program.

2. Senior Member
Join Date
Dec 2004
Location
San Bernardino County, California
Posts
1,468
I think you are in the wrong forum. You can't override operators in Java (yet). You looking for the C++ forum?

3. Registered User
Join Date
Mar 2006
Posts
7

## [I need help fast - Design and impement a class for polynomial

no this is not C++, it's java i'm learning java this is for an assignment

4. Registered User
Join Date
Feb 2006
Location
Cologne - Germany
Posts
271
i love this assignment, but i'm not shure about your skills and what kind of help you need. so i'll start at the most general level.

first, we need a polynomial class, that supports storing the polynomials as well as procesing them. it needs (obvioisly) the following constructors:
-public Polynomial(int [] coefficients)
to generally pass all coefficients at once to a polynom

-public Polynomial(int a0)
as in the assignment creates a polynomial with the only coefficient a0
internally this constructor should invoke the other constructor: this(new int[]{a0});
this will avoid double coding and bugs using different constructors.

-public Polynomial(Polynomial other)
as in the assignment, this will create a copy of the ther polynomial. I suggest a more proper way through a seperate method in Polynomial, which is called clone and returns a clone of this polynomial, as suggested in the java apis class Object.

-public Object clone()

for the other parts of the assignment you also will have to implement a simpe parser, which can be placed in a static method, as used in classes Integer or Double:
-public static final Polynomial parsePolynomial(String polynomialString)
as well as the method toString, to create a string representation of your polynomial:
-public String toString()

Here the simpler parts of the class:

Code:
```public class Polynomial{
// since we use a linked list from the java api, which
// only supports values as objects, we will have to
// encapsulate primitive int values to Integer objects
private List coefficients = new LinkedList();

// empty default constructor for internal purposes
protected Polynomial(){}

// in descending order
public Polynomial(int[] coefficients){
for (int i=0; i<coefficients.length; i++)
}

public Polynomial(int a0){
this(new int[]{a0});
}

public Object clone(){
Polynomial clone = new Polynomial();
// since we have the coefficients stored as objects, we will
// have to do a deep copy by reading all Integer objects and
// convert them to int and back
Integer coefX;
for (int i=0; i<coefficients.size();i++){
coefX = (Integer) coefficients.get(i);
}
return clone;
}

public String toString(){
StringBuffer sb = new StringBuffer();
Integer coefficient;
int intValue;
for (int i=0; i<coefficients.size();i++){
coefficient = (Integer) coefficients.get(i);
intValue = coefficient.intValue();
// append this coefficients term only, when it's > 0
if (intValue!=0){
// negative coefficients get their - automaticaly, but positives
// will not printed as "+xx", so we have to check for this
if (intValue>0) sb.append("+");
sb.append(coefficient+"x");
// now get the exponent. since our list contains the coefficients
// in descending order the coefficient at size-1 is 0, size-2 is 1 ...
// the coef at 0 is then size-1
int expo = (coefficients.size()-1)-i;
// check, if we have the last coefficient, which will have exponent 0
// which will not be printed
if (expo!=0){
sb.append("^");
sb.append(expo);
}
}
}

return sb.toString();
}

}```
the most dificult part on this assignment is writing the parser, for which i now have not enough time. perhaps another day.

um, i recognized some errors in toString. but i'm sure you will fix em.
Last edited by graviton; 05-05-2006 at 03:54 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise