• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems initializing elements of object array  RSS feed

 
James Brown
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having this problem and I can't seem to find the cause of it. I have a Complex class that I created, it basically creates "complex" numbers that consist of a real and an imaginay part. Here is an example: (45 + 3i). The 45 is a real number, and the 3i is the imaginary part. The "i" is always there and never changes, so the class only needs 2 parameters, 2 int's. Well, here is the Complex class:


Now, I made a ComplexDriver to prove that the Complex class works. The real ComplexDriver has a a lot of buttons, each one add, subtracts, compares, etc. different Complex objects in an array (at the beginning of the application an array consisting of Complex objects is created). It even has one button that initializes a specific array element with the values the user puts in. My problems began because this specific button, which used a method to initialize the array element, didn't work. Instead of initializing only a specific element, it initialized all the previous elements with the value that the user gave. Example: if I had already initialized the first element in the array with a (2 + 4i) value, and then I decided to initialize the second element in the array with (3 + 3i), I would then display all the elements in the array (the ones that where already initialized) and to my surprise the first and second elements had a (3 + 3i) value! I then decided to simplify the ComplexDriver so it would be easier to track the "bug" down. Well I already did that and I still get the same problem! The funny thing is that the program itself initializes the elements every time (the user doesn't get to do this), and I stilll have the same problem. Ok, here is the ComplexDriver:
 
James Brown
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If you guys can understand my problem and help me solve it, I would really appreciate it!!! Thanks for your help!!!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The problem is that you have statics holding the state of your complex object, which means there can only be one for all instances of the class and its subclasses. So take out the static modifier and it will probably do what you want. Also, it may be easier to understand if you changed the names to something more representative of the class:

[ February 14, 2004: Message edited by: Michael Morris ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, you most likely don't want anything to be static in your complex number class. you should go through and get rid of the "static" keyword before all your method definitions as well.
In my opinion, static should only be used when you know you need it. At this point, I would recommend that you only use the word static with main(). Don't use it anywhere else.
Also, as mentioned above, you should use names that show what a variable or method does. You may want to change the names of methods like setReal2() and getReal2().
HTH
Layne
[ February 14, 2004: Message edited by: Layne Lund ]
 
James Brown
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys!!! I removed all the static references (except in main) and the program ran perfectly!!! I also changed the variable's names. Once again, thanks for all the help.
[ February 15, 2004: Message edited by: James Brown ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just gotta ask ... do you feel good now? (doodle-oodle-oodle-oop) In Smalltalk you could use a brand new bag instead of an object array.
Sorry. Couldn't stop mice elf.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!