File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Passing Objects Created By User to Array in Different Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

Passing Objects Created By User to Array in Different Class

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
I have to create two classes: Student and Course. The purpose of the program is to have the user create students and courses and add students and drop them from courses. I was told by my professor to create an array called "Student" in the course class. What I'm having a problem understanding is how to pass the student object created by the user in the class "EnrollStudent" to "Course" to fill the array "Student". If you could help me understand this I would really appreciate it! Thanks!



Enroll Student:
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
First I assume you are getting at least one compilation error for your Course class. The addStudent method should not be static.
Once you've fixed that you will need to call that method to add a student to the course.

The first thing you need to do is find out which course the student is to be enrolled on. Lets assume for the moment it's only going to be one course - we can expand it later to support multiple courses.
Probably the easiest way to do this is to display a numbered list of courses and ask the user to enter the number of the course.
Give that a go and see how far you get. You'll need a for loop to display the list and it needs to go after line 39 where you create the Student object.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Sorry, I forgot to welcome you to the Ranch. Welcome, now

This question looks so similar to your question from yesterday that I shall close this thread; please continue discussion there (even if I did scare you with a long reply there).
I agree. Here's the link:
subject: Passing Objects Created By User to Array in Different Class
It's not a secret anymore!