This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

Passing Objects Created By User to Array in Different Class

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
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!

Student:


Course:


Enroll Student:
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3606
    
  15
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.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
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).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Passing Objects Created By User to Array in Different Class