Results 1 to 3 of 3
- 03-08-2011, 11:37 AM #1
Member
- Join Date
- May 2010
- Posts
- 25
- Rep Power
- 0
Dynamic Create Object based on user's input?
If the following line is for me to create a Car object,
Car car1 = new Car();
How can I create a dynamic list of car based on the user's input?
For example, if user enters 5,
I wish to do the following:
Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();
Car car4 = new Car();
Car car5 = new Car();
I wish to be able to randomly assign color to the cars like this:
for(int i=1; i<6; i++){
... //not sure how can I concatenate 'car' with the number...
}
How can I do so?
Should I create an array of Car with the size inputted by the user?
- 03-08-2011, 11:40 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Yes.
You are creating an array of cars of whatever size the user has enetered.
Or a List<Car> if you don't know how many the user is going to create (and you've covered Collections and generics).
- 03-08-2011, 04:47 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
to chooser random colors you can also create an array to hold the colors and randomly index into the array to get the colors to set the car's color variable
I gave you way more than you need to solve this(I ma have given more than I should have)Java Code:Color[] colors = { Color.black, Color.red, Color.green }; Random rand = new Random(); combine colors and random in the car constructor.
Similar Threads
-
Displaying a square based on user's input
By luke in forum New To JavaReplies: 15Last Post: 07-14-2012, 09:43 AM -
Dynamic creation of custom filter based on the columns visible in the tableColumnHead
By neha_sinha in forum AWT / SwingReplies: 5Last Post: 07-09-2010, 06:14 AM -
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
How to create dynamic string object???
By Stephen Douglas in forum New To JavaReplies: 8Last Post: 04-12-2010, 02:35 AM -
Why "Exception while reading user's input as an int"
By soc86 in forum New To JavaReplies: 1Last Post: 01-23-2009, 04:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks