SlideShare a Scribd company logo
1 of 15
Download to read offline
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] == 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);
}
}
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;
}
}
}
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])
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;
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) {
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");
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] == 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);
}
}
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;
}
}
}
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])
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;
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) {
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";
}
}

More Related Content

Similar to import school.; import school.courses.;public class Main { p.pdf

Cs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solutionCs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solution
Fahadaio
 
Java questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdfJava questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdf
forwardcom41
 
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdfTeacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
kareemangels
 
Answer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfAnswer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdf
suresh640714
 
C# Delegates, Events, Lambda
C# Delegates, Events, LambdaC# Delegates, Events, Lambda
C# Delegates, Events, Lambda
Jussi Pohjolainen
 
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
anupambedcovers
 
29. Code an application program that keeps track of student informat.pdf
29. Code an application program that keeps track of student informat.pdf29. Code an application program that keeps track of student informat.pdf
29. Code an application program that keeps track of student informat.pdf
arishaenterprises12
 
Java PSkills Session-6 PNR.pptx
Java PSkills Session-6 PNR.pptxJava PSkills Session-6 PNR.pptx
Java PSkills Session-6 PNR.pptx
ssuser99ca78
 

Similar to import school.; import school.courses.;public class Main { p.pdf (20)

Cs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solutionCs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solution
 
Java questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdfJava questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdf
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
 
Jar chapter 5_part_i
Jar chapter 5_part_iJar chapter 5_part_i
Jar chapter 5_part_i
 
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdfTeacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
 
Answer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfAnswer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdf
 
C# Delegates, Events, Lambda
C# Delegates, Events, LambdaC# Delegates, Events, Lambda
C# Delegates, Events, Lambda
 
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
 
Static keyword.pptx
Static keyword.pptxStatic keyword.pptx
Static keyword.pptx
 
29. Code an application program that keeps track of student informat.pdf
29. Code an application program that keeps track of student informat.pdf29. Code an application program that keeps track of student informat.pdf
29. Code an application program that keeps track of student informat.pdf
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
Unit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptxUnit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptx
 
Java PSkills Session-6 PNR.pptx
Java PSkills Session-6 PNR.pptxJava PSkills Session-6 PNR.pptx
Java PSkills Session-6 PNR.pptx
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
WDD_lec_06.ppt
WDD_lec_06.pptWDD_lec_06.ppt
WDD_lec_06.ppt
 

More from annaiwatertreatment

Hi!For the first reaction, Iron (Fe) is going from a positive oxid.pdf
Hi!For the first reaction, Iron (Fe) is going from a positive oxid.pdfHi!For the first reaction, Iron (Fe) is going from a positive oxid.pdf
Hi!For the first reaction, Iron (Fe) is going from a positive oxid.pdf
annaiwatertreatment
 
If we design any page or devoleped any code we use the word shield a.pdf
If we design any page or devoleped any code we use the word shield a.pdfIf we design any page or devoleped any code we use the word shield a.pdf
If we design any page or devoleped any code we use the word shield a.pdf
annaiwatertreatment
 

More from annaiwatertreatment (20)

1.i. Oxygen is the final electron acceptor for the electron transpor.pdf
1.i. Oxygen is the final electron acceptor for the electron transpor.pdf1.i. Oxygen is the final electron acceptor for the electron transpor.pdf
1.i. Oxygen is the final electron acceptor for the electron transpor.pdf
 
The transverse electric and transverse magnetic (TEM) wave traveling.pdf
The transverse electric and transverse magnetic (TEM) wave traveling.pdfThe transverse electric and transverse magnetic (TEM) wave traveling.pdf
The transverse electric and transverse magnetic (TEM) wave traveling.pdf
 
The fraction for of .15 = 32000 I convereted .15 to a decimal whi.pdf
The fraction for of .15 = 32000 I convereted .15 to a decimal whi.pdfThe fraction for of .15 = 32000 I convereted .15 to a decimal whi.pdf
The fraction for of .15 = 32000 I convereted .15 to a decimal whi.pdf
 
Solution You sound a tone and then squirt pickle juice in a child.pdf
Solution You sound a tone and then squirt pickle juice in a child.pdfSolution You sound a tone and then squirt pickle juice in a child.pdf
Solution You sound a tone and then squirt pickle juice in a child.pdf
 
Anisole is a weakly polar ether compound.Acetone is a polar ketone.pdf
Anisole is a weakly polar ether compound.Acetone is a polar ketone.pdfAnisole is a weakly polar ether compound.Acetone is a polar ketone.pdf
Anisole is a weakly polar ether compound.Acetone is a polar ketone.pdf
 
No , its located towards cytoplasmic side. Its used as precursor.pdf
No , its located towards cytoplasmic side. Its used as precursor.pdfNo , its located towards cytoplasmic side. Its used as precursor.pdf
No , its located towards cytoplasmic side. Its used as precursor.pdf
 
(32 , 132)Solution(32 , 132).pdf
(32 , 132)Solution(32 , 132).pdf(32 , 132)Solution(32 , 132).pdf
(32 , 132)Solution(32 , 132).pdf
 
HI,The image is not clear,but i tried my best to decode it.As far .pdf
HI,The image is not clear,but i tried my best to decode it.As far .pdfHI,The image is not clear,but i tried my best to decode it.As far .pdf
HI,The image is not clear,but i tried my best to decode it.As far .pdf
 
Let A,B and C be the events that coin of each type respectively were.pdf
Let A,B and C be the events that coin of each type respectively were.pdfLet A,B and C be the events that coin of each type respectively were.pdf
Let A,B and C be the events that coin of each type respectively were.pdf
 
International Portfoilo investment is affected by following few fact.pdf
International Portfoilo investment is affected by following few fact.pdfInternational Portfoilo investment is affected by following few fact.pdf
International Portfoilo investment is affected by following few fact.pdf
 
Hi!For the first reaction, Iron (Fe) is going from a positive oxid.pdf
Hi!For the first reaction, Iron (Fe) is going from a positive oxid.pdfHi!For the first reaction, Iron (Fe) is going from a positive oxid.pdf
Hi!For the first reaction, Iron (Fe) is going from a positive oxid.pdf
 
If we design any page or devoleped any code we use the word shield a.pdf
If we design any page or devoleped any code we use the word shield a.pdfIf we design any page or devoleped any code we use the word shield a.pdf
If we design any page or devoleped any code we use the word shield a.pdf
 
Det M of 2x2 matrix can be any value from -infty to +inftyE) B1+B2.pdf
Det M of 2x2 matrix can be any value from -infty to +inftyE) B1+B2.pdfDet M of 2x2 matrix can be any value from -infty to +inftyE) B1+B2.pdf
Det M of 2x2 matrix can be any value from -infty to +inftyE) B1+B2.pdf
 
