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.
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.
Joined: Mar 05, 2002
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.
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.