JAVA Fourier Synthesis Program - Compile Problems

Started by A Twig, March 15, 2007, 11:44:23 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

A Twig

This program I've written won't compile  - according to my tutor its something really obvious. I've spent hours on the bloody thing - any ideas anyone?

Basically you input the parameters, and the program computes the resulting waveform values into an array that can be plotted.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class FourierInput
{

public static void main(String[] args) throws IOException {
 InputStreamReader reader = new InputStreamReader(System.in);
      BufferedReader console = new BufferedReader(reader);
       System.out.println("Choose case to be run (1-5):");
      String input = console.readLine();
      int Case = Integer.parseInt(input);

      System.out.println("Choose an integer number of intervals (t):");
      String input2 = console.readLine();
      double time = Double.parseDouble(input2);
     
      double T = 0.74;
     
      double interval= ((3.0*T)/time);
     
      double[] Solution;
      double[] AnArray;
      double[] BnArray;

      Solution = new double[(int)time];
      AnArray = new double[(int)time];      
      BnArray = new double[(int)time];

      double[] TimeArray;
      TimeArray = new double[(int)time];
      for(r=0; r          if (time==0.0) { TimeArray[(int)r] = (-T);}
          else {TimeArray[(int)r] = (TimeArray[(int)(r-1)]+ interval);
            }
     
switch (Case) {
    case 1:
    double i;
          for(i=0.0; i<(time); i++){
              if (i==1.0) { BnArray[(int)i] = (1/(i+1));}
              else if(i==2.0) { BnArray[(int)i] = (0.0);}
              else if(i==3.0) { BnArray[(int)i] = (1/(i+1));}
              else if(i==4.0) { BnArray[(int)i] = (0.0);}
              else if(i==5.0) { BnArray[(int)i] = (1/(i+1));}
              else if(i==6.0) { BnArray[(int)i] = (0.0);}
              else if(i==7.0) { BnArray[(int)i] = (1/(i+1));}
              else if(i==8.0) { BnArray[(int)i] = (0.0);}
              else if(i==9.0) { BnArray[(int)i] = (1/(i+1));}
              else if(i==0.0) { BnArray[(int)i] = (0);}
           
            }
           Solution=Fourier(time, AnArray, BnArray, interval, T);          
    break;
    case 2: double i;
          for(i=0.0; i<(time); i++){
              if (i==1.0) { AnArray[(int)i] = (1/(i+1));}
              else if(i==2.0) { AnArray[(int)i] = (0.0);}
              else if(i==3.0) { AnArray[(int)i] = (1/(i+1));}
              else if(i==4.0) { AnArray[(int)i] = (0.0);}
              else if(i==5.0) { AnArray[(int)i] = (1/(i+1));}
              else if(i==6.0) { AnArray[(int)i] = (0.0);}
              else if(i==7.0) { AnArray[(int)i] = (1/(i+1));}
              else if(i==8.0) { AnArray[(int)i] = (0.0);}
              else if(i==9.0) { AnArray[(int)i] = (1/(i+1));}
              else if(i==0.0) { AnArray[(int)i] = (0);}
           
            }
           Solution=Fourier(time, AnArray, BnArray, interval, T);
    break;
    case 3: double i;
          for(i=0.0; i<(time); i++){
              if (i==1.0) { BnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==2.0) { BnArray[(int)i] = (0.0);}
              else if(i==3.0) { BnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==4.0) { BnArray[(int)i] = (0.0);}
              else if(i==5.0) { BnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==6.0) { BnArray[(int)i] = (0.0);}
              else if(i==7.0) { BnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==8.0) { BnArray[(int)i] = (0.0);}
              else if(i==9.0) { BnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==0.0) { BnArray[(int)i] = (0);}
           
            }
           Solution=Fourier(time, AnArray, BnArray, interval, T);
    break;
    case 4: double i;
          for(i=0.0; i<(time); i++){
              if (i==1.0) { AnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==2.0) { AnArray[(int)i] = (0.0);}
              else if(i==3.0) { AnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==4.0) { AnArray[(int)i] = (0.0);}
              else if(i==5.0) { AnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==6.0) { AnArray[(int)i] = (0.0);}
              else if(i==7.0) { AnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==8.0) { AnArray[(int)i] = (0.0);}
              else if(i==9.0) { AnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==0.0) { AnArray[(int)i] = (0);}
            }
           Solution=Fourier(time, AnArray, BnArray, interval, T);
    break;
    case 5: double i;
          for(i=0.0; i<(time); i++){
              if (i==0.0) { AnArray[(int)i] = (3.14159/2);}
              else { AnArray[(int)i] = (0.0);}
                     }
                     
          for(i=1.0; i<(time); i++){
              BnArray[(int)i]=(1/i);}
            Solution=Fourier(time, AnArray, BnArray, interval, T);
    break;
   
        default: System.out.println ("Case Error: Case doesn't exist - run the program again selecting a case from 1 - 5");
}
}
}


public static double Fourier(double time, [] AnArray, [] BnArray, double interval, double T){
           double interval= ((3*T)/time)
           double[] TArray= new double[(int)time];
           double Calc= 0.0;
           double omega = ((2.0*3.14159)/T);
           for(int a=0; a<(time); a++){
               TArray[a]=(-T+(a*interval))
            }
               
          for ( int q=0; q<(time); q++) {
            for ( int c=0;c<(q); c++) {
              Calc=(Calc + (AnArray[c]*Math.cos(time*omega*TArray[c])))}
                Solution[q]=(AnArray[0] + Calc}

    for (double item : Solution) {
             System.out.println("Value: " + item);}
}
}
}
[N~@] - Ninja Association
Although we may fade from life, life does not fade from our memories


