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

More Related Content

Similar to Starting with Main.java, where I tested everythingimport College..pdf

Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo....NET Conf UY
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP Zaenal Arifin
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptRithwikRanjan
 
public class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfpublic class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfarjuncp10
 
Java Programs
Java ProgramsJava Programs
Java Programsvvpadhu
 
C# Delegates, Events, Lambda
C# Delegates, Events, LambdaC# Delegates, Events, Lambda
C# Delegates, Events, LambdaJussi Pohjolainen
 
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;.pdfkareemangels
 
#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.pdfanupambedcovers
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword pptVinod Kumar
 
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 LevelRamrao Desai
 
Header #include -string- #include -vector- #include -iostream- using.pdf
Header #include -string- #include -vector- #include -iostream-   using.pdfHeader #include -string- #include -vector- #include -iostream-   using.pdf
Header #include -string- #include -vector- #include -iostream- using.pdfgaurav444u
 
Solid principles
Solid principlesSolid principles
Solid principlesSahil Kumar
 

Similar to Starting with Main.java, where I tested everythingimport College..pdf (20)

Static keyword.pptx
Static keyword.pptxStatic keyword.pptx
Static keyword.pptx
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 
public class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfpublic class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdf
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
C# Delegates, Events, Lambda
C# Delegates, Events, LambdaC# Delegates, Events, Lambda
C# Delegates, Events, Lambda
 
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
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
constructer.pptx
constructer.pptxconstructer.pptx
constructer.pptx
 
Oop
OopOop
Oop
 
#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 ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Migrating to JUnit 5
Migrating to JUnit 5Migrating to JUnit 5
Migrating to JUnit 5
 
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
 
Header #include -string- #include -vector- #include -iostream- using.pdf
Header #include -string- #include -vector- #include -iostream-   using.pdfHeader #include -string- #include -vector- #include -iostream-   using.pdf
Header #include -string- #include -vector- #include -iostream- using.pdf
 
Solid principles
Solid principlesSolid principles
Solid principles
 
WDD_lec_06.ppt
WDD_lec_06.pptWDD_lec_06.ppt
WDD_lec_06.ppt
 
Jar chapter 5_part_i
Jar chapter 5_part_iJar chapter 5_part_i
Jar chapter 5_part_i
 

More from aptind

ssian chemist, Dmitri Mendeleev is often consider.pdf
                     ssian chemist, Dmitri Mendeleev is often consider.pdf                     ssian chemist, Dmitri Mendeleev is often consider.pdf
ssian chemist, Dmitri Mendeleev is often consider.pdfaptind
 
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdfaptind
 
               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdf               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdfaptind
 
You cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdfYou cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdfaptind
 
ViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdfViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdfaptind
 
Waterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdfWaterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdfaptind
 
Hi, I am unable to understand the terminology in .pdf
                     Hi, I am unable to understand the terminology in .pdf                     Hi, I am unable to understand the terminology in .pdf
Hi, I am unable to understand the terminology in .pdfaptind
 
The main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdfThe main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdfaptind
 
solution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfsolution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfaptind
 
Sexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfSexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfaptind
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfaptind
 
And is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfAnd is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfaptind
 
import java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfimport java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfaptind
 
Hi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdfHi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdfaptind
 
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfGiven below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfaptind
 
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfCisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfaptind
 
As we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfAs we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfaptind
 
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfAmount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfaptind
 
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdfaptind
 
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdfaptind
 

More from aptind (20)

ssian chemist, Dmitri Mendeleev is often consider.pdf
                     ssian chemist, Dmitri Mendeleev is often consider.pdf                     ssian chemist, Dmitri Mendeleev is often consider.pdf
ssian chemist, Dmitri Mendeleev is often consider.pdf
 
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
 
               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdf               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdf
 
You cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdfYou cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdf
 
ViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdfViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdf
 
Waterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdfWaterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdf
 
Hi, I am unable to understand the terminology in .pdf
                     Hi, I am unable to understand the terminology in .pdf                     Hi, I am unable to understand the terminology in .pdf
Hi, I am unable to understand the terminology in .pdf
 
The main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdfThe main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdf
 
solution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfsolution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdf
 
Sexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfSexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdf
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
 
And is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfAnd is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdf
 
import java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfimport java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdf
 
Hi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdfHi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdf
 
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfGiven below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdf
 
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfCisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
 
As we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfAs we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdf
 
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfAmount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
 
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
 
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
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 3JemimahLaneBuaron
 
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
 
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 ConsultingTechSoup
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
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...PsychoTech Services
 
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.pdfJayanti Pande
 
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).pdfSoniaTolstoy
 
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. Mahajanpragatimahajan3
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
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...
 
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
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
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...
 
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
 
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
 
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
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

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"; } }