import tester.*; // Interface for Lists of Strings interface ILoS{ // Sort: ILoS -> ILoS // Returns the sorted version of this list ILoS sort(); // Insert takes an element and adds it to this *sorted* list // insert ILoS String -> ILoS ILoS insert(String s); } // Empty elements for List of Strings class MtLoS implements ILoS { public ILoS sort() { return new MtLoS(); } public ILoS insert(String s) { return new ConsLoS(s,this); } } //Cons elements for List of Strings class ConsLoS implements ILoS { String first; ILoS rest; /* Template * * Fields * this.first - String * this.rest - ILoS * * Methods * this.sort() - ILoS */ ConsLoS(String first, ILoS rest) { this.first = first; this.rest = rest; } // Cons sort for insertion sort public ILoS sort() { return( rest.sort().insert(first)); } // Insert String s into this list based on string length public ILoS insert(String s) { if(s.length() < first.length()) { return new ConsLoS(s,this); } else { return new ConsLoS(first,rest.insert(s)); } } } // Examples and tests for lists of strings class ILoSExamples { ILoS los1 = new MtLoS(); ILoS los2 = new ConsLoS("Liam", los1); ILoS los3 = new ConsLoS("Johnny", los2); ILoS los4 = new ConsLoS("Jack", new ConsLoS("Kamboragen", new ConsLoS("Timothy", los1))); ILoS los3s = new ConsLoS("Liam", new ConsLoS("Johnny",los1)); ILoS los4s = new ConsLoS("Jack", new ConsLoS("Timothy", new ConsLoS("Kamboragen", los1))); ILoS los5 = new ConsLoS("Jack", new ConsLoS("Frank", new ConsLoS("Timothy", new ConsLoS("Kamboragen", los1)))); boolean testSort(Tester t) { return (t.checkExpect(this.los1.sort(),los1) && t.checkExpect(this.los2.sort(),los2) && t.checkExpect(this.los3.sort(),los3s) && t.checkExpect(this.los4.sort(),los4s)); } boolean testInsert(Tester t) { return (t.checkExpect(this.los1.insert("Liam"),los2) && t.checkExpect(this.los2.insert("Johnny"),los3s) && t.checkExpect(this.los4s.insert("Frank"),los5)); } } // Some examples of primitive functions class LectureExamples { int i1 = 5; int i2 = 12; int i3 = i2 % i1; String s1 = "Hello"; String s2 = s1.substring(3); } // Public interface persons interface IPerson { // Count the number of class affiliations for this person int countClasses(); // Returns the person's name String getName(); } // Abstract class to hold fields and method implementations // common to all Persons abstract class APerson implements IPerson{ String name; int id; int age; String dept; /* Template * Fields: * String name * int id * int age * String dept * * Methods */ APerson(String name, int id, int age, String dept) { this.name = name; this.id = id; this.age = age; this.dept = dept; } public int countClasses() { // Should actually count classes, returned 1 simply // for demo purposes return 1; } public String getName() { return name; } } // The Faculty Person class Faculty extends APerson { String title; double salary; /* Template * Fields: * String name * int id * int age * String dept * String title * double salary * * Methods */ Faculty(String name, int id, int age, String dept, String title, double salary) { super(name, id, age, dept); this.title = title; this.salary = salary; } } // The Staff Person class Staff extends APerson { double salary; double hoursperweek; /* Template * Fields: * String name * int id * int age * String dept * double salary * double hoursperweek * * Methods */ Staff(String name, int id, int age, String dept, double salary, double hoursperweek) { super(name, id, age, dept); this.salary = salary; this.hoursperweek = hoursperweek; } } // The Student Person class Student extends APerson { String classlist; /* Template * Fields: * String name * int id * int age * String dept * String classlist * * Methods */ Student(String name, int id, int age, String dept, String classlist) { super(name, id, age, dept); this.classlist = classlist; } } // Examples of Persons class APersonExamples { Student me = new Student("Matthew", 1234, 24, "CS", "Info Theory, PPL, Fundies 2"); }