/*********************************************** * CS2510 Spring 2011 * Lecture #10 * Function-Classes/Objects (1) ***********************************************/ import tester.*; // Represents a Student class Student{ String name; double gpa; boolean graduating; Student(String name, double gpa, boolean graduating) { this.name = name; this.gpa = gpa; this.graduating = graduating; } } // Represents a List of Students interface ILoS{ // Insert the given Student into this sorted list with the given IComp public ILoS insertByComp(Student s, IComp decider); // Sort this list by the given IComp public ILoS sortByComp(IComp decider); } // Represents an empty list of Students class MtLoS implements ILoS{ MtLoS(){ } // Insert the given Student into this sorted list by the given IComp public ILoS insertByComp(Student s, IComp decider){ return new ConsLoS(s, this); } // Sort this list by the given IComp public ILoS sortByComp(IComp decider){ return this; } } // An interface for comparing Students interface IComp{ // Decide which Student should go "first" public boolean lessThan(Student s1, Student s2); } // Function-Class for comparing Students by GPA class ByGPA implements IComp{ // Decide which goes first, by GPA public boolean lessThan(Student s1, Student s2){ return s1.gpa > s2.gpa; } } // Function-Class for comparing Students by Name class ByName implements IComp{ // Decide which goes first, by Name public boolean lessThan(Student s1, Student s2){ return s1.name.compareTo(s2.name) < 0; } } // Represents a non-empty list of Students class ConsLoS implements ILoS{ Student first; ILoS rest; ConsLoS(Student first, ILoS rest) { this.first = first; this.rest = rest; } /** Template * Fields: * ... this.first ... -- Student * ... this.rest ... -- ILoS * * Methods: * ... this.insert(Student) ... -- ILoS * ... this.sort() ... -- ILoS * * Method of Fields: * ... this.rest.insertByComp(Student,IComp) ... -- ILoS * ... this.rest.sort(IComp) ... -- ILoS */ // Insert the given Student into this sorted list by the given IComp public ILoS insertByComp(Student s, IComp decider){ if(decider.lessThan(s, this.first)){ return new ConsLoS(s, this); }else{ return new ConsLoS(this.first, this.rest.insertByComp(s, decider)); } } // Sort this list by the given IComp public ILoS sortByComp(IComp decider){ return this.rest.sortByComp(decider) .insertByComp(this.first, decider); } } // Examples and Tests for this Lecture... class LectureExamples{ // Important students... Student kev = new Student("Kevin", 490, true); Student spenc = new Student("Spencer", 500, false); Student gina = new Student("Gina", 500, false); // Some function-objects IComp bygpa = new ByGPA(); IComp byname = new ByName(); // test the method lessThan in classes ByGPA and ByName boolean testLessThan(Tester t){ return t.checkExpect(this.bygpa.lessThan(this.gina, this.kev), false) && t.checkExpect(this.bygpa.lessThan(this.kev, this.spenc), true) && t.checkExpect(this.bygpa.lessThan(this.kev, this.kev), false) && t.checkExpect(this.byname.lessThan(this.spenc, this.kev), false) && t.checkExpect(this.byname.lessThan(this.gina, this.kev), true) && t.checkExpect(this.byname.lessThan(this.spenc, this.spenc), false); } // Some example Lists ILoS mt = new MtLoS(); ILoS los = new ConsLoS(this.spenc, this.mt); ILoS los2 = new ConsLoS(this.kev, this.los); ILoS sortedGPA = new ConsLoS(this.spenc, new ConsLoS(this.kev, this.mt)); ILoS sortedName = new ConsLoS(this.kev, new ConsLoS(this.spenc, this.mt)); boolean testInsert(Tester t){ return (t.checkExpect(this.mt.insertByComp(this.kev, this.bygpa), new ConsLoS(this.kev, this.mt)) && t.checkExpect(this.los.insertByComp(this.kev, this.bygpa), this.sortedGPA) && t.checkExpect(this.mt.insertByComp(this.kev, this.byname), new ConsLoS(this.kev, this.mt)) && t.checkExpect(this.los.insertByComp(this.kev, this.byname), this.sortedName)); } boolean testSort(Tester t){ return (t.checkExpect(this.mt.sortByComp(this.bygpa), this.mt) && t.checkExpect(this.los.sortByComp(this.bygpa), this.los) && t.checkExpect(this.los2.sortByComp(this.bygpa), this.sortedGPA) && t.checkExpect(this.mt.sortByComp(this.byname), this.mt) && t.checkExpect(this.los.sortByComp(this.byname), this.los) && t.checkExpect(this.los2.sortByComp(this.byname), this.sortedName)); } }