7 Replies - 418 Views - Last Post: 26 January 2011 - 03:39 AM Rate Topic: -----

#1 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Generics

Posted 25 January 2011 - 04:41 PM

Well I'm learning about Generics right now and I have to write a elementEqualTo(T e) method that returns the first element of this Pair found equal to to, if neither element is equal to e return null. I'm having a lot of trouble getting this to work (personally I think this is a terrible example to learn generics off of, it is to complex for someone's first experience with generics). Here is my code:

/**
 * A pair consists of two elements of the same type. This class
 * illustrates the definition of a generic type with type
 * parameter <tt>T</tt>.
 */
public class Pair<T> {
    private T firstElement;
    private T secondElement;

    /**
     * Default constructor.
     */
    public Pair() {
        this.firstElement = null;
        this.secondElement = null;
    }

    /**
     * Construct an instance of a <tt>Pair</tt> initialized
     * to the given elements.
     * @param e1 the first element of this pair
     * @param e2 the second element of this pair
     * @throws NullPointerException if either <tt>e1</tt> or
     *   <tt>e2</tt> is <tt>null</tt>.
     */
    public Pair( T e1, T e2 ) {
        if ( ( e1 == null ) || ( e2 == null ) ) {
            throw new NullPointerException();
        }
        this.firstElement = e1;
        this.secondElement = e2;
    }

    /**
     * Return the value of the first element of this pair.
     * @return the first element of this pair
     */
    public T getFirstElement() {
        return this.firstElement;
    }

    /**
     * Return the value of the second element of this pair.
     * @return the second element of this pair
     */
    public T getSecondElement() {
        return this.secondElement;
    }

    /**
     * Set the first element of this pair to the new value.
     * @param newFirst the new value for the first element of
     *   this pair
     * @throws NullPointerException if <tt>newFirst</tt> is
     *   <tt>null</tt>.
     */
    public void setFirstElement( T newFirst ) {
        if ( newFirst == null ) {
            throw new NullPointerException();
        }

        this.firstElement = newFirst;
    }

    /**
     * Set the second element of this pair to the new value.
     * @param newSecond the new value for the second element of
     *    this pair
     * @throws NullPointerException if <tt>newSecond</tt> is
     *   <tt>null</tt>.
     */
    public void setSecondElement( T newSecond ) {
        if ( newSecond == null ) {
            throw new NullPointerException();
        }
        this.secondElement = newSecond;
    }

    /**
     *  Swap the two elements.
     */
    public void swapElements() {
        T temp = this.firstElement;
        this.firstElement = this.secondElement;
        this.secondElement = temp;
    }
    
    public T elementEqualTo(T e) {
    	
    	if(e == null || !(this.equals(e))) {
    		return null;
    	}
    	else if(this.firstElement.equals(e)) {
    		return this.firstElement;
    	}
    	else {
    		return this.secondElement;
    	}
    }
    
    public boolean equals(Object o) {
    	if(o == null || !(o instanceof Pair<?>)) 
    		return false;
    	
    	System.out.println("A");
    	return ( (((Pair<?>) o).getFirstElement() == this.getFirstElement() ) && 
    			(((Pair<?>) o).getSecondElement() == this.getSecondElement()) );
    }

    /**
     * Returns a string representation of the object: this is
     * the string representation of element 1 followed by the
     * string representation of element 2.
     * @returns a string representation of the object.
     */
    public String toString() {
        return "< " + this.firstElement + ", " +
            this.secondElement + " >";
    }
}




All of this code except the equals() and elementEqualTo() method are from the book.

Is This A Good Question/Topic? 0
  • +

Replies To: Generics

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 902
  • View blog
  • Posts: 3,163
  • Joined: 12-May 09

Re: Generics

Posted 25 January 2011 - 05:28 PM

You'll probably get the answer right away from one of 6 people looking at this, but what isn't working for you? Is it not compiling? Is it not returning the correct value?

We have rules here! =p
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,055
  • Joined: 11-December 07

Re: Generics

Posted 25 January 2011 - 05:28 PM

Could you write the method if you were talking about Objects or Strings.

If it was like this, how would you do it:

public class Pair {
    private String firstElement;
    private String secondElement;

    public String elementEqualTo(String s) {
        // TODO
    }

}


And if it was like this?

public class Pair {
    private Object firstElement;
    private Object secondElement;

    public Object elementEqualTo(Object o) {
        // TODO
    }

}


If you can spot a pattern you should be able to do this:

public class Pair<T> {
    private T firstElement;
    private T secondElement;

    public T elementEqualTo(T x) {
        // TODO
    }

}

This post has been edited by cfoley: 25 January 2011 - 05:29 PM

Was This Post Helpful? 0
  • +
  • -

#4 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Generics

Posted 25 January 2011 - 05:48 PM

View Postcfoley, on 25 January 2011 - 05:28 PM, said:

Could you write the method if you were talking about Objects or Strings.

If it was like this, how would you do it:

public class Pair {
    private String firstElement;
    private String secondElement;

    public String elementEqualTo(String s) {
        // TODO
    }

}


And if it was like this?

public class Pair {
    private Object firstElement;
    private Object secondElement;

    public Object elementEqualTo(Object o) {
        // TODO
    }

}


If you can spot a pattern you should be able to do this:

public class Pair<T> {
    private T firstElement;
    private T secondElement;

    public T elementEqualTo(T x) {
        // TODO
    }

}


Yup, I could do that. I would probably implement it the way I'm trying to do it now in Pair. I guess the thing I'm confused about (and I should have mentioned this in the OP) is when I try I write the elementEqualTo() method I have to see if "e" is equal to either the first or second element of the Pair<T>. The thing is I can't cast "e" to a Pair<T> since it will be what ever object <T> is but the compiler won't let me cast just <T> or <?>.
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,055
  • Joined: 11-December 07

Re: Generics

Posted 25 January 2011 - 05:51 PM

Now I see your problem. :)

Let's take my String example. The method returns the fist string that is the same, or null. You're testing it against the two Strings, not the Pair.

Back to your example, you're testing it against the two T variables, not the Pair.

Hope that helps!
Was This Post Helpful? 1
  • +
  • -

#6 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Generics

Posted 25 January 2011 - 06:03 PM

View Postcfoley, on 25 January 2011 - 05:51 PM, said:

Now I see your problem. :)

Let's take my String example. The method returns the fist string that is the same, or null. You're testing it against the two Strings, not the Pair.

Back to your example, you're testing it against the two T variables, not the Pair.

Hope that helps!


Ah, that helped a lot! Sorry for not specifying my problem in the OP. I guess I still have to get used to the idea of thinking in terms of generics.

EDIT: I don't need to override the equals() method then do I? I can just use the Objects equal method that I'm comparing? i.e. use <String>'s equals method to compare the firstElement and SecondElement?

This post has been edited by giggly kisses: 25 January 2011 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,835
  • Joined: 27-December 08

Re: Generics

Posted 25 January 2011 - 09:43 PM

You should override the equals() method in your Pair class, unless you want your Pair objects to be compared using the == operator.

Also, why are you comparing the Pair elements using the == operator when they are Objects? Use the equals() method instead.
return ( (((Pair<?>) o).getFirstElement() == this.getFirstElement() ) 


Was This Post Helpful? 1
  • +
  • -

#8 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,055
  • Joined: 11-December 07

Re: Generics

Posted 26 January 2011 - 03:39 AM

Overriding equals isn't needed for this part of the problem. You can assume that whatever T is has an equals method that works properly.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1