aspose file tools*
The moose likes Beginning Java and the fly likes using Objects and Classes to Add Students to Courses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using Objects and Classes to Add Students to Courses" Watch "using Objects and Classes to Add Students to Courses" New topic
Author

using Objects and Classes to Add Students to Courses

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
I have a few questions pertaining to this assignment I have. Firstly, I'm having some trouble figuring out how to add a student to a course and keep an index of the courses the student is enrolled in. I also don't understand what exactly I am supposed to use the Student array for. The instructions said to add the setCapacity method but I don't know what that is supposed to be used for. We just started covering objects and creating classes and I feel so lost
If anyone could shed some light on these questions for me it would be greatly appreciated

Here is my assignment:
Part I: (15 pts) Create a Student class with the following:

A private String variable named “name” to store the student’s name
A private integer variable named “UFID” that contains the unique ID number for this student
A private String variable named “DOB” to store the student’s date of birth
A private integer class variable named numberOfStudents that keeps track of the number of students that have been created so far
A public constructor Person(String name, int UFID, String dob)
Several public get/set methods for all the properties
getName/setName
getUFID/setUFID
getDob/setDob

Part II: (15 pts) Create a Course class with the following:

A private String variable named “name” to store the course’s name, such as COP3502 etc.
A private array named “students” Student[] students.
A private integer variable “capacity” for the maximum number of students allowed in this class
A private integer variable “currentEnrollment” for the number of students enrolled in the course right now
A private integer class variable named numberOfCourses that keeps track of the number of courses that have been created so far
A public constructor Course(String n, int cap)
Several set/set methods for all the properties
getName/setName
getCap/setCap
etc.
A public method enrollStudent (Student s) to add s to this course and return true if the student was successfully added, or false if not
A public method removeStudent(Student s) to remove s from the students array

Part III: (20 pts) Create a test class to check whether above classes work properly. It should be able to create Course and Student objects, enroll student or drop student to these courses, printout current enrolled courses for a given student.


Here is the code I have so far:
Course class:


Student class:


Thank you so much!
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Hi Kd Martin,

In order to know which courses the student has enrolled to, you need to have something, ideally, an collection of Course objects in your student class.

Hope this helps.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Why have you got a number field in the student class? You do not appear to use it for anything. Are you supposed to use that field to set the ID number? If you, you would not want to pass the ID to the constructor.
Have you got any way of getting courses from the student class? The question about which course a student enrols for sounds like something out of a database assignment, where you get a relation between student and course and you can query it both ways. You can search through all the courses for a particular student, but that is hardly an elegant or efficient solution. You can try a bidirectional map, but that is probably too complicated for your level of experience.
You can add some way of recording courses inside the Student class, you can have students recorded in each course, and courses recorded in each student. You would have to set some sort of maximum, limiting how many courses each student may take at once. You need a method for each course which records a student, and a method for each student which records courses. Those methods mustn’t be static. If you have a means of recording students, you must be able to add a student to the array, and also remove a student. Adding is quite easy, removing from an array slightly more difficult. If you go into your Java™ installation folder, you find a file called src.zip. Unzip that, go into the java folder, the util folder, and the ArrayList.java file, and you can see how it is done there.
What happens when you change the capacity of the course? If you increase capacity, are you creating a new array and copying the students from the old array? If you use an array, you don’t need a capacity field; you can use the length field of the array. Otherwise you are storing the same datum in two places and there is a risk of getting the two values different from each other. What happens if you reduce the capacity, say from 20 to 15, after 16 students have already enrolled? You should consider what I have struck through and discuss it with your teacher.
Do you really want to change the ID in a student object?
How are you going to print out the details of a student or a course? Have you got toString() methods ready to write? I would remind you not to use the + operator on Strings in more than one line; use a StringBuilder instead.

I have made lots of suggestions. Lots and lots. You will have to implement them. Take it easy and don’t let a 2-inch block of solid text on screen scare you. You implement those suggestions one at a time, not writing more than 5 lines of code before you compile and run it. You need a class with some sort of method which creates Student objects, prints their details, changes their details, enrols them on courses, etc., etc. You build up this method one line at a time, running it frequently . . . and when you finish that, you have got half your “testing class” ready-made
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Great thank you!! Also, how do I go about passing a student object I created to the array created in the course class?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Same way you would do it for any other methodObviously you have to ensure that your object has the appropriately named method, which takes an argument of the type of the elements of that array, that the array actually has a member No 123, etc etc.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
I ended up figuring it out!! Thanks so much!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using Objects and Classes to Add Students to Courses