/*********************************************** * CS2510 Spring 2011 * Lecture #11 * Function-Classes/Objects (2) ***********************************************/ 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; } } // Selects a specific student interface ISelectStudent{ boolean select(Student stud); } // Select student by name class ByNameStudent implements ISelectStudent{ String name; // Abstracted/convenience constructor ByNameStudent(String name){ this.name = name; } // Default is (obviously) "Kevin" ByNameStudent(){ this("Kevin"); } // Select the given student if the name matches public boolean select(Student stud){ return stud.name.equals(this.name); } } // Select students with GPA of 4.0 class ByGPAStudent implements ISelectStudent{ // Select the given student if the GPA is a 4.0 public boolean select(Student stud){ return stud.gpa == 4.0; } } // Represents a List of Students interface ILoS{ // Is there any Student in this list that the picker likes boolean any(ISelectStudent picker); // Filter out all the graduating students // This can be abstracted too... ILoS filterGrads(); } // Represents an empty list of Students class MtLoS implements ILoS{ MtLoS(){ } // Is there any Student in this empty list that the picker likes (No) public boolean any(ISelectStudent picker){ return false; } // Filter out all the graduating students (Done) public ILoS filterGrads(){ return this; } } // 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.any(ISelectStudent) -- boolean * ... this.filterGrads() ... -- ILoS * * Method of Fields: * ... this.first.isGoodEnough() -- boolean * ... this.rest.any(ISelectStudent) -- boolean * ... this.rest.filterGrads() ... -- ILoS * */ // Is there any Student in this nonempty list that the picker likes public boolean any(ISelectStudent picker){ // Mini template: // ... picker.select(Student) ... -- boolean return (picker.select(this.first) || this.rest.any(picker)); } // Filter out all the graduating students public ILoS filterGrads(){ if(this.first.graduating) return this.rest.filterGrads(); else return new ConsLoS(this.first, this.rest.filterGrads()); } } // Examples and Tests... class LectureExamples{ Student stud1 = new Student("Ryan", 5.3, false); Student stud2 = new Student("Korbin", 4.0, true); Student stud3 = new Student("Kevin", 500, false); ILoS mt = new MtLoS(); ILoS los1 = new ConsLoS(this.stud1, this.mt); ILoS los2 = new ConsLoS(this.stud2, this.los1); ILoS los3 = new ConsLoS(this.stud3, new ConsLoS(this.stud2, this.los1)); ILoS los4 = new ConsLoS(this.stud3, this.los1); ISelectStudent bygpa = new ByGPAStudent(); ISelectStudent byname = new ByNameStudent(); ISelectStudent bynameRyan = new ByNameStudent("Ryan"); // Test select method in ByGPAStudent and in ByNameStudent boolean testSelect(Tester t){ return (t.checkExpect(this.bygpa.select(this.stud1), false) && t.checkExpect(this.bygpa.select(this.stud2), true) && t.checkExpect(this.byname.select(this.stud1), false) && t.checkExpect(this.byname.select(this.stud3), true) && t.checkExpect(this.bynameRyan.select(this.stud1), true); } // Test any by GPA boolean testAny(Tester t){ return (t.checkExpect(this.los2.any(new ByGPAStudent()), true) && t.checkExpect(this.los1.any(new ByGPAStudent()), false)); } // Test any by Name boolean testAny2(Tester t){ return (t.checkExpect(this.los2.any(new ByNameStudent()), false) && t.checkExpect(this.los1.any(new ByNameStudent("Ryan")), true) && t.checkExpect(this.los3.any(new ByNameStudent()), true) && t.checkExpect(this.los3.any(new ByNameStudent("Deniz")), false)); } // Test filterGrads boolean testFilterGrads(Tester t){ return (t.checkExpect(this.los2.filterGrads(), this.los1) && t.checkExpect(this.los3.filterGrads(), this.los4)); } }