Course.java
Select all
/* Course.java CIS 260 David Klick 2014-02-09 This program demonstrates some simple class concepts. */ public class Course { private String department; private int courseNumber; private String section; private String room; private String name; // a variety of constructors is provided public Course(String dept, int num, String sec, String rm, String nm) { setDepartment(dept); setCourseNumber(num); setSection(sec); setRoom(rm); setName(nm); } public Course(String dept, int num, String sec, String rm) { this(dept, num, sec, "", rm); } public Course(int num, String sec, String rm) { this("CIS", num, sec, "", rm); } public Course(int num, String rm) { this("CIS", num, "A", "", rm); } public Course() { this("", 0, "", "", ""); } // mutator methods public void setDepartment(String dept) { if (dept == null) dept = ""; department = dept; } public void setCourseNumber(int num) { if (num < 0) num *= -1; courseNumber = num; } public void setSection(String sec) { if (sec == null || sec.length() == 0) sec = "A"; section = sec; } public void setRoom(String rm) { if (rm == null) rm = ""; room = rm; } public void setName(String name) { if (name == null) name = ""; this.name = name; } // accessor methods public String getDepartment() { return department; } public int getCourseNumber() { return courseNumber; } public String getSection() { return section; } public String getRoom() { return room; } public String getName() { return name; } // useful for easy output and debugging public String toString() { String s = department + ' ' + courseNumber + section; if (name.length() > 0) s += " (" + name + ")"; if (room.length() > 0) s += room; return s; } }