Results 1 to 6 of 6
  1. #1
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default Generic Question

    I'm trying to learn more about generics, but there are some problems in the DataSet class.
    The problem seems to be here:

    Java Code:
    		if (maximum == null || x.compareTo(maximum) == 1)
    			maximum = x;
    		if (minimum == null || x.compareTo(minimum) == -1)
    			minimum = x;

    The simple classes are below. Any hints?

    Thanks :)

    Java Code:
    package coin;
    
    public class Tester {
    	public static void main(String[] args)
    	{
    		DataSet<Coin> set = new DataSet<Coin>();
    		set.add(new Coin(.10, "Dime"));
    	}
    }
    Java Code:
    package coin;
    
    public class Coin implements Comparable<Coin>
    {
    	private double value;
    	private String name;
    
    	/**
          Constructs a coin.
          @param aValue the monetary value of the coin
          @param aName the name of the coin
    	 */
    	public Coin(double aValue, String aName) 
    	{ 
    		value = aValue; 
    		name = aName;
    	}
    
    	/**
          Gets the coin value.
          @return the value
    	 */
    	public double getValue() 
    	{
    		return value;
    	}
    
    	/**
          Gets the coin name.
          @return the name
    	 */
    	public String getName() 
    	{
    		return name;
    	}
    
    	public int compareTo(Coin o) {
    		if (this.value == o.value)
    			return 0;
    		if(this.value > o.value)
    			return 1;
    		else
    			return -1;
    	}
    }
    Java Code:
    package coin;
    
    public class DataSet<T>
    {
    	private Comparable<T> maximum;
    	private Comparable<T> minimum;
    
    	public DataSet()
    	{
    		minimum = null;
    		maximum = null;
    	}
    
    	public void add(Comparable<T> x)
    	{	
    		if (maximum == null || x.compareTo(maximum) == 1)
    			maximum = x;
    		if (minimum == null || x.compareTo(minimum) == -1)
    			minimum = x;
    	}
    
    	public Comparable<T> getMaximum()
    	{
    		return maximum;
    	}
    	
    	public Comparable<T> getMinimum()
    	{
    		return minimum;
    	}
    }
    Last edited by kwgivler; 03-10-2011 at 09:57 PM.

  2. #2
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Edit: sorry, I edited my original post. I'm still having problems with this :-\
    Last edited by kwgivler; 03-10-2011 at 09:39 PM.

  3. #3
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Any chance you can tell us what those "problems" are or do we have to guess?

    If you get error messages then copy and paste the EXACT messages and indicate which lines they occur on.

  4. #4
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Sorry. I'm pretty sire the the problem is with the method

    Java Code:
    	public void add(Comparable<T> x)
    	{	
    		if (maximum == null || x.compareTo(maximum) == 1)
    			maximum = x;
    		if (minimum == null || x.compareTo(minimum) == -1)
    			minimum = x;
    	}
    Eclipse shows the message:
    The method compareTo(T) in the type Comparable<T> is not applicable for the arguments (Comparable<T>)

  5. #5
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method compareTo(T) in the type Comparable<T> is not applicable for the arguments (Comparable<T>)
    The method compareTo(T) in the type Comparable<T> is not applicable for the arguments (Comparable<T>)

    at coin.DataSet.add(DataSet.java:16)
    at coin.Tester.main(Tester.java:7)

  6. #6
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    If anyone is interested, here is the solution:

    Java Code:
    package coin;
    
    public class DataSet<T extends Comparable<T>>
    {
    	private T minimum;
    	private T maximum;
    
    
    	public DataSet()
    	{
    		maximum = null;
    		minimum = null;
    	}
    
    	public void add(T x)
    	{	
    		if (maximum == null || x.compareTo(maximum) == 1)
    			maximum = x;
    		if (minimum == null || x.compareTo(minimum) == -1)
    			minimum = x;
    	}
    
    	public T getMinimum()
    	{
    		return minimum;
    	}
    	
    	public T getMaximum()
    	{
    		return maximum;
    	}
    }

Similar Threads

  1. Replies: 2
    Last Post: 12-14-2010, 04:10 PM
  2. Java Generic Container Question - please help!!!
    By zhoujackji in forum New To Java
    Replies: 3
    Last Post: 11-13-2010, 11:12 AM
  3. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 10:29 PM
  4. A generic interface example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:42 PM
  5. Generic Hashtables
    By ShoeNinja in forum New To Java
    Replies: 0
    Last Post: 12-04-2007, 10:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •