Generics and +=

Thread Solved

Join Date: Jun 2008
Posts: 408
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Generics and +=

 
0
  #1
Jun 14th, 2009
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?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,650
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: Generics and +=

 
0
  #2
Jun 14th, 2009
Can we see part of the code?
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,356
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Generics and +=

 
0
  #3
Jun 14th, 2009
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 408
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Generics and +=

 
0
  #4
Jun 14th, 2009
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

  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. }
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,566
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Generics and +=

 
0
  #5
Jun 15th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,356
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Generics and +=

 
0
  #6
Jun 15th, 2009
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 408
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Generics and +=

 
0
  #7
Jun 15th, 2009
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.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC