Im trying to to create a simple 4 function calculator using a jump table without switch case or if/else statements. I understand I can create the jump table via function pointers but I am kindda blanking out. I've started with the addition and subtraction part of the program but am trying to grasp the design/strategy to use. I am also getting an error when putting the method in the array, so far this is what i have:

public class Calculator {

   public abstract class Functor{

    abstract double Compute();
    }

    class Addition extends Functor
    {
       double op1, op2;

       public Addition(double op1, double op2){
       this.op1 = op1;
       this.op2 = op2;
       }
        public double Compute(){ return op1 + op2;}
    }

    class Subtraction extends Functor
    {
        double op1, op2;

        public Subtraction(double op1, double op2)
        {
        this.op1 = op1;
        this.op2 = op2;
        }
        public double Compute(){ return op1 - op2;}
    }

    public static void main(String[] args) {
        Functor add = new Addition(); // Seems to be a problem here
        Functor [] jumpArray = new Functor [256];


    }

}

masijade is right, but I think I can see where yo are going with this, using subclasses of an abstract superclass to implement different methods with the same signatures. Then instead of an array of function pointers yo have an array of objects that implement the appropriate version of the functions. I think this will work better if you make the methods pure functions, ie pass the parameters into the compute method rather than use a constructor.
Your line 32 fails simply because you call a constructor with no args, but only define a constructor with 2 args.

Edited 5 Years Ago by JamesCherrill: n/a

so i've made a few changes to program last night that worked (ended up doing what JamesCherrill suggested) but is this what a jumptable in java is? (jumptable without having switch cases or if/else statements).

public class Calculator {

      public static abstract class Functor{

       public static abstract double Compute(double op1, double op2);
            }

   public static  class Addition extends Functor
    {

        public static double Compute(double op1, double op2){ return op1 + op2;}
    }

   public static class Subtraction extends Functor
    {

        public static double Compute(double op1, double op2){ return op1 - op2;}
    }



    public static void main(String[] args) {

        Functor add = new Addition();     
        Functor minus = new Subtraction();  

	System.out.println("sum is :" + add.compute(10,5));
	System.out.println("subtraction is :" + minus.compute(10,5));
    }

Edited 5 Years Ago by java beans: n/a

Not quite a table yet... you still need the array that you had in your first code

Functor[] fns = new Functor[2];
fns[0] = new Addition(); 
fns[1] = new Subtraction(); 

// do function "n" from table (n==0 | n==1)...
double result = fns[n].compute(20, 30);

ps: Java enums(not to be confused with the feeble "enum"s in certain other languages) give you a neater way to package and error-proof this exact construction. I can share a quick example if anyone is interested.

JamesCherrill,

sure the enums example would be greatly appreciated!

I am just reading a book on design patterns and trying to get a better understanding of the designs by actually practicing them so I figured this would be a great example. The book says there is a way to do this with interfaces(in this example im guessing 4, one for each mathematical operator) and an abstract class combined(if im understanding correctly)....but your right I still do need an array.

Java emuns are like abstract classes, and the values of the enum are like subclasses, so the pattern is the same. But enums handle all the initialisation, give you the array, as well as lookup by name, and ensure the rest of the "plumbing" works ok. Here's an eg:

enum Fns {
   ADD {
      int calc(int a, int b){return a+b;}
   },
   SUBTRACT {
      int calc(int a, int b){return a-b;}
   };  // etc
   abstract int calc(int a, int b);// calc must be implemented by each enum value
}
...
      // use an enum value to call the right method..
      System.out.println(Fns.ADD.calc(3,4));

      Fns[] fns = Fns.values(); // array of Fns in order that they were defined
      System.out.println(fns[0].calc(3,4)); // select function by index number
      System.out.println(fns[1].calc(3,4));
      // or just...
      System.out.println(Fns.values()[i].calc(3,4)); // i==0 | i == 1

      // call Fn by name,  
      // Enum.valueOf(Fns.class, str)  returns the enum value named by str
      System.out.println(Enum.valueOf(Fns.class, "ADD").calc(3,4));
This article has been dead for over six months. Start a new discussion instead.