• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics Question  RSS feed

 
Vince Stout
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am having some problems with an assignment.

The assignment is to create a generic class with a type parameter 'T' which needs to be constrained to an upper bound: Number. The class needs to have one field, an ArrayList of type 'T'. I need to provide 3 methods: add() to add an element to the list, largest() to return the largest value in the list and smallest() to do the opposite of largest.

The problem is that beings Number is the superclass of 'T', I am getting errors when trying to use greater/lessthan operators and the compareTo() method to find the largest/smallest value. This is because Number does not support either of these operations.

My question is how can I get my values without using greater/lessthan operators or the compareTo() method? I feel like I'm missing something. It doesn't seem like this should be a hard assignment, but I was up all last night working on it. I tried writing overloaded helper methods to call from largest() named getLargest() which had different returns (Integer, Double etc...). I had tried casting 'T' to the method when calling it but that only worked when I had the Integer method written. As soon as I wrote the Double version an error was thrown that getLargest() was already defined.

Any insight would be greatly appreciated, code to follow.

Code:


Edit: I have come up with a way to do this that seems to work, but I'm not sure if it will accomodate all subclasses of number. So far I have tested it on Integer and Long.

Code for new largest() method


Re-Edit: I've tested everything pretty thoroughly and this the revised largest method seems to be the way to go. My biggest fear was that the long value would overflow but it didn't. I've now written the smallest() method and have tested the class with types: Short, Float, Double, Long and Integer. I have not messed with BigDecimal, BigInteger etc... but considering they all take a long value in their constructor, and my method will accommodate long values, I think it will work ok.

Any additional insight would still be appreciated however.
 
Marcus Kelvin
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My biggest fear was that the long value would overflow but it didn't. I've now written the smallest() method and have tested the class with types: Short, Float, Double, Long and Integer. I have not messed with BigDecimal, BigInteger etc... but considering they all take a long value in their constructor, and my method will accommodate long values, I think it will work ok.


I think the way you did it is the only feasible way to go, but I also think the criteria is a little misguided ;).

A long cast/converted to a double will not overflow because the range of a double is much greater, but it can lose precision, eg:



Outputs:

8500964271916320249
8500964271916319744

A difference of < 0.000000000000001 %, but it still means that beyond a certain point, long values within a range converted to a double and back will equate with one another when they would not have done so as longs. If you tack onto that:



Guess what "Won't happen!" means? The value of d never changed, even though ln did.

The "Big" classes introduce further discrepancies this way, but there is no way around this by the specs of your assignment.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using doubleValue() will probably be crippled when used with BigDecimal or BigInteger, when you have values that are too large to fit into a double.

I would limit to comparable numbers only. That allows you to use their own comparison:
I added Comparable<? super T>, not Comparable<T>, to also allow classes that extend another Comparable class. A good (non-number) example is java.sql.Timestamp. Through inheritance it implements Comparable<Date>, not Comparable<Timestamp>. With the ? super you still allow such cases.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vince Stout wrote:. . . I have not messed with BigDecimal, BigInteger etc... . . .
The sooner people learn about those classes, the better. They are good. Example of the use of BigDecimal here. I would ignore it because the person who posted that never posts anything worthwhile, however.
 
Vince Stout
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. Thanks for the replies. Sorry it took so long to get back, but its been a busy week.

@Marcus - thanks for the example on precision loss during conversion. I had figured there would be some, but its cool to see it broken down.

@Campbell - yeah, I saw some interesting methods in those classes when doing research for the above assignment. Thanks for the example. Oh yeah, lol.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!