CS U370 Assignment #7. Section: Clinger Assigned: Tuesday, 21 November 2006 Due: Thursday, 30 November 2006 **************************************************************** For this assignment, you will do three different things: 1. You will change most of the static methods of the Report ADT into dynamic methods. 2. You will add equals(Object) and hashCode() methods to the Report ADT, overriding the methods inherited from Object. These methods may be defined in terms of the corresponding methods for a version of the Issue ADT that has been extended by overriding the same methods; the instructor will provide an implementation of the extended Issue ADT in /course/csu370wc/Assignments/A7. 3. You will implement the functional visitor pattern by adding an accept method to the Report ADT. Turn in your work on this assignment before 3 pm on the due date by sending electronic mail to will@ccs.neu.edu with subject CSU370 assignment 7 and a body that consists of nothing but your Report.java file. That file should begin with a block comment that lists 1. Your name, as you want the instructor to write it. 2. Your email address. 3. Any remarks that you wish to make to the instructor. Late assignments may be discounted, and very late assignments may be discarded. -------------------------------------------------- Your assignment is to write the code for a single file, Report.java, in the project directory, that declares a public class named Report that implements the Report ADT specified below. Note further that this Report ADT is quite different from the Report ADT you implemented for assignments 4 and 6, because the only static method is Report.empty(). The IssueVisitor interface and a crude test program will be made available in /course/csu370wc/Assignments/A7 -------------------------------------------------- Specification of the Report ADT. Report is an immutable abstract data type. In our implementation language, Java, it corresponds to the project.Report class, which is specified below. The Report class shall be implemented in Java, and will be tested using Sun's Java 2 Runtime Environment, Standard Edition, version 5.0. The code for this implementation will be in the project package, and shall define a public class named Report, which provides the following public methods, which are specified below. Signature and Java syntax: public static Report empty(); public Report adjoin(Issue); public Report select(Comparator, Issue); public Report sort(Comparator); public Report insert(Comparator, Issue); public int length(); public Issue first(); public Issue last(); public Report withoutFirst(); public Report withoutLast(); public String toString(); public boolean equals(Object); public int hashCode(); public Report accept (IssueVisitor); public Iterator iterator (); public Iterator iterator (Comparator); Specification of behavior for all but the iterator methods: Report.empty().select(comp, iss0) = Report.empty() r.adjoin(issue).select(comp, iss0) = r.select(comp, iss0).adjoin(issue) if comp.compare(issue, iss0) = 0 r.adjoin(issue).select(comp, iss0) = r.select(comp, iss0) if comp.compare(issue, iss0) != 0 Report.empty().sort(comp) = Report.empty() r.adjoin(issue).sort(comp) = r.sort(comp).insert(comp, issue) Report.empty().insert(comp, iss0) = Report.empty().adjoin(iss0) r.adjoin(issue).insert(comp, iss0) = r.adjoin(issue).adjoin(iss0) if comp.compare(issue, iss0) <= 0 r.adjoin(issue).insert(comp, iss0) = r.insert(comp, iss0).adjoin(issue) if comp.compare(issue, iss0) > 0 Report.empty().length() = 0 r.adjoin(issue).length() = r.length() + 1 r.adjoin(issue).first() = issue if r.length() = 0 r.adjoin(issue).first() = r.first() if r.length() > 0 r.adjoin(issue).last() = issue r.adjoin(issue).withoutFirst() = Report.empty() if r.length() = 0 r.adjoin(issue).withoutFirst() = r.withoutFirst().adjoin(issue) if r.length() > 0 r.adjoin(issue).withoutLast() = r Report.empty().toString() = "Report: empty" r.adjoin(issue).toString() = issue.toString() if r.length() = 0 r.adjoin(issue).toString() = r.toString() + "\n" + issue.toString() if r.length() > 0 Report.empty().equals(x) = (x instanceof Report) and (((Report) x).length() = 0) r.adjoin(issue).equals(x) = (x instanceof Report) and (((Report) x).length() = (r.length() + 1)) and r.equals(((Report) x).withoutLast()) and issue.equals(((Report) x).last()) Report.empty().hashCode() = 2072679696 r.adjoin(issue).hashCode() = ((r.hashCode() / 2) + issue.hashCode()) modulo 4294967296 Report.empty().accept(v) = Report.empty() r.adjoin(issue).accept(v) = r.accept(v).adjoin(v.visit(issue)) Informal specification of the iterator methods: Both iterator methods return an Iterator that generates the Issue objects of the Report. The iterator() method returns an Iterator that generates those objects beginning with the first, and continuing in the order implied by the Report.first and Report.withoutFirst methods. If r is a Report and c a Comparator, then r.iterator(c) returns an Iterator that generates the Issue objects of r in the same order they would be generated by the Iterator result of Report.sort(r, comp).iterator() If the remove method of any Iterator created by these two iterator methods is called, then it shall throw an UnsupportedOperationException. If the next() method of any Iterator created by these two iterator methods is called when the hasNext() method would return false, then the next() method shall throw a NoSuchElementException. Clients of the extended Report ADT are allowed to rely upon these exceptions. Notes: Note 1: The comparator argument to the select and sort methods is not necessarily consistent with equals. Note 2: The compare method of the comparator argument to the select and sort methods must not have any side effects. Note 3: The IssueVisitor interface will be supplied by the instructor, and is in the project package. Note 4: The equals(Object) and hashCode() methods shall conform to the specification of those methods in the API for Java 2 Platform Standard Edition 5.0. Note 5: In Java, all int addition is performed modulo 4294967296. Note 6: The accept method is not completely specified by the algebraic specification above, because a visitor's accept method may have side effects. In addition to the behavior specified by the algebraic specification, the accept method of a Report r must call the visitor's visit method exactly once on each Issue of r, and these calls must occur in order beginning with the first Issue and continuing to the last. Note 7: The iterator method is not specified by an algebraic specification because it returns a mutable object. --------------------------------------------------