// Code for CS1Ah Note 19: Supporting Queries. // November 2002. public class Student { public String surname; public String forename; public int matricno; public String dos; // Director of Studies public Course[] courses; public Student(String forename, String surname, String dos) { this.forename = forename; this.surname = surname; this.dos = dos; this.courses = new Course[6]; // Assume 6 courses max. // Should allow for some entries being null. } public boolean hasDos(String dosName) { return dos.equals(dosName); } // Check whether any of student's courses have this title. // Returns index to course if does. Otherwise returns -1. private int findCourse(String searchTitle) { for (int i = 0; i < courses.length; i++) { if (courses[i] != null && courses[i].title.equals(searchTitle) ) // NB: Use of &&, not & is essential here. return i; } return -1; } public boolean takingCourse(String title) { return findCourse(title) >= 0; } // For convenience, returns false if course is not being taken. public boolean hasMarkOver(String courseTitle, int markBound) { int i = findCourse(courseTitle); if (i < 0) return false; else return courses[i].mark >= markBound; } public String toString() {return forename + " " + surname;} public boolean property1() { return hasDos("Paul Jackson") & hasMarkOver("CS2", 50) & ( !takingCourse("AI2") | hasMarkOver("AI2", 50) ); } public boolean hasBasicProperty(String name) { if (name.equals("taking CS2")) return takingCourse("CS2"); else if (name.equals("taking AI2")) return takingCourse("AI2"); else if (name.equals("not taking AI2")) return !takingCourse("AI2"); else if (name.equals("DoS is Paul Jackson")) return hasDos("Paul Jackson"); else if (name.equals("CS2 mark over 50")) return hasMarkOver("CS2", 50); else if (name.equals("AI2 mark over 50")) return hasMarkOver("AI2", 50); else return false; } public boolean hasConjunctiveProperty(String[] props) { boolean res = true; for (int i = 0; i < props.length; i++) { if (!hasBasicProperty(props[i])) res = false; } return res; // True if all props hold } // DNF = Disjunctive Normal Form: Disjunction of Conjunction // of literals. Literal = basic property or negation of // basic property. public boolean hasDnfProperty(String[][] dnfProp) { boolean res = false; for (int i = 0; i < dnfProp.length; i++) { if (hasConjunctiveProperty(dnfProp[i])) res = true; } return res; // True if all props hold } }