import school.*;
import school.courses.*;
public class Main {
public static void main(String[] args) {
Teacher phil = new Teacher(\"Phil\");
Teacher bill = new Teacher(\"Bill\");
Teacher lil = new Teacher(\"Lil\");
Teacher joe = new Teacher(\"Joe\");
Course[] courses = {
new NetworkCourse(15, phil),
new SwingCourse(30, bill),
new APIDesignCourse(50, lil),
new PerformanceCourse(5, joe)
};
School school = new School(courses);
Student ludwig = new Student(\"Ludwig\");
Student cam = new Student(\"Cam\");
Student daniel = new Student(\"Daniel\");
ludwig.setPreferredCourses(NetworkCourse.class, SwingCourse.class); //give students preferred
classes if they have them
cam.setPreferredCourses(APIDesignCourse.class, PerformanceCourse.class,
NetworkCourse.class);
school.register(ludwig, cam, daniel);
/*
* Below is where we test by printing things out
*/
test(school);
}
static void test(School school) {
/*
* Prints all the students in the school, all the courses in the school, and which course each
student has
*/
System.out.println(\"Students and their courses:\");
for(Student student : school.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 : school.getCourses()) {
String message = course.getName() + \" is taken by\";
for(Student student : course.getStudents()) {
if(student != null)
message += \" - \" + student.getName();
}
System.out.println(message);
}
}
}
School.java
package school;
import java.util.*;
public class School {
private Course[] courses;
private Student[] students;
public School(Course[] courses) {
this.courses = courses;
//ive been told that constructors shouldnt contain logic. is there any other way to handle this?
int numOfStudents = 0;
for(Course course : courses)
numOfStudents += course.getStudents().length;
students = new Student[numOfStudents];
}
public void register(Student...students) { //this method is pretty messy, and loops quite a few
times. any suggestions?
if(isFull())
throw new IllegalStateException(\"Cannot register anymore students at this time\");
for(Student student : students) {
if(Arrays.asList(this.students).contains(student)) //wrapping the array every loop. is there any
better way to do this, without creating my own private contains method for students?
throw new IllegalArgumentException(\"You cannot add the same student to a school twice\");
//should I be throwing a runtime exception here? or should i just continue with the rest of the
students
for(Course course : courses) {
if(student.prefersCourse(course) && !course.isFull())
student.assignCourse(course);
}
verifyStudent(student); //make sure the student is ready for school
student.setSchool(this);
for(int i = 0; i < this.students.length; i++) {
if(this.students[i].
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
import school.; import school.courses.;public class Main { p.pdf
1. import school.*;
import school.courses.*;
public class Main {
public static void main(String[] args) {
Teacher phil = new Teacher("Phil");
Teacher bill = new Teacher("Bill");
Teacher lil = new Teacher("Lil");
Teacher joe = new Teacher("Joe");
Course[] courses = {
new NetworkCourse(15, phil),
new SwingCourse(30, bill),
new APIDesignCourse(50, lil),
new PerformanceCourse(5, joe)
};
School school = new School(courses);
Student ludwig = new Student("Ludwig");
Student cam = new Student("Cam");
Student daniel = new Student("Daniel");
ludwig.setPreferredCourses(NetworkCourse.class, SwingCourse.class); //give students preferred
classes if they have them
cam.setPreferredCourses(APIDesignCourse.class, PerformanceCourse.class,
NetworkCourse.class);
school.register(ludwig, cam, daniel);
/*
* Below is where we test by printing things out
*/
test(school);
}
static void test(School school) {
/*
* Prints all the students in the school, all the courses in the school, and which course each
student has
*/
System.out.println("Students and their courses:");
for(Student student : school.getStudents()) {
2. 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 : school.getCourses()) {
String message = course.getName() + " is taken by";
for(Student student : course.getStudents()) {
if(student != null)
message += " - " + student.getName();
}
System.out.println(message);
}
}
}
School.java
package school;
import java.util.*;
public class School {
private Course[] courses;
private Student[] students;
public School(Course[] courses) {
this.courses = courses;
//ive been told that constructors shouldnt contain logic. is there any other way to handle this?
int numOfStudents = 0;
for(Course course : courses)
numOfStudents += course.getStudents().length;
students = new Student[numOfStudents];
}
public void register(Student...students) { //this method is pretty messy, and loops quite a few
times. any suggestions?
if(isFull())
3. throw new IllegalStateException("Cannot register anymore students at this time");
for(Student student : students) {
if(Arrays.asList(this.students).contains(student)) //wrapping the array every loop. is there any
better way to do this, without creating my own private contains method for students?
throw new IllegalArgumentException("You cannot add the same student to a school twice");
//should I be throwing a runtime exception here? or should i just continue with the rest of the
students
for(Course course : courses) {
if(student.prefersCourse(course) && !course.isFull())
student.assignCourse(course);
}
verifyStudent(student); //make sure the student is ready for school
student.setSchool(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);
//more will be added here later
}
private void verifyCourses(Student student) {
boolean verified = false;
//assigns a random course. is there a cleaner way to handle this?
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);
4. }
}
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 school;
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;
}
5. }
}
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 school;
import java.util.*;
public class Student extends Entity {
private School school;
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])
6. continue;
if(courses[i] == null) {
course.addStudent(this);
courses[i] = course;
return;
}
}
}
void setSchool(School school) {
this.school = school;
}
public School getSchool() {
return school;
}
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 school;
public class Teacher extends Entity {
public Teacher(String name) {
super(name);
}
}
Entity.java
package school;
7. public abstract class Entity {
private String name;
protected Entity(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
APIDesignCourse.java (the Course subclasses are similar)
package school.courses;
import school.*;
public class APIDesignCourse extends Course {
public APIDesignCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s API Design Course";
}
}
NetworkCourse.java
package school.courses;
import school.*;
public class NetworkCourse extends Course {
public NetworkCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s Network Course";
}
}
PerformanceCourse.java
package school.courses;
import school.*;
public class PerformanceCourse extends Course {
public PerformanceCourse(int numOfStudents, Teacher teacher) {
8. super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s Performance Course";
}
}
SwingCourse.java
package school.courses;
import school.*;
public class SwingCourse extends Course {
public SwingCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s Swing Course";
}
}
Solution
import school.*;
import school.courses.*;
public class Main {
public static void main(String[] args) {
Teacher phil = new Teacher("Phil");
Teacher bill = new Teacher("Bill");
Teacher lil = new Teacher("Lil");
Teacher joe = new Teacher("Joe");
Course[] courses = {
new NetworkCourse(15, phil),
new SwingCourse(30, bill),
new APIDesignCourse(50, lil),
new PerformanceCourse(5, joe)
};
School school = new School(courses);
Student ludwig = new Student("Ludwig");
9. Student cam = new Student("Cam");
Student daniel = new Student("Daniel");
ludwig.setPreferredCourses(NetworkCourse.class, SwingCourse.class); //give students preferred
classes if they have them
cam.setPreferredCourses(APIDesignCourse.class, PerformanceCourse.class,
NetworkCourse.class);
school.register(ludwig, cam, daniel);
/*
* Below is where we test by printing things out
*/
test(school);
}
static void test(School school) {
/*
* Prints all the students in the school, all the courses in the school, and which course each
student has
*/
System.out.println("Students and their courses:");
for(Student student : school.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 : school.getCourses()) {
String message = course.getName() + " is taken by";
for(Student student : course.getStudents()) {
if(student != null)
message += " - " + student.getName();
}
System.out.println(message);
}
10. }
}
School.java
package school;
import java.util.*;
public class School {
private Course[] courses;
private Student[] students;
public School(Course[] courses) {
this.courses = courses;
//ive been told that constructors shouldnt contain logic. is there any other way to handle this?
int numOfStudents = 0;
for(Course course : courses)
numOfStudents += course.getStudents().length;
students = new Student[numOfStudents];
}
public void register(Student...students) { //this method is pretty messy, and loops quite a few
times. any suggestions?
if(isFull())
throw new IllegalStateException("Cannot register anymore students at this time");
for(Student student : students) {
if(Arrays.asList(this.students).contains(student)) //wrapping the array every loop. is there any
better way to do this, without creating my own private contains method for students?
throw new IllegalArgumentException("You cannot add the same student to a school twice");
//should I be throwing a runtime exception here? or should i just continue with the rest of the
students
for(Course course : courses) {
if(student.prefersCourse(course) && !course.isFull())
student.assignCourse(course);
}
verifyStudent(student); //make sure the student is ready for school
student.setSchool(this);
for(int i = 0; i < this.students.length; i++) {
if(this.students[i] == null) {
this.students[i] = student;
break;
11. }
}
}
}
private void verifyStudent(Student student) {
verifyCourses(student);
//more will be added here later
}
private void verifyCourses(Student student) {
boolean verified = false;
//assigns a random course. is there a cleaner way to handle this?
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;
for(Student student : students)
if(student == null)
return full = false;
return full;
}
}
Course.java
12. package school;
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;
for(Student student : students)
full = student != null;
return full;
}
public abstract String getName();
}
Student.java
13. package school;
import java.util.*;
public class Student extends Entity {
private School school;
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 setSchool(School school) {
this.school = school;
}
public School getSchool() {
return school;
}
public Course[] getCourses() {
return Arrays.copyOf(courses, courses.length);
}
14. 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 school;
public class Teacher extends Entity {
public Teacher(String name) {
super(name);
}
}
Entity.java
package school;
public abstract class Entity {
private String name;
protected Entity(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
APIDesignCourse.java (the Course subclasses are similar)
package school.courses;
import school.*;
public class APIDesignCourse extends Course {
public APIDesignCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
15. return getTeacher().getName() + "'s API Design Course";
}
}
NetworkCourse.java
package school.courses;
import school.*;
public class NetworkCourse extends Course {
public NetworkCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s Network Course";
}
}
PerformanceCourse.java
package school.courses;
import school.*;
public class PerformanceCourse extends Course {
public PerformanceCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s Performance Course";
}
}
SwingCourse.java
package school.courses;
import school.*;
public class SwingCourse extends Course {
public SwingCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s Swing Course";
}
}