2011-11-03 55 views
0

在这个论坛的一些非常好的人的帮助下,我已经能够将一些C++翻译成java语言,但我不知道如何调用这个类。无论如何,他们应该做的是返回一个“gen4风格”曲线。如果有人有一个想法如何让这个运行,请让我知道!java类问题

/* 
Derived from gen4 from the UCSD Carl package, described in F.R. Moore, 
"Elements of Computer Music." It works like setline, but there's an 
additional argument for each time,value pair (except the last). This 
arg determines the curvature of the segment, and is called "alpha" in 
the comments to trans() below.       -JGG, 12/2/01 

http://www.music.columbia.edu/cmc/rtcmix/docs/docs.html (maketable/gen4) 



trans(a, alpha, b, n, output) makes a transition from <a> to <b> in 
<n> steps, according to transition parameter <alpha>. It stores the 
resulting <n> values starting at location <output>. 
alpha = 0 yields a straight line, 
alpha < 0 yields an exponential transition, and 
alpha > 0 yields a logarithmic transition. 
All of this in accord with the formula: 
output[i] = a + (b - a) * (1 - exp(i * alpha/(n-1)))/(1 - exp(alpha)) 
for 0 <= i < n 
*/ 





import java.lang.Math; 
private static final int MAX_POINTS =1024; 

public class gen{ 
    int size;    /* size of array to load up */ 
    int nargs;   /* number of arguments passed in p array */ 
    float []pvals;  /* address of array of p values */ 
    double []array;  /* address of array to be loaded up */ 
    int slot;   /* slot number, for fnscl test */ 
} 

public static void fnscl(gen g) { 
} 

static void trans(double a, double alpha, double b, int n, double[] output) { 
    double delta = b - a; 

    if (output.length <= 1) { 
     output[0] = a; 
     return; 
    } 
    double interval = 1.0/(output.length - 1); 
    if (alpha != 0) { 
     double denom = 1/(1 - Math.exp(alpha)); 
     for (int i = 0; i < output.length; i++) 
      output[i] = a + (1 - Math.exp(i * alpha * interval)) * delta * denom; 
    } else { 
     for (int i = 0; i < output.length; i++) 
      output[i] = a + i * delta * interval; 
    } 
} 

public static double gen4(gen g) { 

    int i; 
    int points = 0; 
    int seglen = 0; 

    double factor; 
    double time [] = new double[MAX_POINTS]; 
    double value [] = new double[MAX_POINTS]; 
    double alpha [] = new double[MAX_POINTS]; 
    double ptr []; 

    if (g.nargs < 5 || (g.nargs % 3) != 2) /* check number of args */ 
     System.out.println("gen4 usage: t1 v1 a1 ... tn vn"); 

    if ((g.nargs/3) + 1 > MAX_POINTS) 
     System.out.println("gen4 too many arguments"); 

    for (i = points = 0; i < g.nargs; points++) { 
     time[points] = g.pvals[i++]; 

     if (points > 0 && time[points] < time[points - 1]) 
      System.out.println("gen4 non-increasing time values"); 

     value[points] = g.pvals[i++]; 
     if (i < g.nargs) 
      alpha[points] = g.pvals[i++]; 
    } 

    factor = (g.size - 1)/time[points - 1]; 

    for (i = 0; i < points; i++) 
     time[i] *= factor; 

    ptr = g.array; 

    for (i = 0; i < points - 1; i++) { 
     seglen = (int) (Math.floor(time[i + 1] + 0.5) 
       - Math.floor(time[i] + 0.5) + 1); 
     trans(value[i], alpha[i], value[i + 1], seglen, ptr); 
     ptr[i] += seglen - 1; 
    } 

    fnscl(g); 
    return 0.0; 
} 
+3

如果内存为我提供了正确的服务,那么您无法在Java中使用自由函数。一切都必须是一个单一类的(也许是静态的)成员函数,并且类名必须与*文件*名相同(gasp!)。我认为你的代码中没有外部类。 –

+0

请提及调用的主要方法和顺序。 – Santosh

+0

对不起,我忘了提及这个班是另一班的成员! – menemenemu

回答

1

如果我正确理解你的问题并且想要执行你的程序,那么你需要对代码进行一些调整。

你需要有一个班级。要执行它,你需要一个特殊的主要方法。

/** 
*Derived from... 
*/ 
import java.lang.Math; 

class Gen4Func { 
    class Gen { 
     // insert from question 
    } 

    public static void main(String[] args) { 
     // prepare parameters 
     // ... 

     // call your function 
     trans(...); 

     // do more stuff 
     // ... 
    } 

    public static void fnscl(gen g) { 
    } 

    static void trans(double a, double alpha, double b, int n, double[] output) { 
     // insert from above 
    } 
} 

将其保存到Gen4Func.java(必须与类名称匹配)。然后通过执行

> java Gen4Func 

正如我所说:如果我正确理解你的问题。

HTH,
迈克

+0

谢谢抱歉,我忘了提及这个班是会员。 – menemenemu

0

Java是在范式方面完全面向对象的语言。 (不像C++也是程序性的),这就是Kerrek SB所说的所有方法必须在类中声明和定义的原因。 他误解了一个文件只能包含一个公共类,它必须完全按照文件命名。但它也可以包含任意数量的具有任意名称的非公共类。

