2011-11-03 57 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一部分。我把這個選擇留給你。