I am creating a method to multiply 2 polynomial expressions together such that:

3x^5 * 2x^3 = 6x^8 -> Where the coefficients are multiplied and the exponents are added together.

My test case for this would look something like the following

@Test
public void times01() throws TError { assertEquals(Term.Zero,       Term.Zero.times(Term.Zero)); }

I should also add that Term.Zero = (0,0) and Term.Unit = (1,0) So anything multiplied by Term.Zero is Term.Zero and anything multiplied by Term.Unit returns itself as Term.Unit effectively is 1.

public Term times(Term that) throws CoefficientOverflow, ExponentOverflow, NegativeExponent {

    return null;
}

This is the times method. I'm asking for some help with coding the times method? The problem I've found is how to deal with the 3 Term objects, Term1, Term2 and Term3 and not using an endless amount of if-statements.

What steps does the code need to take to do the multiplication? Or what algorithm have you got for doing it? Once you get a design for the method, then work on how to impliment it.

I have designed the following pseduo code so far:

Term1 == Term.Zero OR Term2 == Term.Zero => Term3 = Term.Zero
Term1 == Term.Unit => Term3 = Term2
Term2 == Term.Unit => Term3 = Term1

Term1.coef * Term2.coef = Term3.coef
Term1.expo * Term2.expo = Term3.expo

With the following code

@SuppressWarnings("null")
public Term times(Term that) throws CoefficientOverflow, ExponentOverflow, NegativeExponent {
    Term term1 = new Term(coef,expo);
    Term term2 = that;
    Term term3 = null;

    if(term1 == Zero || term2 == Zero) {
        term3 = Zero;
    } else if(term1 == Unit) {
        term3 = term2;
    } else if(term2 == Unit) {
        term3 = term1;
    } else if(term1.coef == 2 && term1.expo == 0) {
        term3.coef = term2.coef * 2;
        term3.expo = term2.expo;
    } else if(term2.coef == 2 && term2.expo == 0) {
        term3.coef = term1.coef * 2;
        term3.expo = term1.expo;
    } else {
        term3.coef = term1.coef * term2.coef;
        term3.expo = term1.expo + term2.expo;
    }



    return term3;
}

But this made me have to change my "coef/expo" variables in the Term class from

final private int coef;

to

private int coef;

And this gives an error on the following test...

@Test
public void times09() throws TError { assertEquals(new Term(min,2), new Term(hmin,2).times(new Term(2,0))); }

Any ideas?

And this gives an error on the following test...

What is the full text of the error message?

I know nothing about JUnit testing. You'll have to wait until someone that understands JUnit testing comes along.

All other tests (there are 27, 9 of them are working) are failing with

java.lang.NullPointerException

with the following lines of code listed

term3.coef = term1.coef * 2

and

term3.coef = term1.coef * term2.coef;

This is my Term constructor too.

public class Term
{
    private int coef;
    private int expo;

    private static Term ZERO, UNIT;

    static  {   try {   ZERO = new Term(0,0);       // the number zero
                        UNIT = new Term(1,0);       // the number one
                }
                catch (Exception e) {
                    // constructor will not throw an exception here anyway
                }
            }

    /**
     * 
     * @param c The coefficient of the new term
     * @param e The exponent of the new term (must be non-negative)
     * @throws NegativeExponent 
     */
    public Term(int c, int e) throws NegativeExponent{
        if (e < 0) throw new NegativeExponent();
        coef = c;
        expo = (c == 0 && e != 0) ? 0 : e;
    }

    final public static Term Zero = ZERO;
    final public static Term Unit = UNIT;

NormR1, with the JUnit testing, you shouldn't need to understand JUnit, I am trying to get back Term(min,2) from Term(hmin,2) * Term(2,0) as this is the following
(half of min)x^2 * 2x^0 => (half of min)x^2 * 2 = Term(min,2)

java.lang.NullPointerException

What variable is null?

Do you have a single file with all the parts need for resting that will show the problem?
It needs to compile with javac and execute with java.exe

Be a part of the DaniWeb community

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