Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating Multiple Objects?

 
Dante Hawke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

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.

Any help is most appreciated,

Many Thanks.


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: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) {}

Or a switch statement:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the input guys, my final 'working' code is:

 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks a lot better. Well done.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 =.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic