Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2007, 09:47 PM
Member
 
Join Date: Jul 2007
Posts: 44
susan is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:44 AM
Member
 
Join Date: Jul 2007
Posts: 40
cachi is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a program within a program gibsonrocker800 New To Java 5 05-12-2008 10:24 AM
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 04:40 PM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 11:33 PM
Why does this program not end? trill New To Java 1 08-07-2007 09:22 AM
I need help with this program Daniel Advanced Java 2 07-04-2007 07:14 AM


All times are GMT +3. The time now is 10:43 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org