# Help with polynomial program

• 07-25-2007, 08:47 PM
susan
Help with polynomial program
I can't get my main method to display the right input for this polynomial program.
Can anyone show me what's wrong, or what to do?

Code:

```import java.util.*; public class PolyTest { public static void main(String[] args) { Scanner stdin =new Scanner(System.in); Polynomial polynomial = new Polynomial(); int coefficients; //= { 2, 3, 4 }; int degree; //= { 3, 2, 1 }; System.out.println("Enter degree of polynomial"); degree = stdin.nextInt(); for(int i = degree; i >0; i--) { System.out.println("Enter coefficient of the term x ^ " + i ); coefficients = stdin.nextInt(); polynomial.addTerm(coefficients, degree); polynomial.print(); } } } class Polynomial { private LinkedList terms; /** Construct a Polynomial object. */ public Polynomial() { terms = new LinkedList(); } /** Adds a polynomial. @param p the polynomial to add @return the new polynomial after addition */ public Polynomial add(Polynomial p) { Polynomial r = new Polynomial(); ListIterator iterator = terms.listIterator(); while (iterator.hasNext()) { r.add((Term)iterator.next()); } ListIterator pIterator = p.terms.listIterator(); while (pIterator.hasNext()) { r.add((Term)pIterator.next()); } return r; } /** Adds a coefficient and degree as a new Term. @param c the coefficient @param d the degree */ public void addTerm( int c, int d) { add(new Term(c, d)); // <---ERROR HERE } /** Adds a term. @param t the new Term */ public void add(Term t) { int c = t.getCoeff(); int d = t.getDegree(); ListIterator iterator = terms.listIterator(); while (iterator.hasNext()) { Term current = (Term)iterator.next(); if (d == current.getDegree()) { if (c == -current.getCoeff()) iterator.remove(); else current.add(c); return; } else if (d < current.getDegree()) { iterator.previous(); iterator.add(t); return; } } iterator.add(t); } /** Prints the polynomial. */ public void print() { ListIterator iterator = terms.listIterator(); String expression = ""; while (iterator.hasNext()) { String next = ""; Term current = (Term)iterator.next(); if (current.getCoeff() != 0) { if (current.getCoeff() > 0 && iterator.hasNext()) { next += " + "; System.out.print(" + "); } next += current.getCoeff(); System.out.print(current.getCoeff()); if (current.getDegree() > 0) { next += " * x"; System.out.print(" * x"); if (current.getDegree() > 1) { next += "^" + current.getDegree(); System.out.print("^" + current.getDegree()); } } expression = next + expression; } } System.out.println("\n" + expression); } } class Term { public int coeff; public int degree; /** * constructor has no return in its signature */ public Term(int c, int d) // <<******* fixed ******* { coeff = c; degree = d; } /** Adds a coefficient. @param c the coefficient to add */ public void add(int c) // methods require a return type in their signature { // a return type of "void" -> nothing is returned coeff += c; // from the method. what you return from the method } // must match the return type in the signature /** Gets the coefficient. @return the coefficient */ public int getCoeff() { return coeff; } /** Gets the degree. @return the degree */ public int getDegree() { return degree; } }```
Thanks
• 08-07-2007, 05:44 AM
cachi
Code:

`add(new Term(c, d)); // <---ERROR HERE`
You can't call a method that isn't static all by itself like that. It needs an object.
Either that, or make the method static.

Greetings.