Results 1 to 8 of 8
Thread: incompatible types
- 10-22-2013, 02:05 AM #1
incompatible types
Hello all, I have been trying to get this to work for some time and I just don't know what I am doing wrong.
The problem I am having is that I am trying to get the method plus to return a TVector number so when the Object in the other class receives it the toString method can be called and work properly. Also the array "vect" has to be long.
Could anyone give any advice on how I would get this to work?
Java Code:public class TVector { private static final int N = 4; private long [] vect = new long[N]; public TVector ( String s ) { String[] tokens = s.split("[(),]"); vect = new long[N]; for(int i = 0; i < N; i++) { vect[i] = Integer.parseInt(tokens[i+1]); } } public TVector(TVector v) { vect = v.vect; } public TVector plus ( TVector adding ) { TVector [] total = new TVector[N]; for (int i = 0; i < N; i++) { total[i] = vect[i] + adding.vect[i]; } return total; } ... @Override public String toString() { String Prod; String finalProd = String.format("("); for(int i = 0; i < N - 1; i++) { Prod = String.format( vect[i] + ","); finalProd = finalProd + Prod; } Prod = ( vect[N - 1] + ")"); finalProd = finalProd + Prod; return finalProd; } }
Java Code:if (obj1 instanceof TVector && obj2 instanceof TVector) { //This has to compute the sum of obj1 and obj2 as TVector and assign the sum to obj TVector v1; v1 = (TVector)obj1; obj = v1; obj = v1.plus((TVector)obj2); System.out.println("The sum of obj1 and obj2: " + obj.toString());
Last edited by XNOViiCE; 10-22-2013 at 02:07 AM.
- 10-22-2013, 04:53 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: incompatible types
Your plus method return is declared to return a single TVector instance. However, you are returning an array of TVectors. So the return type
in the method signature needs to be TVector[].
The other problem is the following:
Java Code:total[i] = vect[i] + adding.vect[i];
Java Code:total[i].total = vect[i] + adding.vect[i]
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-22-2013, 05:14 AM #3
Re: incompatible types
Thank you so much for replying! I don't totally understand what you mean by the "total[i].total" part though. Do you mean that "total[i]" is still a TVector array and the ".total" is a type long?
- 10-22-2013, 05:21 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: incompatible types
Yes! TVector is a class which you made to represent some element. You cannot assign a long directly to a TVector anymore than you can assign a String type to an int. And since Java does not allow user defined operator overloading, you can't add two TVectors together either. So you are clearly wanting to add two longs together. Perhaps what you really want is the following:
Java Code:public TVector plus ( TVector adding ) { TVector total = new TVector(); for (int i = 0; i < N; i++) { total.vect[i] = vect[i] + adding.vect[i]; } return total; }
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-22-2013, 05:33 AM #5
Re: incompatible types
Oh! I understand what you are doing. With the "new TVector();" that will require a new constructor?
- 10-22-2013, 05:44 AM #6
Re: incompatible types
I got it, never mind about that last question. Thank you for all your help!
For reference and to see what I did.
Java Code:public TVector plus ( TVector adding ) { TVector total = new TVector(this); //copy constructor because we were required to have only those two constructors for (int i = 0; i < N; i++) { total.vect[i] = vect[i] + adding.vect[i]; } return total; }
Last edited by XNOViiCE; 10-22-2013 at 05:51 AM.
- 10-22-2013, 05:50 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: incompatible types
Yes. Create an empty TVector. I am guessing you just want to pass one TVector instance to another and it to the current TVector and return a new TVector. Like this:
Java Code:TVector sum = currentTVector.plus(someOtherTVector);
Java Code:public class TVector { private long[] vect; public TVector(long...myLongs) { vect = new long[myLong.length]; for ( int j = 0; j < myLongs.length; j++) { vect[j] = myLongs[j]; } } }
Java Code:TVector t = new TVector(1,2,3,4,5,6,19,192,39);
like you did earlier, where N = 4. The point is it makes it easy to pass and fill an array without parsing a String argument.
Regards,
JimLast edited by jim829; 10-22-2013 at 05:54 AM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-22-2013, 06:28 AM #8
Similar Threads
-
Why are these types incompatible?
By ras_oscar in forum New To JavaReplies: 4Last Post: 10-07-2013, 12:08 PM -
incompatible types
By mephistochen in forum New To JavaReplies: 14Last Post: 05-20-2013, 12:40 PM -
Incompatible types?
By jacjacjac in forum New To JavaReplies: 1Last Post: 10-13-2012, 10:01 PM -
incompatible types?
By slider57 in forum New To JavaReplies: 3Last Post: 09-26-2012, 01:55 PM -
incompatible types
By angelicatomnob in forum New To JavaReplies: 3Last Post: 01-17-2012, 04:25 PM
Bookmarks