EvilZone
Programming and Scripting => Java => : DreX August 20, 2014, 06:15:15 PM
-
The program is supposed to manipulate complex numbers.
On the second look I think "c" should be some kind of object array. That "c" is populated with objects.
But how do I write that?
-
Your c is defined as array of doubles, not as array of instances of Complex, isn't it?
-
Your c is defined as array of doubles, not as array of instances of Complex, isn't it?
I think that is the problem. But I don't know how to define it as instances of Complex.
-
Your c is defined as array of doubles, not as array of instances of Complex, isn't it?
Never mind. Found a solution.
-
Could you please elaborate on how you managed to solve it?
-
He learned how to declare an Array of an Object.
-
The problem of the first post was actually that DreX tried to define a variable with the same name in the same method. Although the variable was in a subblock, Java doesn't allow this kind of variable hiding like some other languages do. You can only hide, e.g., class variables in a method, but not other variables within the same method.
Btw, if you ever do something more computationally intensive with complex numbers (something like the mandelbrot set), don't use objects for them. Work with primitives for the real and imaginary part only, or the performance will go downhill fast.
If that isn't case, stay with the objects, they are better readable and provide type safety.
-
Could you please elaborate on how you managed to solve it?
I think this is how I solved it:
-c is supposed to be an array of complex numbers. In the 1st try I declared c to be an array of doubles. Which would mean that c was populated with doubles (2.3 , 3.124.....) and not populated with objects. So in the 2nd try I changed c to be an array of Complex objects without length or any object already in it. In the populate method I first define how many objects I want (c.length) and in the for loop I define the objects one by one.