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?
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?
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:
Note that the more idiomatic way to code the loop would be using a for-loop in place of the while-loop.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
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.