• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object arrays in constructors

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
She's on vacation for two weeks.
 
Sal Velinus
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic