Results 1 to 7 of 7
Thread: Assign object to another object?
- 04-17-2011, 05:31 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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();
Java Code:Player player1 = new Player("John",21); game1.addPlayer(player1); //Here a new Results object for player1 should be created.
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.
-
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 { } }
- 04-17-2011, 06:06 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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.
-
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.
- 04-17-2011, 06:41 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Thanks, I guess it should be it since I missed a couple of lessons on collections, lists etc. I will look into it!
- 04-17-2011, 08:01 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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 }
-
But if Results is an interface all you had to do was implement it....
Java Code:class Player { class PlayerResults implements Results { } }
What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)
Similar Threads
-
Insert class file as object in a table & read the object from the blob.
By facemeguru in forum New To JavaReplies: 1Last Post: 02-02-2011, 07:11 PM -
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 05:29 AM -
How to assign unique id to each object created from same class
By srisar in forum New To JavaReplies: 2Last Post: 02-18-2010, 06:26 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 07:14 PM
Bookmarks