Currently the Session class contains the department and number information for the associated course, as well as its number of credits. If there can be multiple sessions of a course, this implementation is inefficient and troublesome, since the course information will be repeated throughout each Session object. What you want instead is a way of tying many session objects back to a single Course object. The UML diagram in Figure 9.1 shows this many-to-one relationship from Session to a new class named Course. Figure 9.1. A Course Has Many SessionsYou will refactor your code to the above relationship. Start by creating a simple Course class that captures the department, number, and number of credits. A simple test declares that department and number are required to create a Course. The department and number combined represent the unique key for a Course. Two separate courses cannot have the same department and number. package sis.studentinfo; import junit.framework.*; public class CourseTest extends TestCase { public void testCreate() { Course course = new Course("CMSC", "120"); assertEquals("CMSC", course.getDepartment()); assertEquals("120", course.getNumber()); } } Course starts out as a simple data object. It contains a constructor and two getters for the key fields. package sis.studentinfo; public class Course { private String department; private String number; public Course(String department, String number) { this.department = department; this.number = number; } public String getDepartment() { return department; } public String getNumber() { return number; } } |