summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/FloatTables.java
blob: 25ea516a1cd47a5a246be6324ad501e8984a5352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* FloatTables.java */

import java.lang.reflect.*;

public class FloatTables
{
  public static double times2(double value)
  {
    return 2 * value;
  }

  public static double sqr(double value)
  {
    return value * value;
  }

  public static void printTable(String methname)
  {
    try {
      System.out.println("Wertetabelle fuer " + methname);
      int pos = methname.lastIndexOf('.'); 
      Class clazz;
      if (pos == -1) {
        clazz = FloatTables.class;
      } else {
        clazz = Class.forName(methname.substring(0, pos));
        methname = methname.substring(pos + 1);
      }
      Class[] formparas = new Class[1];
      formparas[0] = Double.TYPE;
      Method meth = clazz.getMethod(methname, formparas);
      if (!Modifier.isStatic(meth.getModifiers())) {
        throw new Exception(methname + " ist nicht static");
      }
      Object[] actargs = new Object[1];
      for (double x = 0.0; x <= 5.0; x += 1) {
        actargs[0] = new Double(x); 
        Double ret = (Double)meth.invoke(null, actargs);
        double result = ret.doubleValue();
        System.out.println("  " + x + " -> " + result);
      }
    } catch (Exception e) {
      System.err.println(e.toString());
    }
  }

  public static void main(String[] args)
  {
    printTable("times2");
    printTable("java.lang.Math.exp");
    printTable("sqr");
    printTable("java.lang.Math.sqrt");
  }
}