• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I instantaite 10 objects at the same time?

 
drew taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Student class that sets up random student attributes, and I have a ClassRoom class that contains an ArrayList to store student reference variables. Given that a set of 10 students will need to be created often and assigned to a classRoom, how can Instantaite 10 different student objects at the same time?

I tried making a while loop to loop 10 times but it only created the same object over and over and added it on top of the first one in the ArrayList in the ClassRoom class.

So how can I make 10 different objects without 10 separate object declarations that, when called will always replace the previously instatiated objects with the same reference variable names?
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go ahead and post your code, use [code] tags around it for readability.

There's no "built in" functionality to create random objects for you. You will have to either instantiate them as you have said, or write a service method to create "random" objects for you.
 
drew taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here is my code in the main class where I intstantiate a new student object: ( I have included the while loop I tried to make 10 objects and add them to an ArrayList in the ClassRoom class which doesn't work)


The other code I have are the constructors in the ClassRoom class and Student class.

What does making a service to create random objects involve?
 
Paul Clapham
Sheriff
Pie
Posts: 20971
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by drew taylor:
What does making a service to create random objects involve?
One important feature of such a service is that it creates objects. That sounds obvious, but your code doesn't create any new objects inside that while-loop. It simply adds the same Student object four times to the Tutor object.

Code that creates four new Student objects and adds them to a Tutor would look more like this:
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the more idiomatic way to code the loop would be using a for-loop in place of the while-loop.
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Student class that sets up random student attributes


Post your Student class. From this code, you are trying to add the SAME student to the class several times.

The code might look something like:



Then you'd have to write a static method in the Student class to "randomly" generate the attributes of a student.

It's fairly trivial to do this, I'll give you some pointers after you post your Student code.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Drew,

Have a good, line-by-line look at what your code does.

Student student = new Student();
Here you create a Student object.

Tutor tutor = new Tutor();
And here you create a Tutor object.

Here you have a loop where you add the same Student object to the Tutor object four times. You are not creating multiple Student objects; you are just calling addStudent(...) on the Tutor object four times with the same Student object.

You should put the "new Student()" line inside the loop, so that a new Student object is created at every iteration of the loop.

You'll also have to set the properties of the student to random values.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic