Some Examples using Polymorphism

 

Here is a robot that can compute the function y=2x. Note that it has no if or while structures anywhere.

It depends on a ComputeStrategy interface.

And here is a driver for it. The use of the for loop in the driver is inessential.

Here is everything in a zip

An easy exercise is to compute other linear functions using this technique. A harder one is to compute a polynomial function, but it can be made to work. First try just y = x^2.


Last Updated: October 12, 2003