/*********************************************** * CS2510 Fall 2011 * Lecture #10 * Function-Classes/Objects (1) ***********************************************/ import tester.*; // Thought Exercise for overloading and instance functions class A { String f1(A a) { return a.g(); } String f2() { return this.g(); } String g() { return "A.g"; } } class B extends A { String g() { return "B.g"; } } class QuizExamples { A a1 = new A(); A a2 = new B(); String string1 = a2.f1(a2); String string2 = a2.f2(); String string3 = a2.f1(a1); } /**********************************************************/ // Student: Represents a student with identifying data class Student { String name; double gpa; boolean graduating; /** * Template * * Fields: * this.name -- String * this.gpa -- double * this.graduating -- boolean * Methods: * this.graduate -- Student */ Student(String name, double gpa, boolean graduating) { super(); this.name = name; this.gpa = gpa; this.graduating = graduating; } // Returns this student with graduating set to true Student graduate() { return new Student(this.name, this.gpa, true); } } // Represents a list of students interface ILoS { /** * Template * * Methods: * this.insertByComp(Student, IComp) -- ILoS * this.sortByComp(IComp) -- ILoS */ // insertByComp: Inserts a student into this *sorted* list in the correct // position according to the comparator public ILoS insertByComp(Student s, IComp decider); // sortByComp: Sorts this list by the comparator public ILoS sortByComp(IComp decider); } // MtLoS: The empty element in a student list class MtLoS implements ILoS { /** * Template * * Methods: * this.insertByComp(Student, IComp) -- ILoS * this.sortByComp(IComp) -- ILoS */ // Creates a new cons as there is nothing to compare in an empty list public ILoS insertByComp(Student s, IComp decider) { return new ConsLoS(s,this); } // Nothing to sort, return this public ILoS sortByComp(IComp decider) { return this; } } //ConsLoS: This cons element in a student list class ConsLoS implements ILoS { Student first; ILoS rest; /** * Template * * Fields: * this.first -- Student * this.rest -- ILoS * Methods: * this.insertByComp(Student, IComp) -- ILoS * this.sortByComp(IComp) -- ILoS */ ConsLoS(Student first, ILoS rest) { super(); this.first = first; this.rest = rest; } // insertByComp: Inserts a new student into our *sorted* list // using the decider for comparison public ILoS insertByComp(Student s, IComp decider) { if(decider.lessThan(s,first)) { return new ConsLoS(s,this); } else { return new ConsLoS(first, rest.insertByComp(s,decider)); } } // sortByComp: Sorts this element and the recurses based on the decider public ILoS sortByComp(IComp decider) { return rest.sortByComp(decider).insertByComp(first, decider); } } // IComp: Comparison class interface IComp { /** * Template * * Fields: * Methods: * this.lessThan(Student, Student) -- boolean */ // lessThan: Returns true if s1 < s2 for the given // comparison operation public boolean lessThan(Student s1, Student s2); } // CompByName: A comparator for sorting alphanumerically by name class CompByName implements IComp { /** * Template * * Fields: * Methods: * this.lessThan(Student, Student) -- boolean */ // lessThan: Returns true is s1.name sorts before s2.name public boolean lessThan(Student s1, Student s2) { return (s1.name.compareTo(s2.name) < 0); } } //CompByName: A comparator for sorting numerically by gpa class CompByGPA implements IComp { /** * Template * * Fields: * Methods: * this.lessThan(Student, Student) -- boolean */ // lessThan: Returns true is s1.gpa is less than s2.gpa public boolean lessThan(Student s1, Student s2) { return (s1.gpa < s2.gpa); } } // Examples for the Students and lists class StudentExamples { // Some example students Student kev = new Student("Kevin",3.4,false); Student spenc = new Student("Spencer",2.9,false); Student maria = new Student("Maria",3.7,true); // Comparator examples IComp byname = new CompByName(); IComp bygpa = new CompByGPA(); // List examples ILoS mtS = new MtLoS(); ILoS mlist = new ConsLoS(maria,mtS); ILoS spke = new ConsLoS(spenc,new ConsLoS(kev,mtS)); ILoS kesp = new ConsLoS(kev,new ConsLoS(spenc,mtS)); ILoS students = new ConsLoS(kev, new ConsLoS(spenc, new ConsLoS(maria,mtS))); ILoS studentssort = new ConsLoS(kev, new ConsLoS(maria, new ConsLoS(spenc,mtS))); ILoS studentssortgpa = new ConsLoS(spenc, new ConsLoS(kev, new ConsLoS(maria,mtS))); // Test our comparator boolean testLessThan(Tester t) { return t.checkExpect(byname.lessThan(kev,spenc),true) && t.checkExpect(byname.lessThan(spenc, maria),false) && t.checkExpect(byname.lessThan(spenc, kev),false); } // Test our insertion function boolean testInsert(Tester t) { return t.checkExpect(mtS.insertByComp(kev, byname),new ConsLoS(kev,mtS)) && t.checkExpect(kesp.insertByComp(maria, byname),studentssort) && t.checkExpect(mlist.insertByComp(kev, byname), new ConsLoS(kev,mlist)); } // Test our Sorting function boolean testSort(Tester t) { return t.checkExpect(students.sortByComp(byname),studentssort) && t.checkExpect(students.sortByComp(bygpa),studentssortgpa) ; } }