Hey all!
I am having trouble debugging my program, and after exhausting all of my options i decided to come to you guys.
The assignment requires us to create our own custom linkedList class representing a polynomial, where each term is a link.
I have most of it working, but have a few problems, mainly with my toString() method.
For example when constructing a polynomial like this 4x^5, 2x^7, 1x^9, 0x^3, -1x^8
the toString() is showing me this: "+x^9 -x^8 " so it is ignoring my first 2 terms for some reason, even though i know they were constructed (polynomial.terms() is showing there are 4 terms- as there should be since it needs to eliminate terms with zero coefficients.)
I could really use some help please, any insight or clue as to why this is not working will be greatly appreciated.
Thanks in advance! :)
Here is my code:

``````import java.util.*;
/**
* A class modeling a Polynomial expression using a linked list.
* @author Efrat Eshet
* @version 2/15/2011
*/
public class Polynomial {
public Term first;
public Term last;
public int numTerms;

// Constructor creating a Polynomial from a String.
// every term is represented by two numbers in the string. The first number
// is the coefficient and secong is the exponent.
/**
* Constructor for Polynomial from a String.
* @param s The String to construct the Polynomial from.
* @throws IllegalArgumentException if string s is null or if arguments are not doubles or ints.
*/
public Polynomial (String s){
if (s==null){
throw new IllegalArgumentException();
}
else{
first= null;
last = null;
numTerms= 0;
double coef;
int expo;
Term p;
Scanner sc = new Scanner(s);
if (sc.hasNextDouble()){
coef= sc.nextDouble();
if (sc.hasNextInt())
expo= sc.nextInt();
else {
throw new IllegalArgumentException();
}
}
else {
throw new IllegalArgumentException();
}
if (expo<0){
throw new IllegalArgumentException();
}
else{
first= new Term (coef, expo, null);
last=first;
numTerms++;
p=first;
System.out.println("first term is "+first.coef+" "+first.expo);
System.out.println("number of terms is "+numTerms);

while (sc.hasNext()){
if (sc.hasNextDouble()){
coef= sc.nextDouble();
if (sc.hasNextInt())
expo= sc.nextInt();
else
throw new IllegalArgumentException();
}
else
throw new IllegalArgumentException();
this.addTerm(coef,expo);
System.out.println("p term is "+p.coef+" "+p.expo);
System.out.println("number of terms is "+numTerms);
System.out.println(" ");
//     p.next= new Term (coef, expo, null);
//    numTerms++;
//   p=p.next;
last= p;
}
}
}
}

/**
* Copy Constructor for Polynomial
* @param poly the polynomial to copy.
* @throws IllegalArgumentException if poly s is null.
*/
public Polynomial (Polynomial poly){
first= null;
last = null;
numTerms= 0;
if (poly==null){
throw new IllegalArgumentException();
}
else{
Term p=poly.first;
Term pCopy;
first= pCopy= new Term (poly.first.coef,poly.first.expo,null);
while (p.next!=null){
this.addTerm(p.next.coef,p.next.expo);
//pCopy.next=new Term (p.next.coef,p.next.expo,null);
pCopy=pCopy.next;
p=p.next;
}
last=pCopy;
numTerms=poly.numTerms;

}

}

/**
* return the numberof terms in this polynomial.
* @return the number of terms in the polynomial.
*/
public int terms(){
return numTerms;
}

/**
* add a term to the polynomial.
* @param coef the coefficient of the term.
* @param exp the exponent of the term.
*/
public void addTerm (double coef, int exp){
boolean bool= false;
Term p= first;
if (numTerms== 0){
first= new Term(coef,exp,null);
numTerms++;
}
else if (coef == 0){

}
else if (p.expo< exp){
first= new  Term(coef, exp, first);
numTerms++;
}
else {
while (bool!= true){
if (p.expo== exp){
// System.out.println("hello!!");
p.coef= p.coef + coef;
bool= true;
}
else if (p.next != null && p.next.expo > exp){
p= p.next;
}
else {
p.next= new Term(coef,exp,null);
bool= true;
numTerms++;
}
}
p= p.next;
}
}

/**
* delete the term with the exponent specified and return the coefficient (0.0 if non deleted).
* @param exp the exponent of the term to delete.
* @return the coefficient of the term that was deleted.
*/
public double deleteTerm(int exp){
Term p= first;
double result;
if (first==null)
return 0.0;
else{
if (p.expo==exp){
result= first.coef;
first= first.next;
numTerms--;
return result;
}
else {
while (p.next!=null){
if (p.next.expo==exp){
result = p.next.coef;
p.next= p.next.next;
numTerms--;
return result;
}
else
p=p.next;
}
return 0.0;
}
}
}

/**
* return a string representation of the polynomial.
* @return a string of the polynomial.
*/
public String toString(){
String result ="";
Term p= first;
if (first==null){   // if item is null
return "0.0";
}
else if (p.next==null){  // if poly only has 1 term
if (stringExpo(p)=="0"){  // if expo is 0 we ignore the whole term.
}
else{
result= result+stringCoef(p)+stringExpo(p);  //
}
}
else{
while(p!=null){
if (stringExpo(p)==""){
result= result+stringCoef(p);
}
else {
result= result+stringCoef(p)+stringExpo(p);
if (stringExpo(p)==""){
result= result+ stringCoef(p).substring(0,stringCoef(p).length()-2);
}
}
p=p.next;
result=result+" ";
}
}
return result;
}

/**
* return the string representation of the coefficient of the term specified.
* @param p the term specified.
* @return a string representation of the coefficient.
*/
public String stringCoef(Term p){
String ansCoef= "";
if (p.coef<0){
if (p.coef==-1){
ansCoef="-";
}
else{
ansCoef=p.coef+"";
}
}
else if (p.coef ==0){
ansCoef="";
}
else {
if (p.coef==1){
ansCoef= "+";
}
else{
if (p==first){
ansCoef= p.coef+"";
}
else{
ansCoef="+"+p.coef+"";
}
}
}
return ansCoef;
}

/**
* return the string representation of the exponent of the term specified.
* @param p the term specified.
* @return a string representation of the exponent.
*/
public String stringExpo(Term p){
String ansExpo= "";
if (p.expo==1){
ansExpo= "x";
}
else if (p.expo==0){
ansExpo="";
}
else{
if (p.coef!=0){
ansExpo="x^"+p.expo;
}
else{
ansExpo="";
}
}

return ansExpo;
}

}