Jamoe

One of the guys at work does a lot of Java so I've sent him an email, if hes got time he will likely take a look.

Bob

You have a ton of compilation errors in that one Twig. It is too many for me to explain 'em all, but here is a screen shot from eclipse where you can see 'em all:



As a couple of examples:
The "r cannot be resolved" is due to you have written the for-statement wrong - it should look like this: for(int r=0; r (you had forgotten the 'int' before r).
The "Duplicate local variable i' is because you in the switch-case stamenent re-declare i each single time - either use another variable name, or remove double if you intend to use the same variable.
There is also an error in the signature of the Fourier function, due to that you have not specified which kind of array the different parrameters are (you must say int[] AnArray, and not just [] AnArray).

There are also some more strange stuff (think some variables are not reachable cause you mix a bit with static methods and non-static variables), but I haven't time to check out the details at the monent (as you can see from the rest of my development environment, I'm in the middle of something :flirty:).


But also just as a side note: you said the problem is that it doesn't compile. If you use and IDE when developing (such as Eclipse (as I use right now) or NetBeans), they will find syntax errors for you, so the only thing you have to worry about is "logical" errors.
I just hope you don't try writing code in TextPad or something - I had to do that for one semester earlier in my education, and that truly sucks horse ass :sideways:


Hope this helped at least a bit :)
[imga=right]http://77.108.135.49/fahtags/ms10.jpg[/imga]* Threbrilith the Nightelf, born and raised by the Silver Oak Guardians *
Proud member of Dead Men Walking

spiritus

i'm with bob here. had a little look at it. and i don't have netbeans here.
was a biig code to read. and netbeans are freeware if i remember correctly. had to use textpad also. that sucks.
but here you go: http://www.netbeans.org/
enjoy :D
 
edit: didn't see bobs links.. bah.
bob rocks!
                   
Regards: Spiritus
----------------------------------------------
In nomine Patris et Filii et Spiritus Sancti!
----------------------------------------------

A Twig

Yeah, I've been using BlueJ which seems a bit poo to be honest. Cheers for the pointers - proof that large amounts of bourbon and JAVA don't mix...
[N~@] - Ninja Association
Although we may fade from life, life does not fade from our memories


A Twig

Ok, I've cleared it up, but having difficulties sending my Arrays to the function. It said error "found double, expected double[]" and so I whacked in the square brackets, and now it says ".class expected"

I reckon I'm defining something wrong, again its probably a really obvious thing, but JAVA really isn't my strong point. Neither's maths. In fact come to think of it my whole degree isn't really my strong point....

I think I'm gonna cry...

Anyway...

Solution=Fourier(time, AnArray[], BnArray[], interval, T);

Then goes to this:

public static double Fourier(double time, double[] AnArray, double[] BnArray, double interval, double T){
           double interval= ((3*T)/time);
           double[] TArray= new double[(int)time];
           double Calc= 0.0;
           double omega = ((2.0*3.14159)/T);
           for(int a=0; a<(time); a++){
               TArray[a]=(-T+(a*interval));
            }
               
          for ( int q=0; q<(time); q++) {
            for ( int c=0;c<(q); c++) {
              Calc=(Calc + (AnArray[c]*Math.cos(time*omega*TArray[c])));}
                Solution[q]=(AnArray[0] + Calc);}

    for (double item : Solution) {
             System.out.println("Value: " + item);}
}
[N~@] - Ninja Association
Although we may fade from life, life does not fade from our memories


Jamoe

The only thing i have noticed is that you declare the function Fourier to return a double but that function isn't actually returning anything. Its been along time since I did any java but is that going to cause a problem.

also use the code tags to print code;)