Aspirin is acetylsalicylic acid. As the compound ages, small amounts.pdf
Aspirin is acetylsalicylic acid. As the compound ages, small amounts.pdfAspirin is acetylsalicylic acid. As the compound ages, small amounts.pdf
Aspirin is acetylsalicylic acid. As the compound ages, small amounts.pdf
 
referenceSolution reference.pdf
 referenceSolution reference.pdf referenceSolution reference.pdf
referenceSolution reference.pdf
 
B. The solution has more hydroxide ions than hydronium ions. .pdf
  B. The solution has more hydroxide ions than hydronium ions.        .pdf  B. The solution has more hydroxide ions than hydronium ions.        .pdf
B. The solution has more hydroxide ions than hydronium ions. .pdf
 
they are the atomic number of those element so fr.pdf
                     they are the atomic number of those element so fr.pdf                     they are the atomic number of those element so fr.pdf
they are the atomic number of those element so fr.pdf
 
the bond between O and F .pdf
                     the bond between O and F                         .pdf                     the bond between O and F                         .pdf
the bond between O and F .pdf
 
s(s+1.7x10-3)=2.1x10-10 s=1.24x10^-7 .pdf
                     s(s+1.7x10-3)=2.1x10-10 s=1.24x10^-7            .pdf                     s(s+1.7x10-3)=2.1x10-10 s=1.24x10^-7            .pdf
s(s+1.7x10-3)=2.1x10-10 s=1.24x10^-7 .pdf
 
Option II is correct ; in other 4 cases there is .pdf
                     Option II is correct ; in other 4 cases there is .pdf                     Option II is correct ; in other 4 cases there is .pdf
Option II is correct ; in other 4 cases there is .pdf
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 

Recently uploaded (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
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"; } }