For homeworks 6 and 7 you need two constructors. Here are some examples:
Section a = new Section("CSC","254","12344","PC 2",
"McTeacher, M.",25,15);
Section b = new Section("MAT111E","12345","Statistics",
"McTeacher, M.",25,-3);
The a object uses the constructor that takes separate discipline and course number. The second example uses the combined constructor.
This would be an opportunity to create a private method like the following:
/**
* This takes a courseId like "CSC254" and converts it into
* the discipline and the course number. this.discipline and this.number are set.
* @param courseId This is the combination of the discipline and the number like
* "CSC184" or "MAT110E"
*/
private void splitCourseId(String courseId){
if(courseId.length() >= 6){
this.discipline = courseId.substring(0,3);
this.number = cour ....
Using the patterns we have used in class we would normally try to write something like this:
public Section(String courseId, String crn ...
String tempDiscipline = courseId.substring(0,3);
String tempNumber = courseId.substring(3);
this(tempDiscipline, tempNumber crn ...)
There is a problem. If we use "this" it must be the first statement in the constructor. The problem is that we need to do some logic first.
There is a solution. We won't use "this."
This would be an opportunity to create a private method like the following:
/**
* This takes a courseId like "CSC254" and converts it into
* the discipline and the course number. this.discipline and this.number are set.
* @param courseId This is the combination of the discipline and the number like
* "CSC184" or "MAT110E"
*/
private void splitCourseId(String courseId){
if(courseId.length() >= 6){
this.discipline = courseId.substring(0,3);
this.number = cour ....
This technique would work.
public Section(String courseId, String crn ...
splitCourseId(courseId);
setCrn(crn);
setTitle(title);
setInstructor(instructor);
setMaximumEnrollment(maximumEnrollment);
setSeatsAvailable(seatsAvailable);
}