# Thread: Help with polynomial program

1. Member
Join Date
Jul 2007
Posts
44
Rep Power
0

## 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?

Java 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.print();
}
}
}

class Polynomial
{

/**
Construct a Polynomial object.
*/
public Polynomial()
{
}

/**
@param p the polynomial to add
@return the new polynomial after addition
*/
{
Polynomial r = new Polynomial();
ListIterator iterator = terms.listIterator();
while (iterator.hasNext())
{
}
ListIterator pIterator = p.terms.listIterator();
while (pIterator.hasNext())
{
}
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
}

/**
@param t the new Term
*/
{
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
return;
}
else if (d < current.getDegree())
{
iterator.previous();
return;
}
}
}

/**
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;
}

/**
@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

2. Member
Join Date
Jul 2007
Posts
40
Rep Power
0
Java 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.

#### Posting Permissions

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