I need to make 100 'Customer' Objects that are each assigned a randomly decided on string as their parameter. - I can do this via a random number generator with each number referring to a different string.
e.g.
Possible strings: "Hello" or "Goodbye" or "Farewell".
Customer C1 = new Customer("Hello"); x 100
How can I guarantee that the reference variable to each new object will be different? As after I create 100 of them, I must call a method on each of them 100 times.
If you need to keep a reference to each object then you'll want to use an array or some sort of collection.
Dante Hawke
Greenhorn
Joined: Jan 27, 2009
Posts: 7
posted
0
Ah of course how silly of me. I now create an array and create each object with a reference variable and place that within each element. Does it matter that the reference variable is the same still though?
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
4
posted
0
You want an array which contains "Hello" "Goodbye" and "Farewell" and use the random number as the index to use those numbers in the Customer constructor.
i would also factor out the common code, and combine the three 'if''s into a single if-else. Note that if i ==1, it cannot == 2 or ==3. you'd be better off with
if (ranNum == 1) {}
else if (ranNum == 2) {}
else if (ranNum == 3) {}
further, it's redundant to have
customers[i] = Ci;
customers[i].queueUp(Q1);
three times. I'd factor it out to outside of your if-else entirely.
Never ascribe to malice that which can be adequately explained by stupidity.
Good point...I, personally, don't like switch statements, but have no good reason for my dislike. I can't say there is anything wrong with them...they're just not my style.
Dante Hawke
Greenhorn
Joined: Jan 27, 2009
Posts: 7
posted
0
Thanks for the input guys, my final 'working' code is:
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
4
posted
0
That looks a lot better. Well done.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
4
posted
0
But you would do well to get your spacing consistent. You have some binary operators with spaces and some without. That will lose you marks. Recommend: a binary operator is preceded and followed by single spaces. That includes =.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.