java - Trying to create an ADT that pairs two objects -


this question has answer here:

i'm working on abstract data type called pair in java. it's supposed take 2 objects , group them in data type. supposed take less 30 minutes, have been working @ 3 , half hours. believe have first 2 methods right, cannot figure out reverse or equals. can see attempted in code:

public class pair<t1,t2> implements pairinterface<t1,t2> {     // do: instance variables here.     public t1 first;     public t2 second;      public pair(t1 afirst, t2 asecond)     {         first = afirst;         second = asecond;     }      /* gets first element of pair.      * @return first element of pair.      */     public t1 fst()     {         return this.first;     }      /* gets second element of pair.      * @return second element of pair.      */     public t2 snd()     {         return this.second;     }      /* gets new pair represents reversed order      * of pair. example, reverse order of      * pair (x,y) (y,x).      * @return new pair in reverse order      */     public pairinterface<t2,t1> reverse()     {         return pairinterface<this.snd(),this.fst()>;//pair<second;first>     }      /* checks whether 2 pairs equal. note pair      * (a,b) equal pair (x,y) if , if      * equal x , b equal y.      *       * note if forget implement method,      * compiler not complain since class inherits      * method class object.      *       * @param otherobject object compared object.      * @return true if pair equal apair. otherwise      * return false.      */     public boolean equals(object otherobject)     {         if(otherobject == null)         {             return false;         }          if(getclass() != otherobject.getclass())         {             return false;         }          if (otherobject.fst.equals(this.fst) &&             otherobject.snd.equals(this.snd))         {             return true;         } else {             return false;         }     }      /* generates string representing pair. note      * string representing pair (x,y) "(x,y)". there      * no whitespace unless x or y or both contain whitespace      * themselves.      *       * note if forget implement method,      * compiler not complain since class inherits      * method class object.      *       * @return string representing pair.      */     public string tostring()     {         return "("+first.tostring()+","+second.tostring()+")";     } } 

public pairinterface<t2,t1> reverse()     {         return pairinterface<this.snd(),this.fst()>;//pair<second;first>     } 

first of all, can't return this

return pairinterface<this.snd(),this.fst()>;//pair<second;first> 

interfaces define methods class should offer, not implement these methods. therefore, impossible instantiate interface. can returned object implements pairinterface. can follows: return new pair(this.snd(), this.fst());

note instantiate object here keyword new, , give arguments constructor between brackets, instead of placing them between < >.

i don't quite understand yet rest of question about, i'll update post once do. won't give away solution, since, pointed out in comments on post, looks you're trying work.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -