Assignment 7: Visitors and Mutation
Goals: To practice using the visitor pattern.
Instructions
As always, be very careful with your naming conventions.
The submissions will be organized as follows:
Submission Homework 7 Problem 1: The Visitors.java file with all its classes, interfaces, methods and tests.
Submission Homework 7 Problem 2: The Registrar.java file with all its classes, interfaces, methods and tests.
Problem 1: Visitors
+-------------------+ | IArith | +-------------------+ +-------------------+ /_\ /_\ | |---------------------------------------------------------------- | | | +-------------+ +------------------------------+ +------------------------------------------+ | Const | | UnaryFormula | | BinaryFormula | +-------------+ +------------------------------+ +------------------------------------------+ | double num | | Function<Double,Double> func | | BiFunction <Double, Double, Double> func | +-------------+ | String name | | String name | | IArith child | | IArith left | +------------------------------+ | IArith right | +------------------------------------------+
Specifically, the above represents an arithmetic expression. The Function and BiFunction in UnaryFormula and BinaryFormula respectively denote the arithmetic operation to be applied to their respective operands (child and left,right).
You must support 4 binary formulas (named "plus", "minus", "mul" and "div" representing addition, subtraction, multiplication and division respectively), and 2 unary formulas (named "neg" and "sqr" representing negation and squaring respectively).
Design an interface IArithVisitor<R> representing a visitor that visits an IArith and produces a result of type R. The visitor must be usable as a Function object on IArith producing a result of type R. For example, given an IArith object named iObj, and a SomeVisitor class, we should be able to write new SomeVisitor().apply(iObj).
Design an accept(IArithVisitor<R>) method for the IArith interface and implement it on Const, UnaryFormula and BinaryFormula.
Design an EvalVisitor that visits an IArith and evaluates the tree to a Double answer.
Design a PrintVisitor that visits an IArith and produces a String showing the fully-parenthesized expression in Racket-like prefix notation (i.e. "(div (plus 1.0 2.0) (neg 1.5))"), using the name for Formulas and using Double.toString(num) on Consts.
Design a DoublerVisitor that visits an IArith and produces another IArith, where every Const in the tree has been doubled.
When evaluating the result, the computer on which this program will run has no support for negative numbers. Design a NoNegativeResults visitor that visits an IArith and produces a Boolean that is true, if a negative number is never encountered at any point during its evaluation.
Problem 2: The Registrar’s Office
The registrar’s office maintains a great deal of information about classes, instructors, and students. Your task in this problem is to model that data, and implement a few methods on it. We deliberately do not give you the class diagram for this problem: from the description below, you should properly design whatever classes you think are relevant. Please use generic lists (i.e. IList<T>) for this problem.
7.1 Data constraints
A Course consists of a name (a String), an Instructor named prof, and a list of Students named students. No Course can be constructed without an Instructor available to teach it. Hint: Do you think that you should provide a list when constructing a Course? Why or why not?
An Instructor has a name and a list of Courses named courses he or she teaches. Instructors are initially constructed without any Courses to teach.
A Student has a name, an id number (an int), and a list of Courses they are taking. Students are initially constructed without taking any Courses.
It should always be the case that any Student who is enrolled in a Course should appear in the list of Students for that Course, and the Course should likewise appear in the Student’s list of Courses.
It should always be the case that the Instructor for any Course should have that Course appear in the Instructor’s list of Courses.
7.2 Methods and examples to design
Design a method void enroll(Course c) that enrolls a Student in the given Course. Design any helper methods as appropriate.
Design a method boolean classmates(Student c) that determines whether the given Student is in any of the same classes as this Student.
Design a method boolean dejavu(Student c) that determines whether the given Student is in more than one of this Instructor’s Courses.
Construct example data consisting of at least five Students, at least four Courses and at least two Instructors. Test all your methods thoroughly.