0

I'm writing a Java program for school (so: strict rules to it...) to graphically visualize mathematical function (like Sin, Cosine and Tangent, to make an example).

I have two interfaces: Function and DifferentiableFunction, and every mathematical function is stored in a class that extends Function or DifferentiableFunction.

When I launch the program, I have a textfield in which I need to write the name of the mathematical function (they will be added by the professor as a test) and my idea is to be able to reference the class name by the Text Field input.

The problem is: after days of searching, I really don't know how to do this thing, or even if it is possible.

Please help me, I'm stuck and the deadline is near!

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Raion
  • 3
  • 2
  • If you're only expected to model a few functions, wouldn't a [JComboBox](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html) be more appropriate than a textfield? – PakkuDon Jun 03 '14 at 08:56
  • Yeah, I know that a JComboBox would be more appropriated, but the problem is the same: how can I retrieve the list of classes and use them? As a side note, this is a school project, and the teacher gave us very strict rules... and the list of functions is completely casual, we don't know how many functions (or which functions) the teacher will test... PureSadism(TM) – Raion Jun 03 '14 at 08:58
  • @pintxo maybe but it depends on the level a prebuilt 2D array (lookup table) of names and objects may be more what is wanted for a school project. Depends on the school – Caleryn Jun 03 '14 at 09:00
  • http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html gives a lot of good examples of how to load classes dynamically. – Pphoenix Jun 03 '14 at 09:06
  • Thank you to everyone that answered! I will look at every suggestion! :) – Raion Jun 03 '14 at 09:23

1 Answers1

3

Yes it's (reasonably) possible. While I would prefer to use a JComboBox, if you can't you can't...

There are problems. Your naming style may not match what is input, so you will need to be considerate to that fact...

Basically, what you need is the text from the JTextField and a prefix that acts as the package name of the functions.

Then you can use Class.forName to find the class and Class#newInstance to create a new instance of the class.

From there you need to determine which interface the class supports, for example...

try {
    Class<?> clazz = Class.forName(packagePrefix + "." + functionName);
    Object instance = clazz.newInstance();
    if (instance instanceof DifferentiableFunction) {
        //...
    } else if (instance instanceof Function) {
        //...
    }
} catch (ClassNotFoundException ex) {
    ex.printStackTrace();
} catch (InstantiationException ex) {
    ex.printStackTrace();
} catch (IllegalAccessException ex) {
    ex.printStackTrace();
}

Now, as I said, this is incredibly error prone, so you need to take care

Another approach might be to load an instance of each class into some kind of Map and use that to look up the function you need.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Thank you! I'll try this approach and see what comes out! I'm even happy because I learned how to use try{} catch{} a couple days ago and I'm so happy about it that I look for any excuse to use it :D – Raion Jun 03 '14 at 09:13
  • Remember, Java's case sensitive, so you have to get the function name absolutely right – MadProgrammer Jun 03 '14 at 09:16
  • That's not a problem. All the mathematical classes will be added by the teacher in a specific package (given by him), always with the same name, and he will try them (so if he makes a mistake writing the class name, it's his own fault). If I understand correctly how this code works, I will change to a JComboBox (even if he doesn't want) that will retrieve exactly all the names to minimize the chance of errors and mistakes. Thank you very much! – Raion Jun 03 '14 at 09:21