Win a copy of Terraform in Action this week in the Cloud forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How can I instantaite 10 objects at the same time?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this 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
reply
    Bookmark Topic Watch Topic
  • New Topic