I have to use my Student.java, Course.java, and Period.java to create the follow
ID: 3698011 • Letter: I
Question
I have to use my Student.java, Course.java, and Period.java to create the following method:
*** I have written my Student.java, Course.java and Period.java below***
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.
My Student.java is:
public class Student
{
String firstName;
String lastName;
int id;
int gradYear;
Course schedule[] = new Course[6];
public Student(String firstName, String lastName, int id, int gradYear)
{
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
this.gradYear = gradYear;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public int getId()
{
return id;
}
public int getGradYear()
{
return gradYear;
}
public Course[] getSchedule()
{
return schedule;
}
public String toString()
{
return this.id+": "+this.lastName+", "+this.firstName+" - "+this.gradYear;
}
public boolean equals(Student other)
{
return id==other.id;
}
}
My Course.java:
public class Course
{
int department;
int courseNum;
String name;
char day;
int timeSlot;
int credits;
Student students[] = new Student[20];
private Period period;
public Course(int department, int courseNum, String name, char day, int timeSlot, int credits) {
this.department = department;
this.courseNum = courseNum;
this.name = name;
this.day = day;
this.timeSlot = timeSlot;
this.credits = credits;
this.period = new Period(day, timeSlot);
}
public int getDepartment() {
return department;
}
public int getCourseNumber() {
return courseNum;
}
public String getName() {
return name;
}
public Period getPeriod() {
return period;
}
public int getCredits() {
return credits;
}
public Student[] getRoster() {
return students;
}
public String toString(){
return this.department+":"+this.courseNum+" "+"[" + this.name + "]"+" "+ this.period + " " + "credits:"+this.credits;
}
public boolean equals(Course other){
return courseNum==other.courseNum && department==other.department;
}
}
My Period.java:
public class Period
{
private char day;
private int timeSlot;
public Period(char day, int timeSlot)
{
if(day >= 'a' && day <= 'z'){
day = (char) (day + (int)'A' - (int)'a');
}
this.day = day;
this.timeSlot = timeSlot;
}
public char getDay()
{
return this.day;
}
public int getTimeSlot()
{
return this.timeSlot;
}
public String toString()
{
return ""+this.day + this.timeSlot;
}
public boolean equals(Period p)
{
return day == p.day && timeSlot == p.timeSlot;
}
public int compareTo(Period other){
if(equals(other))
{
return 0;
}
else{
if(other.day == day)
{
if(timeSlot < other.timeSlot)
{
return -1;
}
else{
return 1;
}
}
else{
String days = "SMTWHF";
int ind1 = days.indexOf(day);
int ind2 = days.indexOf(other.day);
if(ind1 < ind2){
return -1;
}
else{
return 1;
}
}
}
}
}
Again my qustion is to USE Student.java, Period.java, Course.java that CREATES the method
public static Course[] commonCourses(Student one, Student two)
THE METHOD SHOULD DO THE FOLLOWING:
1. given two students, return a list of all the courses both of the students are taking together in a new array (in any order).
2. the returned array should be just large enough to hold the results, and no larger; it should not contain any empty (null) entries.
3. if there are no courses in common, return null.
Explanation / Answer
WebReg.java
public class WebReg
{
//////////////////////////////////////////////////////////
// Main Methods
public static Course lookupCourseByName(Course[] catalog, String courseName)
{
for(Course c : catalog)
{
if(c.getName().equalsIgnoreCase(courseName))
{
return c;
}
}
return null;
}
public static Course[] lookupCoursesByDept(Course[] catalog, int department)
{
int counter = 0;
Course[] toReturn;
for(Course c : catalog)
{
if(c.getDepartment() == department)
{
counter++;
}
}
if(counter == 0)
{
return null;
}
toReturn = new Course[counter];
counter = 0;
for(Course c : catalog)
{
if(c.getDepartment() == department)
{
toReturn[counter] = c;
counter++;
}
}
return toReturn;
}
public static int countCredits(Student s)
{
int total = 0;
for(Course c : s.getSchedule())
{
if(c != null)
total += c.getCredits();
}
return total;
}
public static boolean addCourse(Student s, Course c)
{
if(isFullS(s) || isFullR(c) || sameTime(s, c) || duplicateCourse(s, c))
{
return false;
}
for(int i = 0; i < s.getSchedule().length; i++)
{
if(s.getSchedule()[i] == null)
{
s.getSchedule()[i] = c;
break;
}
}
for(int i = 0; i < c.getRoster().length; i++)
{
if(c.getRoster()[i] == null)
{
c.getRoster()[i] = s;
break;
}
}
return true;
}
public static boolean dropCourse(Student s, Course c)
{
int counter = 0;
for(Course current : s.getSchedule())
{
if(current != null)
{
if(current.equals(c))
{
s.getSchedule()[counter] = null;
cleanArray(s);
removeStudent(s, c);
return true;
}
counter++;
}
}
return false;
}
public static Course[] commonCourses(Student one, Student two)
{
int counter = 0;
Course[] common;
for(int i = 0; i < one.getSchedule().length; i++)
{
for(int j = 0; j < two.getSchedule().length; j++)
{
if(one.getSchedule()[i] != null && two.getSchedule()[j] != null)
{
if(one.getSchedule()[i].equals(two.getSchedule()[j]))
{
counter++;
}
}
}
}
if(counter == 0)
{
return null;
}
common = new Course[counter];
counter = 0;
for(int i = 0; i < one.getSchedule().length; i++)
{
for(int j = 0; j < two.getSchedule().length; j++)
{
if(one.getSchedule()[i] != null && two.getSchedule()[j] != null)
{
if(one.getSchedule()[i].equals(two.getSchedule()[j]))
{
common[counter] = one.getSchedule()[i];
counter++;
}
}
}
}
return common;
}
//////////////////////////////////////////////////////////
// Sorting Methods
public static void sortByNumber(Course[] catalog)
{
int itemsSorted;
Course temp;
int loc;
for(itemsSorted = 1; itemsSorted < catalog.length; itemsSorted++)
{
temp = catalog[itemsSorted];
loc = itemsSorted - 1;
while(loc >= 0 && fullID(catalog[loc]) > fullID(temp))
{
catalog[loc + 1] = catalog[loc];
loc--;
}
catalog[loc + 1] = temp;
}
}
public static void sortByTime(Course[] catalog)
{
int itemsSorted;
Course temp;
int loc;
for(itemsSorted = 1; itemsSorted < catalog.length; itemsSorted++)
{
temp = catalog[itemsSorted];
loc = itemsSorted - 1;
while(loc >= 0 && catalog[loc].getPeriod().compareTo(temp.getPeriod()) > 0)
{
catalog[loc + 1] = catalog[loc];
loc--;
}
catalog[loc + 1] = temp;
}
}
//////////////////////////////////////////////////////////
// Helper Methods
// Checks to see if a students schedule is full
public static boolean isFullS(Student s)
{
if(s != null)
{
for(Course c : s.getSchedule())
{
if(c == null)
{
return false;
}
}
}
return true;
}
// Checks to see if a class is full
public static boolean isFullR(Course c)
{
if(c != null)
{
for(Student s : c.getRoster())
{
if(s == null)
{
return false;
}
}
}
return true;
}
// goes through a student's entire schedule to see if the
// supplied course is at the same time as another class
// in said schedule
public static boolean sameTime(Student s, Course c)
{
// this whole long chain of ifs are here for the sole
// purpose of avoid a null pointer exception
if(s.getSchedule() != null)
{
if(c.getRoster() != null)
{
for(Course current : s.getSchedule())
{
if(current != null)
{
if(current.getPeriod().toString().equals(c.getPeriod().toString()))
{
return true;
}
}
}
}
}
return false;
}
// makes sure a student isn't register for the same class twice
// which in theory would be possible is they were at two different
// times
public static boolean duplicateCourse(Student s, Course c)
{
if(s.getSchedule() != null)
{
for(Course current : s.getSchedule())
{
if(current != null)
{
if(current.equals(c))
{
return true;
}
}
}
}
return false;
}
// cleans up an array and gets rid of any gaps
public static void cleanArray(Student s)
{
for(int i = 0; i < s.getSchedule().length; i++)
{
if(s.getSchedule()[i] == null)
{
for(int j = i; j < s.getSchedule().length; j++)
{
if(s.getSchedule()[j] != null)
{
s.getSchedule()[i] = s.getSchedule()[j];
s.getSchedule()[j] = null;
break;
}
}
}
}
}
public static void cleanArray(Course c)
{
for(int i = 0; i < c.getRoster().length; i++)
{
if(c.getRoster()[i] == null)
{
for(int j = i; j < c.getRoster().length; j++)
{
if(c.getRoster()[j] != null)
{
c.getRoster()[i] = c.getRoster()[j];
c.getRoster()[j] = null;
break;
}
}
}
}
}
// removes a student from the roster
public static void removeStudent(Student s, Course c)
{
for(int i = 0; i < c.getRoster().length; i++)
{
if(c.getRoster()[i] != null)
{
if(c.getRoster()[i].equals(s))
{
c.getRoster()[i] = null;
cleanArray(c);
break;
}
}
}
}
public static int fullID(Course c)
{
return (c.getDepartment()*1000) + c.getCourseNumber();
}
}