Skip to content

Instantly share code, notes, and snippets.

@noynaert
Created November 3, 2018 01:23
Show Gist options
  • Save noynaert/e5d32beef4b8fa702e7c24c021c80ea2 to your computer and use it in GitHub Desktop.
Save noynaert/e5d32beef4b8fa702e7c24c021c80ea2 to your computer and use it in GitHub Desktop.
Some Help on Homework 6 (and 7, but mainly 6)

About the Constructors

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.

A private method

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 ....

Houston, we have a problem

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."

A private method

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 ....

A version of the second constructor that works.

This technique would work.

   public Section(String courseId, String crn ...
       splitCourseId(courseId);
       setCrn(crn);
       setTitle(title);
       setInstructor(instructor);
       setMaximumEnrollment(maximumEnrollment);
       setSeatsAvailable(seatsAvailable);
   }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment