File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object arrays in constructors

 
Sal Velinus
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is code for a class Computer. MemoryChip is another class, it works fine. I'm having trouble trying to figure out how to work with the arrays of MemoryChip in the constructors and with method addAChip(). I don't like writing duplicate code, but maybe I'll have to in spots. Will the constructor have to call addAChip() twice, and addAChip() have to call setRam()? That doesn't seem right.
Thanks for any help.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg,
It is not clear what you are trying to do with the MemoryChip. If you are just trying to give the computer some amount of memory then I would probably just pass in an integer for the amount of chips instead of passing in the actually MemoryChip. Something like the following:

If your addAChip method is supposed to give some default value to the new chip then you can go ahead and call it inside the loop after the initialization line that already exists.
Regards,
Manfred.
 
Sal Velinus
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that would no doubt be a good solution, but I have to code according to a UML diagram I was given that includes all the methods, variables and parameters. I can add to it, or change to more advanced stuff like lists, but can't take away. The trouble is with the constructor with MemoryChip parameters, and the setRam() and addAChip methods(). To quote the UML, addAChip() is a:
Convenience method that takes the chip object that was passes in and adds it to the array at the index specified as the 2nd parameter.
Some of these problems may be my design thinking. I think all constructors here should chain to the final constructor with this calls, so all the work is done there. ram should only be set in setRam(). But where does the parameter MemoryChip[] chips come from in the constructor to pass to setRam()? It can't be ram, because that's what I'm setting. Do I have to create a new MemoryChip[]? Does setRam() call addAChip()? This is where I'm getting stuck.
Thanks for all help, I appreciate it.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not ask the person who gave you the UML diagram what he/she had in mind.
 
Sal Velinus
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
She's on vacation for two weeks.
 
Sal Velinus
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, this is what I came up with, and it works, but seems a sloppy way to do it. It seems odd to declare another array in the constructor, and to call addAChip twice. Any ideas on a better way? setRam() seems redundant as well. Thanks.

[ April 02, 2002: Message edited by: Greg Adams ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic