Results 1 to 2 of 2
  1. #1
    susan is offline Member
    Join Date
    Jul 2007
    Posts
    44
    Rep Power
    0

    Default 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.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

  2. #2
    cachi is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    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.

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  2. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  3. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  4. Why does this program not end?
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:22 AM
  5. I need help with this program
    By Daniel in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 05:14 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
  •