Results 1 to 9 of 9
  1. #1
    Hollywood_99 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default Returning Multiple Value's

    I don't know how to return more than one variable from a method. This code works, it returns val1 from the method get_Info(). However, what if I have a method that I want to return two variables? I want to know if it's possible to return val1 and val2 from the get_Info() method. How would I remove both variables and then put them into my run() method?

    I apologize for the newbie questions... your help is appreciated.


    public void run() {
    int value1 = get_Info(5);
    println("This is the first Value Returned " + value1);
    }


    private int get_Info(int x) {
    int val1 = 5 + x;
    int val2 = 10 + x;
    return val1;
    }

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    You can only return one thing from a method. You could however create an object that has components val1 and val2 and then return the entire object. Or just create two separate methods and return each integer.
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    Hollywood_99 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Again, Thank you. That is my next question. I am very close to figuring this out. In the following code, I believe I have created the object brick and then returned the object to my run() method. If this is true...then how do I access the brick GRect object from the run() method.

    ie: Let's say I want to change it's position. From my experience I can only do this within the method that created it.

    public class ReturnObject extends GraphicsProgram {

    public void run() {
    drawBrick(20,20,50,50);
    }

    private GRect drawBrick(double x,
    double y,
    double x1,
    double y1) {

    GRect brick = new GRect(x, y, x1, y1);
    add(brick);
    return brick;
    }

    }

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    You could set a GRect in the run method if I'm not mistaken:

    Java Code:
    GRect brick = drawBrick(20,20,50,50);

    Then if you wanted to change parts of the brick individually you would have to create new methods. A call to such a method would probably look like this if the method was called "setX" and had a GRect parameter:

    Java Code:
    brick.x = setX(brick);

    Let me know if this doesn't make sense and I'll try to explain further.
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    Hollywood_99 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Josh, thank you for taking the time to help me out. That makes perfect sense. That's how I have been doing it. I am trying to right my code in such a way that I don't have to duplicate code, which is why I created the drawBrick() method. I wanted to somehow create the the brick in the method and then manipulate it in the run() method.

    I guess that's not possible?

    Chad

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    So you want to be able to do something like...

    Java Code:
    brick.x = 50;

    I'm just trying to understand fully, and you're welcome, it's no trouble at all.
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    Hollywood_99 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    I want to be able to do the following code in the run() method

    brick.x = setX(brick);

    when the method that created the box was in drawbrick().

  8. #8
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Adding the following method should work then:

    Java Code:
    private void setX(GRect brick)
    {
        x = 50;
    }

    Also, if you could post an updated version of your code that would help a lot so I could see where you are at now.
    Sincerely, Joshua Green
    Please REP if I help :)

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default

    Quote Originally Posted by Hollywood_99 View Post
    I want to be able to do the following code in the run() method

    brick.x = setX(brick);

    when the method that created the box was in drawbrick().
    What is a GRect?
    Is something checking the values in that brick object created every so often to see if it's changed?

    If there isn't some mechanism like that then simply changing them won't result in it moving.

    Of course I don't actually know the framework you're using, so it's possible that stuff add()ed to a GraphicsProgram is checked in this way.

Similar Threads

  1. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 03:48 PM
  2. returning arrays
    By cjohnson412 in forum New To Java
    Replies: 4
    Last Post: 11-25-2008, 01:30 PM
  3. Why is my list returning nothing?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-05-2008, 03:51 PM
  4. Replies: 5
    Last Post: 07-18-2008, 04:30 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
  •