Issue
I tried to implement this but no success. I dont know how to add the students created in SSO to the arraylist in Modules. I also dont know how to implement a enrollment/ unenrollment methode. Here is what my code looks like:
Modules class
package CIS2206.Practical_11;
import CIS2206.Practical_11.Student;
import CIS2206.Practical_11.StudentSupportOffice;
import CIS2206.Practical_11.Unit_11.university.Person;
public class Module {
String Module_ID; //Stores module id//
public Module(String Module_ID) {
this.Module_ID = Module_ID;
}
//an array of enrolled students with default capacity for 5 students), and the current size of the class list
Student[] Module_classlist = new Student[5];
int size = 0; //current size//
/*Enrollment method*/
public void Enrollment() {
Module_classlist[size] = new Student("U0000001");
size++;
Module_classlist[1] = Module_classlist[0];
Module_classlist[0] = new Student("U0000002");
size++;
Module_classlist[2] = Module_classlist[1];
Module_classlist[1] = new Student("U0000003");
size++;
Module_classlist[3] = Module_classlist[2];
Module_classlist[2] = new Student("U0000004");
size++;
Module_classlist[4] = Module_classlist[3];
Module_classlist[3] = new Student("U0000005");
size++;
}
/* Unenrollment method*/
public void Unenrollment() {
//Sequential search needed//
// create a new array with the size of the prev array - 1//
}
@Override
public String toString() {
return "Module = " + Module_ID + " contains in its class list:" ;
}
}
Student class
package CIS2206.Practical_11;
public class Student implements Comparable<Student> {
String Student_ID; //Stores student id//
/**
* This constructor initialises the fields of the class
*/
public Student(String Student_ID) {
this.Student_ID = Student_ID;
}
/**
* This method creates a String representation of
* the object in a human friendly fashion.
*
* @return A String representation of the person
*/
@Override
public String toString() { //Creates a human friendly representation of the class (i.e. toString())
return "\n Student_ID = " + Student_ID;
}
@Override
public int compareTo(Student S) { //Allows a total ordering of students (i.e. students are Comparable)
int ID = this.Student_ID.compareTo(S.Student_ID);
return ID;
}
}
StudentSupportOffice class
package CIS2206.Practical_11;
public class StudentSupportOffice {
public static void main(String[] args) {
Module One = new Module("CIS2206");
Module Two = new Module("CIS2360") ;
Module Three = new Module("CIS2205");
Student First = new Student("U0000001");
Student Second = new Student("U0000002");
Student Third = new Student("U0000003");
Student Fourth = new Student("U0000004");
Student Fifth = new Student("U0000005");
System.out.println(" ");
}
}
I am not looking for a full solution just hints
Solution
If you have a look at the requirements and the responsibilities of each of the classes.
Your Student
class looks fine - it just encapsulates student details - which is the Student_ID
In your Module
class - it encapsulates the Module's details (Module_ID
) and is responsible for enrolling and unenrolling (methods)
You would need to implement a method like public void enroll(Student student)
and public void unenroll(Student student)
passing in the students that need to be enrolled.
package CIS2206.Practical_11;
import java.util.ArrayList;
import java.util.List;
public class Module {
String Module_ID; //Stores module id//
public Module(String Module_ID) {
this.Module_ID = Module_ID;
}
List<Student> classList = new ArrayList<Student>();
public void enroll(Student student) {
// here you would need to check whether the size of the classList is over 5 and
// then not allow the student to enroll - see classList.contains() method
// and classList.size()
// add in your logic here - look at the List documentation: classList.add() method
}
public void unenroll(Student student) {
// add in your logic here - look at the List documentation: classList.remove() method
}
@Override
public String toString() {
String returnString = "Module = " + Module_ID + " contains in its class list:" ;
// look at the for (Student student : classList)
// and then add it to the returnString
// for extra points look at the StringBuffer class for a better way to do String concatenation
return(returnString);
}
}
Your StudentSupportOffice
class creates the modules and student - you now need to tie it all together and enroll the Student
in the Module
As the Module
is responsible for enroll
ing a student you need to call
module.enroll(student)
package CIS2206.Practical_11;
public class StudentSupportOffice {
public static void main(String[] args) {
Module One = new Module("CIS2206");
Module Two = new Module("CIS2360") ;
Module Three = new Module("CIS2205");
Student First = new Student("U0000001");
Student Second = new Student("U0000002");
Student Third = new Student("U0000003");
Student Fourth = new Student("U0000004");
Student Fifth = new Student("U0000005");
// enroll the students here
// print out the modules here
System.out.println(One.toString());
}
}
As a final side-note - have a search for java naming conventions
Answered By - synapticloop
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)