Results 1 to 7 of 7
  1. #1
    Marvin is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Assign object to another object?

    So let's say I have following classes -- Game, Player, Results. I can add new players to a game via Game class. The Player class just contains some variables like name and age. Results class can for example start and stop a timer, and then get the result. Separate timers and results for each player.

    What I need is a new Results object to be created each time I add a new player to my Game. So that later I can start and stop timer for each of my players.

    So for example I create a new game:

    Java Code:
    Game game1 = new Game();
    Then create a player and add it to my game:

    Java Code:
    Player player1 = new Player("John",21);
    game1.addPlayer(player1); //Here a new Results object for player1 should be created.
    How would I create a new Results object for each new player? After I've done so, how can I check results of a certain player?

    I imagine it would look something like: (res1 would be a Results object that was created automatically in addPlayer method of Game class)

    Java Code:
    player1.res1.startTimer();
    player1.res1.stopTimer();
    player1.res1.getResults();

    But I am not sure how it is done in Java.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    each Player has its own Results right?

    so nest Results into Player class, then make results happen in the constructor, something like this:

    Java Code:
    class Player {
        private Results playerResults;
        //Player constructor
        Player() {
            playerResults = new Results().start();
        }
        //get results
        getResults() {
            return playerResults;
        }
    
        class Results {
        }
    }

  3. #3
    Marvin is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Good idea, but I forgot to mention that it's a school assignment and we get finished interfaces that we have to implement in those 3 classes. And all the functions like getResults() or start() are inside the Results interface and have to stay there.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Another way to associate a single Results object with a single Player object is to add to your Game class a HashMap<Player, Result>. Please check out the tutorial for this. If you are going to go this route though, be sure that Player has both a decent hashcode method and equals method and that no two players can have different hashcodes yet still be equal.

  5. #5
    Marvin is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks, I guess it should be it since I missed a couple of lessons on collections, lists etc. I will look into it!

  6. #6
    Marvin is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Hmm, okay so I have a HashMap inside my Game class:

    Java Code:
    public Map<Player, Results> m = new HashMap<Player, Results>();

    addPlayer method in the Game class:

    Java Code:
    public void addPlayer(Player p)
    {
              Results res = new Results();
              m.put(p, res);
    }

    There is a method in my Results class that should return the player linked to the current Results object:

    Java Code:
    public Player getPlayer()
    {
    	// TODO
    }
    I have no idea how to make the Results class 'know' which player it is linked to, since HashMap is inside the Game class.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    But if Results is an interface all you had to do was implement it....

    Java Code:
    class Player {
        class PlayerResults implements Results {
            
        }
    }
    If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
    What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2011, 07:11 PM
  2. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  3. Replies: 2
    Last Post: 02-18-2010, 06:26 PM
  4. Replies: 1
    Last Post: 03-04-2009, 07:14 PM

Posting Permissions

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