next up previous
Next: Static Members of Classes Up: The Command Pattern Previous: The Command Pattern

Exercises:

1.
Add a map method to the IntList class that takes an Operation and applies it to each element of this to produce a new IntList with exactly the same number of elements as this.
2.
Assume that a vector

< a0 , a1 , a2, ... , an >

is represented by the list

\begin{displaymath}{\tt\small (} a_0 \; a_1 \; a_2 \; ... \; a_n {\tt\small )}.
\end{displaymath}

where the coefficient's ai are objects of type Double. Add a method
double norm()
to IntList computes the norm by v by squaring the elements, adding the squares together and taking the square-root of the sum. You can compute the vector of squares using map and the define a method double sum() to compute the sum.
3.
Assume that a polynomial

a0 x + a1 x + a2 x2 + ... + an xn

is represented by the list

\begin{displaymath}{\tt\small (} a_0 \; a_1 \; a_2 \; ... \; a_n {\tt\small )}0
\end{displaymath}

where the coefficient's ai are objects of type Double. Write a method
double eval(IntList p, Double x)
to evaluate the polynomial at coordinate x. Use Horner' rule asserting that

\begin{displaymath}a_0 + a_1 x + a_2 x^2 + ... + a_n x^n = \\
a_0 + x \cdot (a_1 + x \cdot (a_2 + ... x \cdot a_n))
\end{displaymath}

Remember to use the structural design pattern for processing lists (and by association polynomials as we have represented them).


next up previous
Next: Static Members of Classes Up: The Command Pattern Previous: The Command Pattern
Corky Cartwright
1999-09-07