1
\$\begingroup\$

Here's what the question asks:

Build a class called Course.java. This class should have 4 properties: CourseId, CourseName, Description, and credit hours. Also add the appropriate set and get methods. Add a display() method to display these 4 properties out to the DOS window. Lastly add a main() method for testing. In the main() method instantiate a Course object, fill it with data using the set methods, then call the display method to display the data.

I just want to make sure is my code right and if there are mistake in my code.

public class Course {

//   ========================== Properties ===========================
private int courseid;
private String courseName;
private String description;
private String creditHours;

//   ==========================  Behaviors  ==========================
public void setCourseId(int c) { courseid = c; }
public int getCourseId() { return courseid;}

public void setCourseName(String cn) { courseName = cn; }
public String getCourseName() { return courseName;}

public void setDescription(String d) { description = d; }
public String getDescription() { return description;}

public void setCreditHours(int ch) { ch = 4; }
public int getCreditHours() { return ch;}

//Returning String
public String toString() {
    return courseName + ":" + description + ":" + creditHours;
}

public void display() {
    System.out.println("Course ID             = " + getCourseId());
    System.out.println("Course Name      = " + getCourseName());
    System.out.println("Description      = " + getDescription());
    System.out.println("Credit Hours           = " + getCreditHours());

} //end display()





public static void main(String args []) {

    Course c1;
    c1 = new Course();

    c1.setCourseId(109);
    c1.setCourseName("Intro to Python");
    c1.setDescription("This course intros the Python Prog Lang.");
    c1.setCreditHours(4);

    c1.display();


    //Test out toString() method
    System.out.println(c1);
} //end main
} //end class
\$\endgroup\$
2
\$\begingroup\$

Here's the list of things I would change:

  1. Indentation (make it 2 or 4 tabs)
  2. Field naming is inconsistent. For example, courseid should be changed to courseId. Secondly, you should append the prefix course to all fields, or omit it at all. (Don't forget to change your methods as well).
  3. Assuming that this is a POJO (Plain Old Java Object) class, then I would recommend removing the display() method from this class. It's simply not its responsibility to display itself(S from SOLID principles). Instead, you can modify your toString() method to look something like this:

`

public String toString() {
   return "Course ID:" + id + '\n' +
            "Course name:" + name + '\n' +
            "Course description:" + description + '\n' +
            "Credit hours:" + creditHours + '\n';
}

` And then use it in a different object/function whose responsibility is to present information to the user.

  1. From my personal experience, I can say that the id field should be a String. If you are getting data from a server, and then convert that data to a POJO, the id field may cause an overflow in some situations if it's an integer. Even longdata type may cause an overflow if the id value is very long.

  2. I don't think it's a professional way to declare and initialize an instance of a class the way it was written. You can do both in a single line.

This is how I would have written it:

public class Course {

  private String id;
  private String name;
  private String description;
  private float creditHours;

  public void setId(String id) {
     this.id = id;
  }

  public String getId() {
     return id;
  }

  public String getName() {
     return name;
  }

  public void setCreditHours(float creditHours) {
     this.creditHours = creditHours;
  }

  public void setName(String name) {
     this.name = name;
  }

  public void setDescription(String description) {
     this.description = description;
  }

  public String getDescription() {
     return description;
  }


  public String toString() {
     return "Course ID:" + id + '\n' +
            "Course name:" + name + '\n' +
            "Course description:" + description + '\n' +
            "Credit hours:" + creditHours + '\n';
  }

  public static void main(String args[]) {
    final Course course = new Course();
    course.setCreditHours(15.6f);
    course.setDescription("BlaBla");
    course.setId("12345567878665345453123");
    course.setName("Just a boring class");

    System.out.print(course.getDescription());
  }
}

It contains some other corrections as well.

Hope this helps!

As a side note, though it seems absolutely superfluous, I would like you to know that's it's not my intention to criticize your code, I'm simply expressing my opinion based on the experience I have.

\$\endgroup\$
  • \$\begingroup\$ I agree for the most, but: 3) is a remark to the teacher, display() method has been requested explicitly. 4) String is only applicapable if your IDs can contain letters or leading zeros. For numeric IDs of (virtually) infinite length BigInteger is the better choice. \$\endgroup\$ – Timothy Truckle Mar 13 '18 at 11:49
  • \$\begingroup\$ Aslo, in 2) would you mind to add a link to the Java Naming Conventions ? \$\endgroup\$ – Timothy Truckle Mar 13 '18 at 11:52
  • \$\begingroup\$ @TimothyTruckle for 3 I'd recommend pointing out to the teacher that this isn't very cohesive (Not in front of the class though. Everyone makes small mistakes and this could be embarrassing for them). For 4) You both are wrong. id is an ID, and should be modelled as such, an object should be created for the ID. It isn't the better choice to store a BigInteger in a string, it's the best option to model things as closely as their realistic counterpart. 2) He is right naming conventions are camelCase for fields/methods in java. Contants are all caps and classes are CamelCase. \$\endgroup\$ – James Mar 15 '18 at 3:51
0
\$\begingroup\$

This is an immutable version of your course class:

// using 'final' assuming we want this class to be immutable
// making class final eliminates the possiblity of inheritance.
public final class Course {

    // adding final modifier
    private final int courseid;
    private final String courseName;
    private final String description;
    private final String creditHours;

    // single public constructor with no setters.
    public Course(final int courseid,
            final String courseName,
            final String description,
            final String creditHours) {
        this.courseid = courseid;
        this.courseName = courseName;
        this.description = description;
        this.creditHours = creditHours;
    }

    public int getCourseid() {
        return courseid;
    }

    public String getCourseName() {
        return courseName;
    }

    public String getDescription() {
        return description;
    }

    public String getCreditHours() {
        return creditHours;
    }

    // credit @nullbyte
    public String toString() {
        return "Course ID:" + courseid + '\n' +
            "Course name:" + courseName + '\n' +
            "Course description:" + description + '\n' +
            "Credit hours:" + creditHours + '\n';
    }

    @Override
    public boolean equals(final Object pO) {
        if (this == pO) {
            return true;
        }
        if (pO == null || getClass() != pO.getClass()) {
            return false;
        }

        final Course course = (Course) pO;

        if (courseid != course.courseid) {
            return false;
        }
        if (courseName != null ? !courseName.equals(course.courseName) : course.courseName != null) {
            return false;
        }
        if (description != null ? !description.equals(course.description) : course.description != null) {
            return false;
        }
        return creditHours != null ? creditHours.equals(course.creditHours) : course.creditHours == null;

    }

    @Override
    public int hashCode() {
        int result = courseid;
        result = 31 * result + (courseName != null ? courseName.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (creditHours != null ? creditHours.hashCode() : 0);
        return result;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.