The document defines classes for a college registration system including College, Course, Student, and Teacher classes. It tests registering students to courses, ensuring preferences and availability are checked, and outputs the enrolled students for each course and courses for each student. Main creates sample data and calls registration and output methods.
Starting with Main.java, where I tested everythingimport College..pdf
1. Starting with Main.java, where I tested everything:
import College.*;
import College.courses.*;
public class Main
{
public static void main(String[] args)
{
Teacher willson = new Teacher("Willson");
Teacher modi = new Teacher("Modi");
Teacher lil = new Teacher("Lil");
Teacher jorge = new Teacher("Jorge");
Course[] courses = {
new NetworkCourse(15, willson),
new SwingCourse(30, modi),
new APIDesignCourse(50, lil),
new PerformanceCourse(5, jorge)
};
College College = new College(courses);
Student jhon = new Student("Jhon");
Student devid = new Student("Devid");
Student daniel = new Student("Daniel");
jhon.setPreferredCourses(NetworkCourse.class, SwingCourse.class);
devid.setPreferredCourses(APIDesignCourse.class, PerformanceCourse.class,
NetworkCourse.class);
College.register(jhon, devid, daniel);
test(College);
}
static void test(College College) {
System.out.println("Students and their courses:");
for(Student student : College.getStudents()) {
if(student != null) {
String message = student.getName() + " is taking"; //message will reset for each new
student, since we do = and not += here
for(Course course : student.getCourses())
message += " - " + course.getName();
2. System.out.println(message);
}
}
System.out.println(" Courses and their students:");
for(Course course : College.getCourses()) {
String message = course.getName() + " is taken by";
for(Student student : course.getStudents()) {
if(student != null)
message += " - " + student.getName();
}
System.out.println(message);
}
}
}
College.java
package College;
import java.util.*;
public class College {
private Course[] courses;
private Student[] students;
public College(Course[] courses) {
this.courses = courses;
int numOfStudents = 0;
for(Course course : courses)
numOfStudents += course.getStudents().length;
students = new Student[numOfStudents];
}
public void register(Student...students)
{
if(isFull())
throw new IllegalStateException("Cannot register anymore students at this time");
for(Student student : students) {
if(Arrays.asList(this.students).contains(student))
throw new IllegalArgumentException("You cannot add the same student to a College
twice");
for(Course course : courses) {
3. if(student.prefersCourse(course) && !course.isFull())
student.assignCourse(course);
}
verifyStudent(student); //make sure the student is ready for College
student.setCollege(this);
for(int i = 0; i < this.students.length; i++) {
if(this.students[i] == null) {
this.students[i] = student;
break;
}
}
}
}
private void verifyStudent(Student student) {
verifyCourses(student);
}
private void verifyCourses(Student student) {
boolean verified = false;
while(!verified) {
for(Course course : student.getCourses()) {
if(course == null) {
int index = (int) (Math.random() * courses.length);
student.assignCourse(courses[index]);
}
}
verified = !Arrays.asList(student.getCourses()).contains(null);
}
}
public Student[] getStudents() {
return Arrays.copyOf(students, students.length);
}
public Course[] getCourses() {
return Arrays.copyOf(courses, courses.length);
}
public boolean isFull() {
boolean full = true;
4. for(Student student : students)
if(student == null)
return full = false;
return full;
}
}
Course.java
package College;
import java.util.*;
public abstract class Course {
private Teacher teacher;
private Student[] students;
private UUID id;
protected Course(int maxStudents, Teacher teacher) { //might allow multiple teachers later
students = new Student[maxStudents];
this.teacher = teacher;
id = UUID.randomUUID();
}
void addStudent(Student student) {
for(int i = 0; i < students.length; i++) {
if(student == students[i])
continue;
if(students[i] == null) {
students[i] = student;
return;
}
}
}
public Teacher getTeacher() {
return teacher;
}
public Student[] getStudents() {
return Arrays.copyOf(students, students.length);
}
public boolean isFull() {
boolean full = true;
5. for(Student student : students)
full = student != null;
return full;
}
public abstract String getName();
}
Student.java
package College;
import java.util.*;
public class Student extends Entity {
private College College;
private Course[] courses;
private Set> preferredCourses;
public Student(String name) {
super(name);
courses = new Course[2];
preferredCourses = new HashSet<>();
}
public void setPreferredCourses(Class...courses) {
for(Class course : courses) {
preferredCourses.add(course);
}
}
void assignCourse(Course course) {
for(int i = 0; i < courses.length; i++) {
if(course == courses[i])
continue;
if(courses[i] == null) {
course.addStudent(this);
courses[i] = course;
return;
}
}
}
void setCollege(College College) {
this.College = College;
6. }
public College getCollege() {
return College;
}
public Course[] getCourses() {
return Arrays.copyOf(courses, courses.length);
}
public boolean prefersCourse(Course course) {
return preferredCourses.contains(course.getClass());
}
public boolean isTakingCourse(Course course) {
boolean contains = false;
for(Course c : courses)
return contains = (c == course);
return contains;
}
}
Teacher.java
package College;
public class Teacher extends Entity {
public Teacher(String name) {
super(name);
}
}
Entity.java
package College;
public abstract class Entity {
private String name;
protected Entity(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
APIDesignCourse.java
7. package College.courses;
import College.*;
public class APIDesignCourse extends Course {
public APIDesignCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s API Design Course";
}
}
Solution
Starting with Main.java, where I tested everything:
import College.*;
import College.courses.*;
public class Main
{
public static void main(String[] args)
{
Teacher willson = new Teacher("Willson");
Teacher modi = new Teacher("Modi");
Teacher lil = new Teacher("Lil");
Teacher jorge = new Teacher("Jorge");
Course[] courses = {
new NetworkCourse(15, willson),
new SwingCourse(30, modi),
new APIDesignCourse(50, lil),
new PerformanceCourse(5, jorge)
};
College College = new College(courses);
Student jhon = new Student("Jhon");
Student devid = new Student("Devid");
Student daniel = new Student("Daniel");
jhon.setPreferredCourses(NetworkCourse.class, SwingCourse.class);
devid.setPreferredCourses(APIDesignCourse.class, PerformanceCourse.class,
8. NetworkCourse.class);
College.register(jhon, devid, daniel);
test(College);
}
static void test(College College) {
System.out.println("Students and their courses:");
for(Student student : College.getStudents()) {
if(student != null) {
String message = student.getName() + " is taking"; //message will reset for each new
student, since we do = and not += here
for(Course course : student.getCourses())
message += " - " + course.getName();
System.out.println(message);
}
}
System.out.println(" Courses and their students:");
for(Course course : College.getCourses()) {
String message = course.getName() + " is taken by";
for(Student student : course.getStudents()) {
if(student != null)
message += " - " + student.getName();
}
System.out.println(message);
}
}
}
College.java
package College;
import java.util.*;
public class College {
private Course[] courses;
private Student[] students;
public College(Course[] courses) {
this.courses = courses;
int numOfStudents = 0;
for(Course course : courses)
9. numOfStudents += course.getStudents().length;
students = new Student[numOfStudents];
}
public void register(Student...students)
{
if(isFull())
throw new IllegalStateException("Cannot register anymore students at this time");
for(Student student : students) {
if(Arrays.asList(this.students).contains(student))
throw new IllegalArgumentException("You cannot add the same student to a College
twice");
for(Course course : courses) {
if(student.prefersCourse(course) && !course.isFull())
student.assignCourse(course);
}
verifyStudent(student); //make sure the student is ready for College
student.setCollege(this);
for(int i = 0; i < this.students.length; i++) {
if(this.students[i] == null) {
this.students[i] = student;
break;
}
}
}
}
private void verifyStudent(Student student) {
verifyCourses(student);
}
private void verifyCourses(Student student) {
boolean verified = false;
while(!verified) {
for(Course course : student.getCourses()) {
if(course == null) {
int index = (int) (Math.random() * courses.length);
student.assignCourse(courses[index]);
}
10. }
verified = !Arrays.asList(student.getCourses()).contains(null);
}
}
public Student[] getStudents() {
return Arrays.copyOf(students, students.length);
}
public Course[] getCourses() {
return Arrays.copyOf(courses, courses.length);
}
public boolean isFull() {
boolean full = true;
for(Student student : students)
if(student == null)
return full = false;
return full;
}
}
Course.java
package College;
import java.util.*;
public abstract class Course {
private Teacher teacher;
private Student[] students;
private UUID id;
protected Course(int maxStudents, Teacher teacher) { //might allow multiple teachers later
students = new Student[maxStudents];
this.teacher = teacher;
id = UUID.randomUUID();
}
void addStudent(Student student) {
for(int i = 0; i < students.length; i++) {
if(student == students[i])
continue;
if(students[i] == null) {
students[i] = student;
11. return;
}
}
}
public Teacher getTeacher() {
return teacher;
}
public Student[] getStudents() {
return Arrays.copyOf(students, students.length);
}
public boolean isFull() {
boolean full = true;
for(Student student : students)
full = student != null;
return full;
}
public abstract String getName();
}
Student.java
package College;
import java.util.*;
public class Student extends Entity {
private College College;
private Course[] courses;
private Set> preferredCourses;
public Student(String name) {
super(name);
courses = new Course[2];
preferredCourses = new HashSet<>();
}
public void setPreferredCourses(Class...courses) {
for(Class course : courses) {
preferredCourses.add(course);
}
}
void assignCourse(Course course) {
12. for(int i = 0; i < courses.length; i++) {
if(course == courses[i])
continue;
if(courses[i] == null) {
course.addStudent(this);
courses[i] = course;
return;
}
}
}
void setCollege(College College) {
this.College = College;
}
public College getCollege() {
return College;
}
public Course[] getCourses() {
return Arrays.copyOf(courses, courses.length);
}
public boolean prefersCourse(Course course) {
return preferredCourses.contains(course.getClass());
}
public boolean isTakingCourse(Course course) {
boolean contains = false;
for(Course c : courses)
return contains = (c == course);
return contains;
}
}
Teacher.java
package College;
public class Teacher extends Entity {
public Teacher(String name) {
super(name);
}
}
13. Entity.java
package College;
public abstract class Entity {
private String name;
protected Entity(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
APIDesignCourse.java
package College.courses;
import College.*;
public class APIDesignCourse extends Course {
public APIDesignCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s API Design Course";
}
}