Results 1 to 8 of 8
  1. #1
    tnahsin is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default How do i return values in object return type function(method)???

    How do i take input values for TwoDPoint (which are objects) and return it back in numerical values also print them. When i create an object in main method and pass values to my function of return type TwoDPoint,it gives error:- found int,int need TwoDPoiint,TwoDPoint.

    // Here is what i tried to do:

    Created class TwoDPoint that contains two fields x, y which are of type int. Defined another class TestTwoDPoint, where a main method is defined.In the main method created two TwoDPoint objects.

    Then I modified the class TestTwoDPoint and add another function to it. This function takes two TwoDPoints as input and returns the TwoDPoint that is farthest from the point (0,0).

    Then I added another function to TestTwoDPoint. This function takes two TwoDPoints as input and returns a new TwoDPoint whose x value is the sum of x values of the input TwoDPoint’s and whose y value is the sum of the y values of the input TwoDPoint’s.




    class TwoDPoint {
    int x = 2;
    int y = 4;
    }
    class TestTwoDPoint {
    public static void main(String args[]) {
    TwoDPoint obj1 = new TwoDPoint();
    System.out.println(obj1.x);
    System.out.println(obj1.y);
    TestTwoDPoint obj = new TestTwoDPoint();
    obj.newfunction(5, 6);
    }
    public TwoDPoint newfunction(int a, int b) {
    TwoDPoint obj2 = new TwoDPoint();
    obj2.x = a;
    obj2.y = b;
    return obj2;
    }
    public TwoDPoint firstFunction(TwoDPoint p1, TwoDPoint p2) {
    int x1 = p1.x;
    int x2 = p2.x;
    int y1 = p1.y;
    int y2 = p2.y;
    if ((x1 > 0 && x1 > x2) && (y1 > 0 && y1 > y2))
    return p1;
    else
    return p2;
    }
    public TwoDPoint secondFunction(TwoDPoint t1, TwoDPoint t2) {
    int nx = t2.x;
    int ny = t2.y;
    return
    }
    }

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How do i return values in object return type function(method)???

    Hi,

    1) When you post code, then you should put it into CODE tags so it is formatted properly.

    2) Please copy all error details if you encounter an error.

    3) What I saw in your code right now:
    - You created a class TwoDPoint with just x and y fields in it. No functions.
    - Inside your class TestTwoDPoint you created a function newtwodpoint which returns a twodpoint. - But you try to call it from an instance of TwoDPoint which is not possible (because it is in your Test class!)
    - Your secondfunction seems to be incomplete. Your return statement should give a TwoDPoint back.

    With kind regards,

    Konrad

  3. #3
    tnahsin is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: How do i return values in object return type function(method)???

    Java Code:
    TestTwoDPoint obj = new TestTwoDPoint();
    obj.newfunction(5, 6);
    i am calling it with object of TestTwoDPoint
    The reason i did'nt write anything after return because i have confusion in that. I want to return new TwoDPoint whose x value is the sum of x values of the input TwoDPoint’s. and same with y values.Also how i can use the object return from this to print values.

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How do i return values in object return type function(method)???

    You are correct of course. Sorry for my mistake.

    You can either store the TwoDPoint inside a variable for further use or you could directly use the returning object.
    I created a quick example (where I moved everything into the TwoDPoint class).

    I just modified the code a little bit because I think that it is easier to understand something if you see an example:

    Java Code:
    class TwoDPoint {
        int x;
        int y;
    
        // Renamed the newfunction to createInstance
        // Also made it static, because no instance is required (No use of "this" inside the function!)
        public static TwoDPoint createInstance(int a, int b) {
            TwoDPoint obj2 = new TwoDPoint();
            obj2.x = a;
            obj2.y = b;
            return obj2;
        }
    
        // Such a createInstance function is normally not required / used.
        // Instead a constructor is created for this:
        public TwoDPoint(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    
        // We need to define a default constructor on our own because Java is only
        // creating one if there is no constructor at all
        public TwoDPoint ()
        {
            x = 2;
            y = 4;
        }
    
        // Just a simple method I want to use later when concatenating stuff on one line
        public void print()
        {
            System.out.println("TwoDPoint(" + x + ", " + y + ")");
        }
    
        public TwoDPoint firstFunction(TwoDPoint p1, TwoDPoint p2) {
            int x1 = p1.x;
            int x2 = p2.x;
            int y1 = p1.y;
            int y2 = p2.y;
            if ((x1 > 0 && x1 > x2) && (y1 > 0 && y1 > y2))
                return p1;
            else
                return p2;
        }
    
        // This function is not really related to a generic TwoDPoint. I would place it into
        // something like TwoDPointTest or TestTwoDPoint as you did. But the other functions
        // are related to the TwoDPoint so I would keep them inside that class!
        public static void main(String args[]) {
            TwoDPoint obj1 = new TwoDPoint();
            System.out.println(obj1.x);
            System.out.println(obj1.y);
    
            // We use createInstance and store it inside a variable for later use and then call print.
            TwoDPoint obj = TwoDPoint.createInstance(5, 6);
            obj.print():
    
            // We directly create and print an instance without storing it for later use
            TwoDPoint.createInstance(5,6).print();
        }
    }
    So you see inside the main function, that I get an instance of the createInstance call inside a variable and in the next line I simply chain up the method calls so the instance of the createInstance call is used to call print.

    Another point is, that I made the createInstance call static. This is recommend, because it does not use an instance of an object at all.
    If you want to create an instance, the common way is to use a constructor. So I wrote a constructor that is doing more or less the same. (only the new operator and the return of the value is left out because that is moved outside.)
    So a TwoDPoint.createInstance(a,b) is now equal to new TwoDPoint(a,b).
    Now that I added a constructor, the default constructor is no longer build be java. So I also added a constructor with no argument.
    (Maybe I should have left out the constructors. I hope that I do not confuse you!)

    And the last part is just showing how to store an instance you returned or simply use it to call another function.

    With kind regards,

    Konrad

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,165
    Rep Power
    6

    Default Re: How do i return values in object return type function(method)???

    If you must tell people to start applying statics (something I don't support) then at least do it right:

    Java Code:
    public static TwoDPoint createInstance(int a, int b) {
         return new TwoDPoint(a, b);
    }
    And by correcting the code I hope I show you the futility of this exercise: you might as well just use the constructor. The new keyword exists for a reason: to be used, not to be hidden behind static factory methods.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How do i return values in object return type function(method)???

    Hi gimbal,

    that is the problem with mixing up multiple points. The point with the static function is not really connected with the constructor that I would introduce instead. So I am not using the constructor inside of it. (If there is such a constructor, the whole createInstance thing is simply obsolete.)

    And just for your information: Inside the .Net development area in which I earn my money, a non-static function that is not using the instance is causing a code analysis warning (when using the Code Analysis of Visual Studio). That is something that you might not support or do not like. But there are also some reasons for this. But as always: These are personal views and each person has its own likes and dislikes. (I just wanted to explain why I see such things even without a tool...)

    With kind regards,

    Konrad

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,165
    Rep Power
    6

    Default Re: How do i return values in object return type function(method)???

    That makes -no- sense to me at all.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How do i return values in object return type function(method)???

    Which part does not make sense to you?

    a) That I handled multiply points in one reply? So that I mentioned a static function that returns an instance? (Which could be used with Singletons just to give you an example?)

    b) To make a function static if no instance is required by the function? You can see this point as you want. Why do you want to force the presence of an instance if it is not required at all? That is something that does not make sense to me. But we do not have to discuss that point. There are enough discussions on the net about this e.g. Java: when to use static methods - Stack Overflow

    c) Maybe just the information about my daily job. I would ask what part didn't make sense in your eyes. Maybe the "code analysis" thing is unknown to you. But a discussion about that is also nothing I would like to have with you.

    With kind regards,

    Konrad

Similar Threads

  1. question about return type of overriding the method
    By thinkinjava24 in forum New To Java
    Replies: 6
    Last Post: 01-15-2013, 08:33 AM
  2. Method to return values
    By puchatek in forum New To Java
    Replies: 4
    Last Post: 11-11-2010, 10:27 AM
  3. [SOLVED] Undeclared Return Type for Method?
    By fullmetaljacket in forum New To Java
    Replies: 11
    Last Post: 05-16-2009, 04:38 AM
  4. Method return type problem
    By McChill in forum New To Java
    Replies: 7
    Last Post: 05-05-2009, 10:21 PM
  5. Replies: 4
    Last Post: 11-25-2008, 06:53 AM

Tags for this Thread

Posting Permissions

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