Results 1 to 8 of 8
  1. #1
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default 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 01:07 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default 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];
    total is an array of type TVector[]. You can't simply add two longs together and store them directly in a TVector[] array. You would need to do something like the following:
    Java Code:
    total[i].total = vect[i] + adding.vect[i]
    where total is a field in the TVector class and is of type long.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default 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?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default 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;
       }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: incompatible types

    Oh! I understand what you are doing. With the "new TVector();" that will require a new constructor?

  6. #6
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default 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 04:51 AM.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default 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);
    BTW, did you know you can use a variable length field for populating your TVector of arbitrary length?

    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];
          }
      }
    }
    Then you can do this:
    Java Code:
    TVector t = new TVector(1,2,3,4,5,6,19,192,39);
    In your case it might complicate things since it depends on the argument to declare the array size. But you could modify it to use a fixed length array
    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,
    Jim
    Last edited by jim829; 10-22-2013 at 04:54 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: incompatible types

    I didn't know that. Thank you for the tip! I will certainty use that in the future.

Similar Threads

  1. Why are these types incompatible?
    By ras_oscar in forum New To Java
    Replies: 4
    Last Post: 10-07-2013, 11:08 AM
  2. incompatible types
    By mephistochen in forum New To Java
    Replies: 14
    Last Post: 05-20-2013, 11:40 AM
  3. Incompatible types?
    By jacjacjac in forum New To Java
    Replies: 1
    Last Post: 10-13-2012, 09:01 PM
  4. incompatible types?
    By slider57 in forum New To Java
    Replies: 3
    Last Post: 09-26-2012, 12:55 PM
  5. incompatible types
    By angelicatomnob in forum New To Java
    Replies: 3
    Last Post: 01-17-2012, 03:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •