CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2008
    Posts
    1

    Stuck on assignment (due soon)

    We're making a sort of yahtzee-type dice game.
    There are two classes, Die, and Dice. Dice serves as a collection and means of manipulating multiple Die at a time.

    The class for the dice is relatively simple and unimportant at the moment storing a 'size' value, a 'value' value, along with set, get, and roll methods.

    In the 'Dice' Class, I add desired Die into an ArrayList, as I was told to.
    Adding Die to this list is not difficult.

    However, I am also to be able to add Die from a and existing 'Dice' object.

    Here's what I've got so far.


    ...
    public void add( Die dieToAdd )
    {
    diceArray.add( dieToAdd );
    }
    ...


    This part seems to work fine. (I've already declared the ArrayList a while back.)
    Now here is where I get problems.


    ...
    public void add( Dice diceToAdd )
    {
    Collections.copy(diceToAdd.diceArray,this.diceArray);
    }
    ...


    Why this does not work is beyond me. Please help. Much appreciated.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Stuck on assignment (due soon)

    this does not work
    Are there error messages? Please copy and paste them here. The text of the message will say what's wrong.
    Norm

  3. #3
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Stuck on assignment (due soon)

    The question is too generic. Please, post the error message at least. It would be perfect if you attach both of your classes as well tho.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  4. #4
    Join Date
    Dec 2007
    Posts
    13

    Re: Stuck on assignment (due soon)

    you'd better put your full source code here. So that we can see the exact problem.




    ---------------------------------------
    www.codeuu.com

  5. #5
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Stuck on assignment (due soon)

    Quote Originally Posted by nimm02
    Why this does not work is beyond me.
    And what doesn't work is beyond everybody else.

    Maybe the problem is that Collections.copy doesn't make a deep copy, only a shallow one. This means that no actual copies of the Dice objects are created when they're copied to the new ArrayList. Only the references to the objects are copied, not the objects themselves. After the copy you have two ArrayLists referencing the same objects.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured