• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Creating Multiple Objects?

 
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.


 
Author
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?

 
Marshal
Posts: 70223
282
  • 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.
 
lowercase baba
Posts: 12893
63
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.
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
Posts: 12893
63
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
Marshal
Posts: 70223
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks a lot better. Well done.
 
Campbell Ritchie
Marshal
Posts: 70223
282
  • 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 =.
 
Bring out your dead! Or a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic