Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Norm

Thread: Noob question - Create objects using objects as parameters

  1. #1
    pantaloc is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Noob question - Create objects using objects as parameters

    Hi, I'm trying to create a method to capture user inputs and use them as parameters for creating new objects, which are also passed as parameters into this method.
    This is what I had so far and it does not pass the user input into newly created object as desired. Since I'm very new to Java, this is the best way I could think of to do this.
    If there is a better way would you guys kindly provide me with a recommendation please. Any help will be dearly appreciated!

    Java Code:
        public void usrInput(int objnumber, anObject blah)
        {
            System.out.println("Object #"+objnumber);
            System.out.print("Object's x: ");
            int x = keyboard.nextInt();
            keyboard.nextLine();
            System.out.print("Object's y: ");
            int y = keyboard.nextInt();
            keyboard.nextLine();
            blah = new anObject(x, y);
            
        }
    
        public Create()
        {                
            usrInput(1, blah1);
            usrInput(2, blah2);
            usrInput(3, blah3);
    
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Noob question - Create objects using objects as parameters

    Line 10 changes the value of a local variable: blah only. When the method exits its value will be gone. Parameters are passed by value which means they are local to the method.
    You should have the method return the newly created object if you want the caller of the method to have access to it. The userInput method can call methods in the anObect class that could change the contents of the passed object, but it can not change the value that the caller passed.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pantaloc is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Noob question - Create objects using objects as parameters

    Hi sir, thank you very much for the reply. So what would be a better way of solving this problem, I don't quite understand about returning the created object, is it possible to do that using the method I created ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Noob question - Create objects using objects as parameters

    Define the method like this:
    public AnObject usrInput(int objnumber)
    ...
    return blah;
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    pantaloc is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Noob question - Create objects using objects as parameters

    Thank you for the reply. In that case, would it mean this method can only create the object blah, how would I go to create blah1, blah2 and blah3?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Noob question - Create objects using objects as parameters

    Each call to the method would create a new instance of the AnObject class. You can assign the returned value to a variable.
    The variables: blah, blah1, blah2 are not different types of objects. They all reference instances of the AnObject class.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    pantaloc is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Noob question - Create objects using objects as parameters

    It worked like magic, thank you so so so so much for your great help! Oh, is it against the rules of the forum to ask for your help for another problem, which I asked in another thread? :D

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Noob question - Create objects using objects as parameters

    If the new problem is very different use a new thread otherwise ask it here.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    pantaloc is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Noob question - Create objects using objects as parameters

    What I'm trying to do is to print 3 blank lines, then 3 lines of 13 dots under it, followed by another 2 blank lines and finally a line of 13 underscores.

    What I've got so far is a very messy piece of code, which I wish to simplify. Is it possible to do this in a 2D array, if so how would I loop through it to input different characters on different lines? Or is there another way without using array?

    Java Code:
    for (int y = 1; y <= 3; y++)
    {
        for (int x = 0; x < 13; x++)
        {
            System.out.print(" ");
        }
        System.out.println();
    }
    for (int y = 4; y <= 6; y++)
    {
        for (int x = 0; x < 13; x++)
        {
            System.out.print(".");
        }
        System.out.println();
    }
    for (int y = 7; y <= 8; y++)
    {
        for (int x = 0; x < 13; x++)
        {
            System.out.print(" ");
        }
        System.out.println();
    }
    for (int x = 0; x < 13; x++)
    {
        System.out.print("_");
        System.out.println();
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Noob question - Create objects using objects as parameters

    Post the output, explain what is wrong with it and show what you want it to be.

    What would be the purpose of an array? It would take one set of loops to fill it and another set of loops to print it.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    pantaloc is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Noob question - Create objects using objects as parameters

    The above piece of code is working fine, its because it's really messy so I was thinking of using an array to make it cleaner, plus because I also want to, for example change a dot on line 4 to an X under certain conditions (using if statements), I think it would be easier if I use an array.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Noob question - Create objects using objects as parameters

    want to, for example change a dot on line 4 to an X
    Use a variable instead of hardcoding the dot.
    Then change the variable's value to be what you want to print.
    pantaloc likes this.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    pantaloc is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Noob question - Create objects using objects as parameters

    Thank you very much, you've shown me the light yet again :D Have a good day sir!

Similar Threads

  1. create new Objects with for loop
    By nome in forum New To Java
    Replies: 4
    Last Post: 02-28-2012, 02:26 AM
  2. objects as parameters change in method
    By zcd in forum New To Java
    Replies: 0
    Last Post: 01-29-2012, 08:37 PM
  3. Replies: 1
    Last Post: 02-03-2011, 05:43 PM
  4. Replies: 3
    Last Post: 09-29-2010, 02:38 PM
  5. Replies: 1
    Last Post: 01-22-2009, 04: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
  •