0

I'm working on an Abstract Data Type called Pair in Java. It's supposed to take two objects and group them together in this data type. This is supposed to take less than 30 minutes, but I have been working at it for 3 and a half hours. I believe I have the first two methods right, but I cannot figure out reverse or equals. You can see what I attempted in the code:

public class Pair<T1,T2> implements PairInterface<T1,T2>
{
    // TO DO: Your instance variables here.
    public T1 first;
    public T2 second;

    public Pair(T1 aFirst, T2 aSecond)
    {
        first = aFirst;
        second = aSecond;
    }

    /* Gets the first element of this pair.
     * @return the first element of this pair.
     */
    public T1 fst()
    {
        return this.first;
    }

    /* Gets the second element of this pair.
     * @return the second element of this pair.
     */
    public T2 snd()
    {
        return this.second;
    }

    /* Gets a NEW pair the represents the reversed order
     * of this pair. For example, the reverse order of the
     * pair (x,y) is (y,x).
     * @return a NEW pair in reverse order
     */
    public PairInterface<T2,T1> reverse()
    {
        return PairInterface<this.snd(),this.fst()>;//Pair<second;first>
    }

    /* Checks whether two pairs are equal. Note that the pair
     * (a,b) is equal to the pair (x,y) if and only if a is
     * equal to x and b is equal to y.
     * 
     * Note that if you forget to implement this method, your
     * compiler will not complain since your class inherits this
     * method from the class Object.
     * 
     * @param otherObject the object to be compared to this object.
     * @return true if this pair is equal to 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 a string representing this pair. Note that
     * the String representing the pair (x,y) is "(x,y)". There
     * is no whitespace unless x or y or both contain whitespace
     * themselves.
     * 
     * Note that if you forget to implement this method, your
     * compiler will not complain since your class inherits this
     * method from the class Object.
     * 
     * @return a string representing this pair.
     */
    public String toString()
    {
        return "("+first.toString()+","+second.toString()+")";
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dan Smith
  • 33
  • 5
  • 1
    What do you mean by _"cannot figure out reverse or equals"_? Please explain in more detail. Also, read the [FAQ] and [Ask]. Your question as written sounds too much like "please do the work for me". – Jim Garrison Sep 10 '14 at 15:42
  • Well, I'll start with equals. Im assuming its used to determine if another pair is equal to this pair so why does it take an object and not a pair? How can I compare a random object with a pair? – Dan Smith Sep 10 '14 at 15:44
  • As far as reverse, its supposed to return a pairInterface so I think what I have should work but the compiler is asking for a semicolon inbetween this.snd() and this.fst(). Why is that? – Dan Smith Sep 10 '14 at 15:48
  • For reverse, it looks like your instructor is trying to give you a BIG clue in the javadoc. – GenericJon Sep 10 '14 at 15:53
  • @GenericJon Thanks the clue was the NEW right? didn't even realize he was talking about the keyword new went right over my head. – Dan Smith Sep 10 '14 at 18:21
  • @JimGarrison I figured out reverse with the help from Ward L but I also cant figure out how to get equals to run the way I want it to. I know its trying to compare two pairs (Not sure why the one is passed as "Object otherObject" could you explain that?) so I tried to use the .fst and .snd methods to do get their first and second values and compare them with the .equals method but when I do that I get a compiling error "cannot find symbol"... – Dan Smith Sep 10 '14 at 18:36

1 Answers1

0
public PairInterface<T2,T1> reverse()
    {
        return PairInterface<this.snd(),this.fst()>;//Pair<second;first>
    }

First of all, you can't return this

return PairInterface<this.snd(),this.fst()>;//Pair<second;first>

Interfaces define methods that a class should offer, but do not implement these methods. Therefore, it is impossible to instantiate an interface. What can be returned however is an object that implements your PairInterface. You can do this as follows: return new Pair(this.snd(), this.fst());

Note that we instantiate an object here with the keyword new, and that we give the arguments for the constructor between brackets, instead of placing them between < >.

I don't quite understand yet what the rest of your question is about, but I'll update this post once I do. I won't however give away the solution, since, as pointed out in the comments on your post, it looks like you're trying to get us to do your work.

WardL
  • 96
  • 5
  • Hey I think i figured out the reverse I had tried pair before I just didn't think to add the keyword new. Thank You for the help btw Im really interested in learning this but was getting nowhere from the textbook. – Dan Smith Sep 10 '14 at 18:15
  • For reverse its supposed to take two "Pairs" and compare them. The third if statement is the one I added. Even though it takes in an object I'm assuming that object must be a Pair for it to be equal to another pair so I used the .fst and .snd methods to compare them (via a .equals method) to the this.fst and this.snd. Am I close to having this correct or completely off base? – Dan Smith Sep 10 '14 at 18:19
  • Looking at it more... Am I supposed to do something to otherObject to make it into a pair? Im assuming thats what was meant by "@return true if this pair is equal to aPair" but im not sure – Dan Smith Sep 10 '14 at 18:23
  • Voilà, you solved it! After making sure the object is a pair, you have to "make it into a Pair". You can do this by casting it to a Pair like this: ( (Pair) otherObject ) – WardL Sep 10 '14 at 19:08
  • I tried this but it isnt working: 'Pair aPair = new (Pair)otherObject;' – Dan Smith Sep 10 '14 at 19:33
  • NVM I got it I created a new blamk constructor for Pair and then: Pair aPair = new Pair(); aPair = (Pair)otherObject; THANK YOU I own you one man! This was really difficult for me today! – Dan Smith Sep 10 '14 at 19:53
  • Your first solution, "Pair aPair = new (Pair)otherObject" is actually not so far off. What you need to do to make it work is drop the keyword "new". In this case, you don't want to create a new object, since you already have one: otherObject. Simply doing Pair aPair = (Pair) otherPair; will do the job. – WardL Sep 10 '14 at 20:25
  • @MikeOles Not sure why you're casting, or creating another constructor. The one line `return new Pair(second,first)` should be all you need. – GenericJon Sep 11 '14 at 10:14
  • This is about his equals method: he has determined that the objects are of the same class, and now wants to check if their fst and snd are equal. To do this however, he forgot to cast the otherObject to a Pair. By forgetting this, he did not have access to the fst- and snd-attribute. – WardL Sep 11 '14 at 11:48