Results 1 to 13 of 13
- 04-29-2012, 12:53 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
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); }
- 04-29-2012, 01:28 PM #2
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.
- 04-29-2012, 01:40 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
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 ?
- 04-29-2012, 01:43 PM #4
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.
- 04-29-2012, 01:49 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
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?
- 04-29-2012, 01:57 PM #6
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.
- 04-29-2012, 02:07 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
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
- 04-29-2012, 02:29 PM #8
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.
- 04-29-2012, 02:36 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
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(); }
- 04-29-2012, 02:39 PM #10
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.
- 04-29-2012, 02:45 PM #11
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
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.
- 04-29-2012, 02:48 PM #12
Re: Noob question - Create objects using objects as parameters
Use a variable instead of hardcoding the dot.want to, for example change a dot on line 4 to an X
Then change the variable's value to be what you want to print.If you don't understand my response, don't ignore it, ask a question.
- 04-29-2012, 02:55 PM #13
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
create new Objects with for loop
By nome in forum New To JavaReplies: 4Last Post: 02-28-2012, 02:26 AM -
objects as parameters change in method
By zcd in forum New To JavaReplies: 0Last Post: 01-29-2012, 08:37 PM -
Problem passing objects as parameters to remote methods via RMI
By nicoeschpiko in forum New To JavaReplies: 1Last Post: 02-03-2011, 05:43 PM -
how to use objects and its values to use in other function without using parameters
By sfaiz in forum New To JavaReplies: 3Last Post: 09-29-2010, 02:38 PM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks