Search code examples
javalistpolynomials

Addition of two Polynomials using LinkedList in Java


I was tasked to make a program that adds two Polynomial using the LinkedList data structure, so far I've written the codes for adding and accepting two Polynomials. My problem is that after the user enters their Polynomial the addition should happen, however the value of the returned Polynomial is always 0x^0.

Here's the main code of the program.(Note: this is in a switch case so I didn't put the whole method).

System.out.println("Literal Coefficient of the two Polynomials: \'x\'");
System.out.println("Degree of the first Polynomial?");
System.out.print("Input: ");
    eV = readInteger();
    nC = new int[eV];
        for(int x=0; x<=nC.length-1; x++) {
            System.out.println("Numerical Coefficient of the term with Degree "+eV+"?");
            System.out.print("Input: ");
            nC[x] = readInteger();

            firstPoly.add(new Term(nC[x], eV, 'x'));

            eV--;
        }
resultPolyOne.setTerms(firstPoly);
System.out.println();   
System.out.println("The first Polynomial entered: ");
stringRepresentation(firstPoly);
System.out.println();
System.out.println();
System.out.println("Degree of the second Polynomial?");
System.out.print("Input: ");
    eV = readInteger();
    nC = new int[eV];
        for(int x=0; x<=nC.length-1; x++) {
            System.out.println("Numerical Coefficient of the term with Degree "+eV+"?");
            System.out.print("Input: ");
            nC[x] = readInteger();

            secondPoly.add(new Term(nC[x], eV, 'x'));

            eV--;
        }
resultPolyTwo.setTerms(firstPoly);    
System.out.println();   
System.out.println("The second Polynomial entered: ");
stringRepresentation(secondPoly);
System.out.println();
System.out.println();  
System.out.println("Result of the Addition: ");
System.out.println((resultPolyOne.addPolys(resultPolyTwo)).toString());

firstPoly.clear();
secondPoly.clear();

Method for adding Polynomials

public Polynomial addPolys(Polynomial otherPoly) throws Exception {
    LinkedList<Term> resultTerms = new LinkedList<Term>();
    Polynomial resultPoly = new Polynomial();
        for(int x=0; x<this.getTerms().size(); x++) {
            Term currentTerm = this.getTerms().get(x);
            resultTerms.add(new Term(currentTerm.getNumC(), currentTerm.getExpC(), currentTerm.getLitC()));
        }
        resultPoly.setTerms(resultTerms);
        for(int y=0; y<otherPoly.getTerms().size(); y++) {
            resultPoly.addTerm(otherPoly.getTerms().get(y));
        } 
        if(resultPoly.getTerms().size()==0) {
            resultPoly.addTerm(new Term(0,0,'x'));
        }
return resultPoly;
}//addPolys

Method for adding a term to a Polynomial

public void addTerm(Term newTerm) throws Exception {
    int listIndex = 00;
    boolean foundFlag = false;
    Term currentTerm = null;

    for(listIndex=0; listIndex<polyTerm.size(); listIndex++) {
        currentTerm = polyTerm.get(listIndex);
            if(currentTerm.getExpC()<=newTerm.getExpC()) {
                foundFlag = true;
                break;
            }
    }
    if(!foundFlag) {
        polyTerm.add(newTerm);
    } else {
        if(currentTerm.getExpC()<newTerm.getExpC()) {
            polyTerm.add(listIndex, newTerm);
        } else {
            currentTerm.setNumC(currentTerm.getNumC()+newTerm.getNumC());
            if(currentTerm.getNumC()==0) {
                polyTerm.remove(listIndex);
            }
        }
    }
}//addTerm

Just to add, the constructor for my Term is like this.

Term(int numberCoefficent, char literalCoefficent, int exponentialCoefficient) {
    //Codes here
}

Solution

  • Thanks to @GhostCat, I ended up reviewing my question and tried to improve it as a result I found an answer to my question.

    @MordechayS the problem was within the line where I display the Polynomial. @matt I'll be posting my answer so I guess help's not needed anymore.

    The problem was this:

    System.out.println((resultPolyOne.addPolys(resultPolyTwo)).toString());
    

    Which I replaced with:

    resultPolyThree.toStringRep();
    

    which is a method I made in my Polynomial Class, I didn't override the toString method of Object since I want to polish up on this.