This forum is in archive mode. You will not be able to post new content.

Author Topic: Need help with code (object array?)  (Read 2176 times)

0 Members and 1 Guest are viewing this topic.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Need help with code (object array?)
« on: 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?

Offline Schalla

  • VIP
  • Peasant
  • *
  • Posts: 81
  • Cookies: 29
    • View Profile
Re: Need help with code (object array?)
« Reply #1 on: August 20, 2014, 06:53:12 PM »
Your c is defined as array of doubles, not as array of instances of Complex, isn't it?

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Need help with code (object array?)
« Reply #2 on: August 20, 2014, 07:13:06 PM »
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.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Need help with code (object array?)
« Reply #3 on: August 20, 2014, 07:17:40 PM »
Your c is defined as array of doubles, not as array of instances of Complex, isn't it?

Never mind. Found a solution.


Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Need help with code (object array?)
« Reply #4 on: August 20, 2014, 08:37:24 PM »
Could you please elaborate on how you managed to solve it?

Offline Schalla

  • VIP
  • Peasant
  • *
  • Posts: 81
  • Cookies: 29
    • View Profile
Re: Need help with code (object array?)
« Reply #5 on: August 20, 2014, 09:13:19 PM »
He learned how to declare an Array of an Object.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Need help with code (object array?)
« Reply #6 on: August 20, 2014, 09:41:54 PM »
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.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Need help with code (object array?)
« Reply #7 on: August 22, 2014, 11:08:39 AM »
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.

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.