public static double Fourier(double time, double[] AnArray, double[] BnArray, double interval, double T)
{
    double interval= ((3*T)/time);
    double[] TArray= new double[(int)time];
    double Calc= 0.0;
    double omega = ((2.0*3.14159)/T);
   
    for(int a=0; a<(time); a++)
    {
        TArray[a]=(-T+(a*interval));
    }

    for ( int q=0; q<(time); q++)
    {
        for ( int c=0;c<(q); c++)
        {
            Calc=(Calc + (AnArray[c]*Math.cos(time*omega*TArray[c])));
        }
   
        Solution[q]=(AnArray[0] + Calc);
    }

    for (double item : Solution)
    {
        System.out.println(&quot;Value: &quot; + item);
    }
}

Bob

Also, I see you write  Solution=Fourier(time, AnArray[], BnArray[], interval, T);

If you're gonna that function as you defined it, you'll have to write something like this:
double[] anArray;
double[] bnbArray;

/* Initialize the arrays with the values they should hold */

Solution = Fourier(time, anArray, bnArray, interval, T);
And also as Jamoe said, the function Fourier must return a double!
[imga=right]http://77.108.135.49/fahtags/ms10.jpg[/imga]* Threbrilith the Nightelf, born and raised by the Silver Oak Guardians *
Proud member of Dead Men Walking

suicidal_monkey

Quote from: Jamoe;181254also use the code tags to print code;)
definitely helps! Java looks none too dissimilar to C ... sort of... or perhaps not! :rolleyes:
[SIGPIC].[/SIGPIC]

A Twig

Quote from: Bob;181255Also, I see you write  Solution=Fourier(time, AnArray[], BnArray[], interval, T);

If you're gonna that function as you defined it, you'll have to write something like this:
double[] anArray;
double[] bnbArray;

/* Initialize the arrays with the values they should hold */

Solution = Fourier(time, anArray, bnArray, interval, T);
And also as Jamoe said, the function Fourier must return a double!

Sorry to be a pina, I've done that, my Fourier function now returns the double array (can I do that?) The AnArray and BnArray are defined in each case from the switch statement.

Writing it as you've put it there gives me "incompatible types - found double, expected double[]"  error when compiling.

