Results 1 to 6 of 6
Thread: Generic Question
- 03-10-2011, 09:23 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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.
- 03-10-2011, 09:27 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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.
- 03-10-2011, 09:46 PM #3
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.
- 03-10-2011, 09:48 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
Sorry. I'm pretty sire the the problem is with the method
Eclipse shows the message: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; }
The method compareTo(T) in the type Comparable<T> is not applicable for the arguments (Comparable<T>)
- 03-10-2011, 09:56 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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)
- 03-10-2011, 10:50 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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
-
Generic question on implementing bluetooth/net code
By SM2010 in forum New To JavaReplies: 2Last Post: 12-14-2010, 04:10 PM -
Java Generic Container Question - please help!!!
By zhoujackji in forum New To JavaReplies: 3Last Post: 11-13-2010, 11:12 AM -
generic types
By jon80 in forum New To JavaReplies: 6Last Post: 06-12-2009, 10:29 PM -
A generic interface example
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:42 PM -
Generic Hashtables
By ShoeNinja in forum New To JavaReplies: 0Last Post: 12-04-2007, 10:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks