Pledge of Honor |
Design Pattern
In milestone 1 of the hangman project, you are asked to implement AWord as shown in the following UML diagram.1. Describe how you would implement AWord using the visitor pattern as well by drawing an appropriate UML diagram showing only the public methods of all the classes involved. Explain briefly the rationale for each of the abstract public methods in AWord. Describe briefly each of the concrete visitors you must have. Do not write any code. 20 points
Polynomials
anxn + an-1xn-1+ ... + a1x + a0,
where n is a non-negative integer, ak (k = 0.. n) are numbers, and an is a number not equal to 0.
n is called the degree (or order) of p(x),ak (k = 0.. n) are called the coefficients of p(x), and an is called the leading coefficient of p(x).
For examples:
We can describe polynomials in the following manner. There is an abstraction called polynomial. A constant polynomial is a polynomial with a leading coefficient and degree (order) 0. A non-constant polynomial is a polynomial with a leading coefficient, a non-zero degree, and a lower order polynomial. The polynomial's intrinsic behavior is to provide the clients with its degree -getDegree (), its leading coefficient - get LeadCoef (), and its lower order polynomial (if any) - getLowerPoly (). Polynomials can be viewed as having an immutable linear recursive structure and can be implemented using the composite pattern. All extrinsic algorithms on polynomials can be implemented as visitors to polynomials. The following UML diagram illustrates the design of the polynomials and their visitors. In this diagram, all the inherited concrete methods are not shown in order to save space.
2. Write a polynomial visitor called ToString to compute and return the String representation of the host as described by the following. For constant polynomials, return the String representation of the coefficient (you may use Double.toString (double d) to convert a double to a String). For non-constant polynomials, use ^ to denote exponentiation, and if the constant term is zero, do not show zero. For examples, 12x5 - 3x2 + 7 prints as 12x^5 + -3x^2 + 7, and 12x5 - 3x2 + 0 prints as 12x^5 + -3x^2. 15 points
3. Write a polynomial visitor called AddConst to add a constant double to the host polynomial and return the resulting polynomial. Decide how to pass the constant to AddConst yourself. 10 points
4. Write a polynomial visitor called Add to add a polynomial to the host and return the resulting sum polynomial. Decide how to pass the polynomial parameter to Add yourself. Feel free to use the AddConst visitor in the previous question to facilitate the coding. 15 points
Linear Recursive Structures
5. Write a visitor for an AListFW called LastNElements to compute and return an AListFW that contains the last n elements of the host. Here 0 <= n, and the case n equals 0 corresponds to the empty list. When n is more than the number of elements in the list, throw an appropriate exception. Decide how to pass n to LastNElements yourself. 20 points
6. Write a visitor for an LRStruct called TruncateN to truncate the host and keep only the first n elements of the host. Here 0 <= n, and the case n equals 0 corresponds to the empty list. When n is more than the number of elements in the list, the host should remain unchanged. Decide how to pass n to TruncateN yourself. 20 points