I have a Java class called Term holding polynomials like below

public Term(int c, int e) throws NegativeExponent {
    if (e < 0) throw new NegativeExponent();
    coef = c;
    expo = (coef == 0) ? 1 : e;
}

I also have an equals method in the same class like below

@Override
public boolean equals(Object obj) {

}

I am stuck with how to code how to compare these 2 Term objects

Within my JUnit test file I am using the test below to try and test the equals method

static org.junit.Assert.*;

import org.junit.Test;

public class ConEqTest
{
    private int min = Integer.MIN_VALUE;
    private int max = Integer.MAX_VALUE;



@Test
public void eq01() throws TError { assertTrue(new Term(-10,0).equals(new Term(-10,0))); }

Recommended Answers

All 4 Replies

how to compare these 2 Term objects

Would the two terms be equal if their coef and expo values were equal?
The equals() method would need to cast its arg to a Term object and access its contents.

Yes if the coef and expo values for both Term objects were the same they would be "equal". The coef and expo values are defined as follows:

final private int coef;
final private int expo;

So I don't really know at all how to compare 2 Term objects.

how to compare 2 Term objects

You need to compare the values of the variables in the two objects.
The equals() method is in one object and it can get at the two variables that are in its object.
The method is passed a reference to another Term object and can use that reference to get at the two variables in the passed object.

Your variables are private, so you will need to use accessor methods like getCoef() and getExpo() so you can accees those values for the other object.
OR
Because your variables are defined as final they are immutable, so there's no danger in making them public so you can access them directly.

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.