Total program is like this now:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class FourierInput
{

public static void main(String[] args) throws IOException {
 InputStreamReader reader = new InputStreamReader(System.in);
      BufferedReader console = new BufferedReader(reader);
       System.out.println("Choose case to be run (1-5):");
      String input = console.readLine();
      int Case = Integer.parseInt(input);

      System.out.println("Choose an integer number of intervals (t):");
      String input2 = console.readLine();
      double time = Double.parseDouble(input2);
     
      double T = 0.74;
     
      double interval= ((3.0*T)/time);
      double i;
      double[] Solution;
      double[] AnArray;
      double[] BnArray;

      Solution = new double[(int)time];
      AnArray = new double[(int)time];      
      BnArray = new double[(int)time];

      double[] TimeArray;
      TimeArray = new double[(int)time];
      for(int r=0; r          if (time==0.0) { TimeArray[(int)r] = (-T);}
          else {TimeArray[(int)r] = (TimeArray[(int)(r-1)]+ interval);
            }
     
switch (Case) {
    case 1:
   
          for(i=0.0; i<(time); i++){
              if (i==1.0) { BnArray[(int)i] = (1/(i+1));}
              else if(i==2.0) { BnArray[(int)i] = (0.0);}
              else if(i==3.0) { BnArray[(int)i] = (1/(i+1));}
              else if(i==4.0) { BnArray[(int)i] = (0.0);}
              else if(i==5.0) { BnArray[(int)i] = (1/(i+1));}
              else if(i==6.0) { BnArray[(int)i] = (0.0);}
              else if(i==7.0) { BnArray[(int)i] = (1/(i+1));}
              else if(i==8.0) { BnArray[(int)i] = (0.0);}
              else if(i==9.0) { BnArray[(int)i] = (1/(i+1));}
              else if(i==0.0) { BnArray[(int)i] = (0);}
           
            }
           Solution=Fourier(time, AnArray, BnArray, interval, T);          
    break;
    case 2:
          for(i=0.0; i<(time); i++){
              if (i==1.0) { AnArray[(int)i] = (1/(i+1));}
              else if(i==2.0) { AnArray[(int)i] = (0.0);}
              else if(i==3.0) { AnArray[(int)i] = (1/(i+1));}
              else if(i==4.0) { AnArray[(int)i] = (0.0);}
              else if(i==5.0) { AnArray[(int)i] = (1/(i+1));}
              else if(i==6.0) { AnArray[(int)i] = (0.0);}
              else if(i==7.0) { AnArray[(int)i] = (1/(i+1));}
              else if(i==8.0) { AnArray[(int)i] = (0.0);}
              else if(i==9.0) { AnArray[(int)i] = (1/(i+1));}
              else if(i==0.0) { AnArray[(int)i] = (0);}
           
            }
           Solution=Fourier(time, AnArray, BnArray, interval, T);
    break;
    case 3:
          for(i=0.0; i<(time); i++){
              if (i==1.0) { BnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==2.0) { BnArray[(int)i] = (0.0);}
              else if(i==3.0) { BnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==4.0) { BnArray[(int)i] = (0.0);}
              else if(i==5.0) { BnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==6.0) { BnArray[(int)i] = (0.0);}
              else if(i==7.0) { BnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==8.0) { BnArray[(int)i] = (0.0);}
              else if(i==9.0) { BnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==0.0) { BnArray[(int)i] = (0);}
           
            }
           Solution=Fourier(time, AnArray, BnArray, interval, T);
    break;
    case 4:
          for(i=0.0; i<(time); i++){
              if (i==1.0) { AnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==2.0) { AnArray[(int)i] = (0.0);}
              else if(i==3.0) { AnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==4.0) { AnArray[(int)i] = (0.0);}
              else if(i==5.0) { AnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==6.0) { AnArray[(int)i] = (0.0);}
              else if(i==7.0) { AnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==8.0) { AnArray[(int)i] = (0.0);}
              else if(i==9.0) { AnArray[(int)i] = ((-1*((i-1)/2))/((i+1)*(i+1)));}
              else if(i==0.0) { AnArray[(int)i] = (0);}
            }
           Solution=Fourier(time, AnArray, BnArray, interval, T);
    break;
    case 5:
          for(i=0.0; i<(time); i++){
              if (i==0.0) { AnArray[(int)i] = (3.14159/2);}
              else { AnArray[(int)i] = (0.0);}
                     }
                     
          for(i=1.0; i<(time); i++){
              BnArray[(int)i]=(1/i);}
            Solution=Fourier(time, AnArray, BnArray, interval, T);
    break;
   
        default: System.out.println ("Case Error: Case doesn't exist - run the program again selecting a case from 1 - 5");
}
}
}


public static double Fourier(double time, double[] AnArray, double[] BnArray, double interval, double T){
           double interval= ((3*T)/time);
           double[] TArray= new double[(int)time];
           double Calc= 0.0;
           double omega = ((2.0*3.14159)/T);
           for(int a=0; a<(time); a++){
               TArray[a]=(-T+(a*interval));
            }
               
          for ( int q=0; q<(time); q++) {
            for ( int c=0;c<(q); c++) {
              Calc=(Calc + (AnArray[c]*Math.cos(time*omega*TArray[c])));}
                Solution[q]=(AnArray[0] + Calc);}

    for (double item : Solution) {
             System.out.println("Value: " + item);}
             
             return Solution;
}
}

[N~@] - Ninja Association
Although we may fade from life, life does not fade from our memories


Bob

The signature of the Fourier function need to look like this:
public static double[] Fourier(double time, double[] AnArray, double[] BnArray, double interval, double T)Notice the square brackets after public static double[] which are needed since you are returning an array of double.

You also write:
public static double[] Fourier(double time, double[] AnArray, double[] BnArray, double interval, double T){
           double interval= ((3*T)/time);
This will give you a Duplicate local variable interval error. Either you must remove double before interval= ((3*T)/time) if your intention is to use the variable which are passed to the function, or you need to choose another name for it.

You will also still have the error that Solution cannot be resolved inside the Fourier function, as you haven't declared this array in here.
[imga=right]http://77.108.135.49/fahtags/ms10.jpg[/imga]* Threbrilith the Nightelf, born and raised by the Silver Oak Guardians *
Proud member of Dead Men Walking

A Twig

#11
Brilliant, cheers mate. You're an absolute lifesaver. Just reckon there's something wrong with my maths bit somewhere, cos my entire solution array is zero :eyebrow:
 
Reckon that should just be a pretty simple tweak tho, thanks again, send me your address and I'll post you a beer!

 EDIT: Had forgotten the second half of the Fourier function. It's still not quite right, but it'll definitely get me a pass at least now!

EDIT EDIT: All fixed :D
[N~@] - Ninja Association
Although we may fade from life, life does not fade from our memories


Bob

Glad I could be of any help :)

But I think I'll pass on that beer - don't think it'll be too god after passing through the postal service...
But I'll remember it for dMw lan XII if you should show up there :cheers:
[imga=right]http://77.108.135.49/fahtags/ms10.jpg[/imga]* Threbrilith the Nightelf, born and raised by the Silver Oak Guardians *
Proud member of Dead Men Walking