/* Tueday pm lecture - Part 4: Sorting +---------------------------------+ | ALoCourse |<---------------------------+ +---------------------------------+ | | ALoCourse sort() | | | ALoCourse insert(Course course) | | +---------------------------------+ | / \ | | | - - - - - - - - - - - - - - - - - - - - - - - - | | | | +---------------------------------+ +---------------------------------+ | | MTLoCourse | | ConsLoCourse | | +---------------------------------+ +---------------------------------+ | | ALoCourse sort() | +-| Course first | | | ALoCourse insert(Course course) | | | ALoCourse rest |--+ +---------------------------------+ | +---------------------------------+ | | ALoCourse sort() | | | ALoCourse insert(Course course) | | +---------------------------------+ | v +---------------------------+ | Course | +---------------------------+ | String title | | int number | +---------------------------+ +---------------------------+ */ // to represent a Course class Course { String title; int number; Course(String title, int number) { this.title = title; this.number = number; } } // to represent a list of Courses interface ALoCourse { // sort this list of Courses in ascending order of the number of number ALoCourse sort(); // insert the given Course into this sorted list ALoCourse insert(Course course); } // to represent an empty llist of Courses class MTLoCourse implements ALoCourse { MTLoCourse() {} // sort this list of Courses in ascending order of the number of number ALoCourse sort(){ return this; } // insert the given Course into this sorted list ALoCourse insert(Course course){ return new ConsLoCourse(course, this); } } // to represent a non-empty list of Courses class ConsLoCourse implements ALoCourse { Course first; ALoCourse rest; ConsLoCourse(Course first, ALoCourse rest) { this.first = first; this.rest = rest; } // sort this list of Courses in ascending order of the number of number ALoCourse sort(){ return this.rest.sort().insert(this.first); } // insert the given Course into this sorted list ALoCourse insert(Course course){ if (course.number < this.first.number){ return new ConsLoCourse(course, this); } else{ return new ConsLoCourse(this.first, this.rest.insert(course)); } } } // client class for the list of Courses class Examples { Examples(){} Course lab1 = new Course("CS Lab 1", 212); Course eng1 = new Course("English 1", 128); Course opsys = new Course("Operating Systems", 313); Course biol = new Course("Biology 1", 210); ALoCourse mtcourses = new MTLoCourse(); ALoCourse list1 = new ConsLoCourse(this.lab1, this.mtcourses); ALoCourse list2 = new ConsLoCourse(this.eng1,this.list1); ALoCourse list4 = new ConsLoCourse(this.opsys, new ConsLoCourse(this.biol, this.list2)); // test the method insert boolean testInsert(){ return (check this.mtcourses.insert(this.opsys) expect new ConsLoCourse(this.opsys, this.mtcourses)) && (check this.list1.insert(this.eng1) expect new ConsLoCourse(this.eng1, this.list1)) && (check this.list1.insert(this.opsys) expect new ConsLoCourse(this.lab1, new ConsLoCourse(this.opsys, this.mtcourses))) && (check new ConsLoCourse(this.eng1, new ConsLoCourse(this.biol, new ConsLoCourse(this.opsys, this.mtcourses))).insert(this.lab1) expect new ConsLoCourse(this.eng1, new ConsLoCourse(this.biol, new ConsLoCourse(this.lab1, new ConsLoCourse(this.opsys,this.mtcourses))))); } boolean testSort(){ return (check this.mtcourses.sort() expect new MTLoCourse()) && (check this.list1.sort() expect this.list1) && (check this.list4.sort() expect new ConsLoCourse(this.eng1, new ConsLoCourse(this.biol, new ConsLoCourse(this.lab1, new ConsLoCourse(this.opsys, this.mtcourses))))); } boolean testAll(){ return this.testInsert() && this.testSort(); } }