/**
* Private inner class Term
*/
private class Term{
double coef;
int expo;
Term next;

// Constructor for a new Term with the given data and the next Term.
/**
* constructor for inner class Term.
* @param coef the coefficient of the term.
* @param expo the exponent of the term.
* @param n the link to the next term.
*/
Term(double coef, int expo, Term n){
this.coef= coef;
this.expo= expo;
this.next=n;
}
}
}``````

## Recommended Answers

``````if (stringExpo(p)==""){
...
else {
result= result+stringCoef(p)+stringExpo(p);
if (stringExpo(p)==""){``````

Looks like the second if will always be false, since it's in an else clause following an if with the same test?

Plus, the old old chestnut, you can't compare Strings with == unless you actually mean …

## All 2 Replies

``````if (stringExpo(p)==""){
...
else {
result= result+stringCoef(p)+stringExpo(p);
if (stringExpo(p)==""){``````

Looks like the second if will always be false, since it's in an else clause following an if with the same test?

Plus, the old old chestnut, you can't compare Strings with == unless you actually mean "are exactly the same Object". Use string1.equals(string2) to test of they contain the same sequence of characters.

Thank you so much for taking the time to look at my thread. i really do appreciate it, i know the code is quite long.
As for what you said, you were quite right. I changed the things you said, and tweaked a few other things and got it to work just fine.
So thanks again! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.