Sorting Courses and Analysis (Phase 4) Project Description You will build part o
ID: 3697837 • Letter: S
Question
Sorting Courses and Analysis (Phase 4) Project Description You will build part of a college's course registration system (similar to WebReg, though considerably less complex). The system will keep track of a catalog of courses that students can take. It will allow the user (a student) to view and search the course catalog, and to add (register for) and drop (withdraw from) courses. The following restrictions apply: All courses meet only once per week, for one class period. Days will be denoted by the following letters: M - Monday, T - Tuesday, W - Wednesday, H - Thursday, F - Friday, S - Saturday. There are no Sunday courses. Class periods (time slots) are numbered from 1 to 9. A student cannot register for a course that is not in the catalog. There is a limit on the number of courses a student may take in one semester. There is NO explicit limit on the number of credits a student may carry in one semester. A student cannot register for two courses that meet on the same day and time. Sorting Courses and Analysis (Phase 4) Implement the following methods in WebReg.java. DO NOT modify your Period, Course, and Student objects. If needed, you may add helper methods and fields in WebReg.java. public static Course[] commonCourses(Student one, Student two) given two students, return a list of all the courses both of the students are taking together in a new array (in any order). the returned array should be just large enough to hold the results, and no larger; it should not contain any empty (null) entries. if there are no courses in common, return null. public static void sortByNumber(Course[] catalog) sort the catalog into ascending order by department and course number (i.e., courses should be ordered by department number, and courses in the same department should be ordered by individual course number). you may use any sorting algorithm that you wish. public static void sortByTime(Course[] catalog) sort the catalog into chronological order by meeting day and time (i.e., courses should be ordered by the day of week on which they meet, and courses that meet on the same day should be ordered by the class period at which they meet). you may use any sorting algorithm that you wish
Explanation / Answer
Assuming the course and student classes partially as
class Student {
private String name;
private Course courses[];
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Course[] getCourses() {
return courses;
}
public void setCourses(Course[] courses) {
this.courses = courses;
}
}
class Course {
private int courseNumber;
private int departmentId;
private char day;
private int time;
public int getCourseNumber() {
return courseNumber;
}
public void setCourseNumber(int courseNumber) {
this.courseNumber = courseNumber;
}
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;
}
public char getDay() {
return day;
}
public void setDay(char day) {
this.day = day;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
}
class WebReg {
public static Course[] commonCourses(Student one, Student two) {
ArrayList<Course> courses = new ArrayList<Course>();
Course[] courses1 = one.getCourses();
Course[] courses2 = one.getCourses();
if(courses1 == null || courses2 == null) //no common
return null;
if(courses1 != null && courses2 != null) {
for(Course cs1 : courses1) {
for(Course cs2 : courses1) {
//compare courses based on deparatment id and course id
if(cs1.getCourseNumber() == cs2.getCourseNumber() && cs1.getDepartmentId() == cs2.getDepartmentId()) {
courses.add(cs1);
break;
}
}
}
}
if(courses.size() > 0)
return (Course[])courses.toArray();
else
return null;
}
public static void sortByNumber(Course[] catalog) {
Course temp = null;
if(catalog != null && catalog.length > 0) {
for(int i = 0; i < catalog.length; i++) {
for(int j = 0; j < catalog.length; j++) {
if(catalog[i].getDepartmentId() < catalog[j].getDepartmentId()) {
temp = catalog[j];
catalog[j] = catalog[i];
catalog[i] = temp;
} else if(catalog[i].getDepartmentId() == catalog[j].getDepartmentId()) {
if(catalog[i].getCourseNumber() < catalog[j].getCourseNumber()) {
temp = catalog[j];
catalog[j] = catalog[i];
catalog[i] = temp;
}
}
}
}
}
}
public static void sortByTime(Course[] catalog) {
Course temp = null;
if(catalog != null && catalog.length > 0) {
for(int i = 0; i < catalog.length; i++) {
for(int j = 0; j < catalog.length; j++) {
if(catalog[i].getDay() < catalog[j].getDay()) {
temp = catalog[j];
catalog[j] = catalog[i];
catalog[i] = temp;
} else if(catalog[i].getDay() == catalog[j].getDay()) {
if(catalog[i].getTime() < catalog[j].getTime()) {
temp = catalog[j];
catalog[j] = catalog[i];
catalog[i] = temp;
}
}
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.