An interface based plotter

exercise No. 165


In the section called “Plotting functions” you implemented a plotter hard coding the desired plot function e.g. y = sin(x).

A better solution will separate the plotter's implementation from the desired plotting function. The idea is defining a corresponding interface:

public interface DoubleOfDouble {
    * A function expecting a double argument and returning
    * a double value like e.g. double Math.sin(double)
    * @param x An argument
    * @return A value
   public double compute(double x);

This interface may be used by the plotter class as a placeholder for the intended plot function. Plotting e.g. y=sin(x) will then be effected by:

class MySin implements DoubleOfDoubleFunction {

   public double compute(double x) {
      return Math.sin(x);

Plotting will then require an instance:

final DoubleOfDoubleFunction sinFunction = new MySin();


No template of mode='make' defined for element'para'

The solution in addition contains a variant DriverLambda using Java 8 lambda expressions which allows for supplying functions as arguments to the plotting facility. This solution will not be covered in the current lecture but you may catch a glimpse with respect to upcoming topics in Softwareentwicklung 2:

public class DriverLambda {

     * @param args Unused
    public static void main( String[] args ) {
        final Plotter plotter = new Plotter();

        plotter.setNumTics(80, 40);// 80 characters vertical, 40 characters horizontal
        plotter.setXrange(0, 2 * Math.PI);
        plotter.setYrange(-1, 1);

        // Function implementing the underlying interface
        // de.hdm_stuttgart.mi.sd1.plot.DoubleOfDoubleFunction
        // are being conveniently passed as arguments.
        plotter.plot(x -> Math.sin(x));
        plotter.plot(x -> Math.cos(x));