0

Why doesn't this work? I'm not sure what other information you would require apart from Formula is given by a char and an int which makes a type Term.

// returns true if f is identical to this Formula 
// e.g. terms = {Term('C',2),Term('H',6)} and f = {Term('C',2),Term('H',6)} would return true 
// but  terms = {Term('C',2),Term('H',6)} and f = {Term('H',6),Term('C',2)} would return false

public boolean identical(Formula f)
{
   int fSize = f.getTerms().size();

   if(fSize!=terms.size())
   {
       return false;
   } 
   else 
   {
       for(int j = 0; j < fSize; j++)
       {
           Term tester = terms.get(j);
           Term fTester = f.getTerms().get(j);

           if(fTester == tester)
           {
               continue;
           } 
           else 
           {
               return false;
           }
       }
   }

   return true; 
}

N.B. terms is the name of the ArrayList

Noam M
  • 3,156
  • 5
  • 26
  • 41
Alfred
  • 67
  • 8
  • 4
    maybe because you are comparing Objects using the == operator, instead of a good equals implementation – Stultuske Apr 13 '16 at 09:33
  • damn, then it says Term cannot be converted to boolean. – Alfred Apr 13 '16 at 09:33
  • 4
    Probably because `if(fTester == tester)` compares addresses and not content. You need to implement `equals` and use it in order to compare custom objects. – Ori Lentz Apr 13 '16 at 09:34
  • "then" ? when? = is not an equals implementation, it's an operator used to assign a value. – Stultuske Apr 13 '16 at 09:35
  • How do you use the equals method? – Alfred Apr 13 '16 at 09:35
  • Comparing objects with == operator compares the instance id's not the objects himself. As already said. overwrite the equals method in your Term class and doing there your compare and return true or false. Thats it – Rene M. Apr 13 '16 at 09:36
  • http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java – Rene M. Apr 13 '16 at 09:36
  • https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html – Stultuske Apr 13 '16 at 09:37

2 Answers2

1

You need to compare two objects not using ==, but using the equals method so that the objects' contents are compared.

Since Term is your custom class, you need to override this method yourself:

class Term {
  char c;  //the two values inside your Term class
  int i;

  @Override
  public boolean equals(Object o){
    //checks omitted
    Term other = (Term)o;
    //now compare the contents:
    return i==other.i && c==other.c;
  }
}

Then you can compare them using

if(fTester.equals(tester)){
  continue;
} 
f1sh
  • 11,489
  • 3
  • 25
  • 51
  • when overriding `equals()` one must override `hashCode()` as well; I think it's worth mentioning. – Dmitry Bychenko Apr 13 '16 at 09:44
  • @DmitryBychenko Very true, but when you're stuck at these basics, the contract with ``hashCode()`` is only confusing. – f1sh Apr 13 '16 at 09:45
0

for compare two objects of Term you need override the methods equals() and hashCode() in Term class. Your code will be:

public boolean identical(Formula f)
{
   int fSize = f.getTerms().size();
   if(fSize!=terms.size()){
       return false;
   } else {
       for(int j = 0; j < fSize; j++){
           Term tester = terms.get(j);
           Term fTester = f.getTerms().get(j);
           if(fTester.equals(tester)){
               continue;
           } 
           else {
               return false;
           }
       }
   }

   return true; 
}
gradski
  • 71
  • 1
  • 2
  • 8