要运行该程序,必须首先使用javac [filename] .java编译该文件,然后使用java [classname]不带! .class

还有一件事。你不能声明这样的方法:

public void foo(); 

编译器会认为它是一个抽象方法并引发错误消息。

+0

您是否可以在公共类之外拥有非公共类,或者只有嵌套类?我的Java有点生疏... –

+0

外面也是。我认为这总是可能的,后来的内部类是1.1,如果我没有记错的话。 – zeller

0

大规模的复制到Gen.java,并尝试在main()方法中使用测试值设置Gen类字段。

/* 
* Derived from gen4 from the UCSD Carl package, described in F.R. Moore, 
* "Elements of Computer Music." It works like setline, but there's an additional 
* argument for each time,value pair (except the last). This arg determines the 
* curvature of the segment, and is called "alpha" in the comments to trans() 
* below. -JGG, 12/2/01 
* 
* http://www.music.columbia.edu/cmc/rtcmix/docs/docs.html (maketable/gen4) 
* 
* 
* 
* trans(a, alpha, b, n, output) makes a transition from <a> to <b> in <n> 
* steps, according to transition parameter <alpha>. It stores the resulting <n> 
* values starting at location <output>. alpha = 0 yields a straight line, alpha 
* < 0 yields an exponential transition, and alpha > 0 yields a logarithmic 
* transition. All of this in accord with the formula: output[i] = a + (b - a) * 
* (1 - exp(i * alpha/(n-1)))/(1 - exp(alpha)) for 0 <= i < n 
*/ 
public class Gen { 

    private static final int MAX_POINTS = 1024; 
    int size;    //size of array to load up 
    int nargs;   //number of arguments passed in p array 
    float[] pvals;  //address of array of p values 
    double[] array;  //address of array to be loaded up 
    int slot;   //slot number, for fnscl test 

    public static void main(String[] args) { 
     Gen g = new Gen(); 
     //initialize Gen fields here.. 
     Gen.gen4(g); 
    } 

    public static void fnscl(Gen g) { 
    } 

    public static void trans(double a, double alpha, double b, int n, double[] output) { 
     double delta = b - a; 

     if (output.length <= 1) { 
      output[0] = a; 
      return; 
     } 
     double interval = 1.0/(output.length - 1); 
     if (alpha != 0) { 
      double denom = 1/(1 - Math.exp(alpha)); 
      for (int i = 0; i < output.length; i++) { 
       output[i] = a + (1 - Math.exp(i * alpha * interval)) * delta * denom; 
      } 
     } else { 
      for (int i = 0; i < output.length; i++) { 
       output[i] = a + i * delta * interval; 
      } 
     } 
    } 

    public static double gen4(Gen g) { 
     int i; 
     int points = 0; 
     int seglen = 0; 

     double factor; 
     double time[] = new double[MAX_POINTS]; 
     double value[] = new double[MAX_POINTS]; 
     double alpha[] = new double[MAX_POINTS]; 
     double ptr[]; 

     if (g.nargs < 5 || (g.nargs % 3) != 2) /* 
     * check number of args 
     */ { 
      System.out.println("gen4 usage: t1 v1 a1 ... tn vn"); 
     } 

     if ((g.nargs/3) + 1 > MAX_POINTS) { 
      System.out.println("gen4 too many arguments"); 
     } 

     for (i = points = 0; i < g.nargs; points++) { 
      time[points] = g.pvals[i++]; 

      if (points > 0 && time[points] < time[points - 1]) { 
       System.out.println("gen4 non-increasing time values"); 
      } 

      value[points] = g.pvals[i++]; 
      if (i < g.nargs) { 
       alpha[points] = g.pvals[i++]; 
      } 
     } 

     factor = (g.size - 1)/time[points - 1]; 

     for (i = 0; i < points; i++) { 
      time[i] *= factor; 
     } 

     ptr = g.array; 

     for (i = 0; i < points - 1; i++) { 
      seglen = (int) (Math.floor(time[i + 1] + 0.5) 
        - Math.floor(time[i] + 0.5) + 1); 
      trans(value[i], alpha[i], value[i + 1], seglen, ptr); 
      ptr[i] += seglen - 1; 
     } 

     fnscl(g); 
     return 0.0; 
    } 
} 
+0

感谢D1e。初始化我只需要使用g(value,value ..)? – menemenemu

+0

它的基础知识,在Sun的教程中花费5分钟。初始化一个对象 - 你需要使用no-arg构造函数:Gen g = new Gen();.要初始化它的状态 - 你需要设置字段:g.size = 3;等等。 – JMelnik

+0

谢谢,我正在尝试,但我得到︰gen4 usage:t1 v1 a1 ... tn vn 异常在线程“main”java.lang.ArrayIndexOutOfBoundsException:-1 \t在Gen.gen4(Gen.java:94) \t at Gen.main(Gen.java:34) – menemenemu

0

在Java中,不允许使用独立方法。你应该让他们成为一些class。在你的情况下,你的方法似乎有2个使用class gen作为参数fnscl()gen4()。您可以将它们作为成员方法。另一个可以在class内保持static

public class gen{ 
    // data ... 

    public void fnscl() { ... } // member method 
    public double gen4() { ... } // member method 

    // static method 
    public static void trans(double a, double alpha, double b, int n, double[] output) { ... } 
} 

main()也应该是一些class一部分。我把这个选择留给你。