6 Replies - 6326 Views - Last Post: 28 November 2011 - 02:57 AM Rate Topic: -----

#1 kikib92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 26-September 11

Using array of objects to add students to courses

Posted 24 November 2011 - 10:12 AM

I'm having some troube figuring out how to add a student to a course. Here is my assignment:
If anyone could shed some light on this for me it would be greatly appreciated :)

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 what I have so far:
Student class:
public class Student {
	private String name; 
	private int UFID;
	private String DOB;
	private static int number;
	
	//Constructors
	Student(String name, int UFID, String DOB) {
		this.name = name;
		this.UFID = UFID;
		this.DOB = DOB;
	}
	
	//Get and set methods for all properties
	String getName() {
		return name;
	}
	public void setName(String newName) {
		name = newName;
	}
	int getUFID() {
		return UFID;
	}
	public void setUFID(int newUFID) {
		UFID = newUFID;
	}
	String getDOB() {
		return DOB;
	}
	public void setDOB(String newDOB) {
		DOB = newDOB;
	}
}




Course class:
public class Course {
	private String name; //Name of course
	private Student[] students = new Student[5];
	private int capacity; //Number of students allowed in course
	private static int currentEnrollment; //number of students enrolled
	private static int numberOfCourses; //Number of courses created so far
	
	//Constructor
	Course(String n, int cap) {
		name = n;
		capacity = cap;
	 }
	
	//Get and set methods
	String getName() {
		return name;
	}
	public void setName(String newName) {
		name = newName;
	}
	int getCapacity() {
		return capacity;
	}
	public void setCapacity(int newCapacity) {
		capacity = newCapacity;
	}
	
	//Method to add a student (s) to the course and return true if added and false if not
	public static boolean addStudent(Student s) {
		if (capacity - currentEnrollment > 0) { //See if class has room
			
			return true;
		}
		else {
			return false;
		}
	
	//Method to remove student from a class
	


This post has been edited by smohd: 24 November 2011 - 10:55 AM
Reason for edit:: title fixed


Is This A Good Question/Topic? 0
  • +

Replies To: Using array of objects to add students to courses

#2 kikib92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 26-September 11

Re: Using array of objects to add students to courses

Posted 24 November 2011 - 10:18 AM

Whoops...typo. The title should read "ADD"
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Using array of objects to add students to courses

Posted 24 November 2011 - 11:29 AM

Few things with your Course class:
- First the size of your students array should be the capacity entered, so you dont have to hard code it. Thi means you have to initialize your array in a constructor after getting a capacity;

- Your addStudent() does not really add a student, it just check. So what you have to do is if there is a space, then add a student and increase the current enrollment.

- Also the check is wrong, you should have check if the current enrollment exceeds the capacity or not

- this means currentEnrollment has not to be static, because every course has its own current enrollment

- Also if you are planning to change the capacity, then you have to update the array also, otherwise remove setCapacity() which means after course has been created with a capacity, it cant be changed
Was This Post Helpful? 3
  • +
  • -

#4 kikib92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 26-September 11

Re: Using array of objects to add students to courses

Posted 25 November 2011 - 04:26 PM

View Postsmohd, on 24 November 2011 - 11:29 AM, said:

- Also if you are planning to change the capacity, then you have to update the array also, otherwise remove setCapacity() which means after course has been created with a capacity, it cant be changed



I'm not quite sure what the set capacity is for. The instructions said to add it though. So how would I go about updating the array??

Also, I;m still not quite sure how to add a student to a course because I should be able to printout current enrolled courses for a given student. :/
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Using array of objects to add students to courses

Posted 25 November 2011 - 04:42 PM

It is jsut simple, a student passed to a method, if there is a space in your array, add him to the index available and increment the currentEnrollment( the one which you will use as the index to add, and also if the course is full or not
Was This Post Helpful? 2
  • +
  • -

#6 kikib92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 26-September 11

Re: Using array of objects to add students to courses

Posted 27 November 2011 - 10:30 PM

What is the student array intended to hold? I'm sorry, I feel really dumb but I just don't understand OOP at all :(
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Using array of objects to add students to courses

Posted 28 November 2011 - 02:57 AM

Quote

What is the student array intended to hold?

From what you defined, it should hold Students
private Student[] students = new Student[5];

-It holds students in a certain Course
-This means its size will depend on the capacity of students in a course.
-That is it should not be always of size 5, its size depends on the capacity:

And so, you will have something like:
public class Course {
     //other instance variables;
     private Student[] students;

     public Course(String n, int cap) {
	name = n;
	capacity = cap;
        students = new Student[capacity]; //give the array size according to the capacity of the class
    }


This will mean every object of a Course(every course) will have its capacity, and so adding students must consider the size of the course you are adding. This is why I said currentEnrollment must not be static, a static instance variable means it will be shared by all objects of the class, and updating it the change affects all objects....(unless if you need it to count students in all courses)

This post has been edited by smohd: 28 November 2011 - 03:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1