-2

I am working on this simple program that adds two polynomials. However, I am getting wrong results and could not spot the mistake.

import java.util.LinkedList;

public class Polynomial {

    private LinkedList<Term> terms = new LinkedList<Term>();


    private class Term {

        private int coef;
        private int exp;

        public Term(int coef, int exp) {
            this.coef = coef;
            this.exp = exp;
        }

        public int getCoef() {
            return coef;
        }

        public int getExp() {
            return exp;
        }

        public String toString() {
            return (this.coef + "x^" + this.exp);
        }
    }

    public String addPoly(String first, String second) {

        LinkedList<Term> otherTerms = new LinkedList<Term>();

        String result = "";

        String [] termsArray1 = first.split(";");
        String [] termsArray2 = second.split(";");

        for (int i = 0; i < termsArray1.length; i++) {
            String [] temp = termsArray1[i].split("x\\^");

            int currentCoef = Integer.parseInt(temp[0]);
            int currentExp = Integer.parseInt(temp[1]);

            Term currentTerm = new Term(currentCoef, currentExp);
            terms.add(currentTerm);
        }

        for (int i = 0; i < termsArray2.length; i++) {
            String [] temp = termsArray2[i].split("x\\^");

            int currentCoef = Integer.parseInt(temp[0]);
            int currentExp = Integer.parseInt(temp[1]);

            Term currentTerm = new Term(currentCoef, currentExp);
            otherTerms.add(currentTerm);
        }

        int i = 0;
        int j = 0;

       while (true){

           if(i == terms.size() || j == otherTerms.size()) {
               break;
           }

           if(terms.get(i).getExp() < otherTerms.get(j).getExp()) {
              result += (otherTerms.get(j).toString() + ";");

               j++;
           }

           if(terms.get(i).getExp() > otherTerms.get(j).getExp()) {
               result += (terms.get(i).toString() + ";");

               i++;
           }

           if(terms.get(i).getExp() == otherTerms.get(j).getExp()) {
               Term temp = new Term((terms.get(i).getCoef() + otherTerms.get(j).getCoef()), terms.get(i).getExp());
               result += (temp.toString() + ";");
               i++;
               j++;
           }
       }

       result = result.substring(0, result.length()-1);
       return result;
    }
}

::Test::

String s3 = "5x^2;-4x^1;3x^0";

String s4 = "6x^4;-1x^3;3x^2";

Polynomial p = new Polynomial();

System.out.println(p.addPoly(s4, s3));

Expected result: 6x^4;-1x^3;7x^2;-4x^1;3x^0

Actual result: 3x^4;7x^2;-1x^1;10x^0

metsys
  • 3
  • 4

1 Answers1

0

The problem is that when your loop exits, one of the following can still be true:

  • i < terms.size()
  • j < j == otherTerms.size()

And this is the case with your example input. This means that part of one of the terms has not been processed and integrated into the output.

A second problem is that your multiple if statements are not exclusive; after the first if block is executed and j++ has executed, it might well be that j is an invalid index in otherTerms when the second if is evaluated. This should be avoided by turning the second and third if into else if.

Here is a fix for that loop:

    while (i < terms.size() || j < otherTerms.size()) {
        if(i == terms.size() || j < otherTerms.size() && terms.get(i).getExp() < otherTerms.get(j).getExp()) {
            result += (otherTerms.get(j).toString() + ";");
            j++;
        }
        else if(j == otherTerms.size() || i < terms.size() && terms.get(i).getExp() > otherTerms.get(j).getExp()) {
            result += (terms.get(i).toString() + ";");
            i++;
        } 
        else if(terms.get(i).getExp() == otherTerms.get(j).getExp()) {
            Term temp = new Term((terms.get(i).getCoef() + otherTerms.get(j).getCoef()), terms.get(i).getExp());
            result += (temp.toString() + ";");
            i++;
            j++;
        }
    }

Better approach

Your approach is not really OOP. Ideally, the first expression should serve to create one instance of Polynomial and the other expression should serve to create another instance of Polynomial. Then there should be a method that can add another Polynomial instance to the own instance. Finally there should be a toString method that returns the instance as a string in the required format. Your driver code would then look like this:

        Polynomial a = new Polynomial("5x^2;-4x^1;3x^0");    
        Polynomial b = new Polynomial("6x^4;-1x^3;3x^2");
        Polynomial sum = a.addPoly(b);
        System.out.println(sum.toString());

This is much more object oriented, and will automatically avoid the code repetition that you currently have.

trincot
  • 317,000
  • 35
  • 244
  • 286