943,748 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 414
  • Java RSS
Jun 14th, 2009
0

Generics and +=

Expand Post »
Hello all, it's been a while since I've had a question, but I'm back.

I'm trying to write a Vector class which can be any type. The problem I have is that generics can only hold objects, so when I try to say x+=vec.x it says that it cannot do that, I tried to make sure that it extends number, but the autoboxing doesn't seem to work with generics.

can anyone help?
Similar Threads
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Jun 14th, 2009
0

Re: Generics and +=

Can we see part of the code?
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jun 14th, 2009
0

Re: Generics and +=

You might also explain what you mean by "cannot do that".

And explain exactly what "vec.x" is, as that is not how you access something in a vector.

But, of course, don't forget the actual code.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jun 14th, 2009
0

Re: Generics and +=

what i mean by "cannot do that" is that because of the generic within the class trying to take the component x of type <T> and add it to another Java won't accept that as correct.
here is the code that is causing the problem

java Syntax (Toggle Plain Text)
  1. package cannonfodder;
  2.  
  3.  
  4. /**
  5.  * @author Bill
  6.  * @param <T> type
  7.  */
  8. public class Vector <T extends Number> {
  9. T x, y;
  10. /**
  11.   * @param x x
  12.   * @param y y
  13.   */
  14. public Vector(T x,T y) {
  15. this.x=x;
  16. this.y=y;
  17. }
  18. /**
  19.   * @param x x
  20.   */
  21. public void setX(T x) {
  22. this.x=x;
  23. }
  24. /**
  25.   * @param y y
  26.   */
  27. public void setY(T y) {
  28. this.y=y;
  29. }
  30. /**
  31.   * @param x x
  32.   * @param y y
  33.   */
  34. public void set(T x, T y) {
  35. this.x=x;
  36. this.y=y;
  37. }
  38. /**
  39.   * @param vec vector
  40.   */
  41. public void add(Vector<T> vec) {
  42. x+=vec.x;
  43. y+=vec.y;
  44. }
  45. /**
  46.   * @param vec vector
  47.   * @return dot
  48.   */
  49. public double dot(Vector vec) {
  50. return x*vec.x+y*vec.y;
  51. }
  52. /**
  53.   * @param vec vector
  54.   */
  55. public void sub(Vector vec) {
  56. x-=vec.x;
  57. y-=vec.y;
  58. }
  59. /**
  60.   * @param d d
  61.   */
  62. public void mult(double d) {
  63. x*=d;
  64. y*=d;
  65. }
  66. }
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Jun 15th, 2009
0

Re: Generics and +=

The + operator only works with certain types. If you aren't using it with those types, then it isn't going to work. If you want more detailed reasoning on this, masijade is probably your man. But you should try using the floatValue method of the Number class, then adding the floats you get. But for example, Number could be a BigDecimal, BigInteger, etc (look at the doc) - which can't be added as simply as an int.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jun 15th, 2009
0

Re: Generics and +=

Nope, it can't, mainly because Number itself can't use those operators, only a little over half the classes that extend Number can use those operators (only through autoboxing of course, which Number, of course, also can't do).

The compiler can only work with what it knows it has, and in this case, that is "Number", and "Number", as already mentioned, cannot use those operators (nor can it be autoboxed, of course).

You can still "get away with" some of this by making this an abstract class and making the last three methods abstract, otherwise you will have to implement a somewhat involved case statement in those methods.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jun 15th, 2009
0

Re: Generics and +=

Thanks, perhaps I'll just make it for only one type. The reason I wanted this is because I wanted to have a position in an array as a vector which has to be an int, but I also wanted to be able to have a vector that was in doubles for things like forces velocity and acceleration. but I suppose I will either have to make due with one, or write both separately. Thanks for the info.
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: pack200 for reducing size of applets
Next Thread in Java Forum Timeline: Java audio stream <<Very basic>>





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC