Results 1 to 14 of 14
  1. #1
    java_novice91 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Using Set in Java for putting all data in one collecion

    Hi,everyone! I am very very new in Java - I stage student and I have never done it before.

    Problem is that I need to do following( what is in bold!! other is beginning which I finished already):

    A local football club wishes to store information on its current squad of players:
    Each Player’s personal information (first name, last name, age, height and weight) should be recorder in a class Person. Each Player’s record for the season should be stored in a class Player. The information stored for each player consists of the number of games played and* goals scored. Write a class Goalkeeper which in addition records the number of penalties that the goalkeeper has saved. Provide appropriate methods for accessing this information. You should make appropriate use of inheritance in your solution.

    Information on all Players should be stored in a class Club. Club stores the name of the club and maintains information about all of it's players.

    As I said, I ve written Person's,Player's,Goalkeeper's classes with inheritance. Everything works fine, but now I have to put all info in Club class.

    I know I can use ArrayList or Sets (Collections) Am I right with that? But the problem is that I do not really get it, would be happy to receive any help(structure,order etc.)

    Thank you!

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    If I understand the requirements correctly it seems that all you really need is a new class Club with a String to hold the club's name and a collection that holds player objects. If you've written the other classes, this shouldn't be hard.

    PHP Code:
    public class Club {
      private String name;
      private "Collection with Player objects";
    
      // Constructors and methods.
    }
    Post your best attempt and ask specific questions about where you're stuck. I'm guessing it's with collections and how to use them? If so, check out the tutorials and you should get the hang of them quite fast, at least for a project of this size.

    As to which collection to use, it's really up to you and how you want the objects stored and how you wish to access them. Read up on the different collections and try to figure out which one suits your requirements and your application the best. There's no absolute right answer.
    Last edited by ojn; 05-03-2011 at 03:50 PM.

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

    Default

    What don't you get? Just use an ArrayList, its simpler to use, unless you have a reason to use a Set (i.e. no duplicates).

    Something like this should do:

    Java Code:
    class Club {
      private String clubName;
      private java.util.List<Player> playerList;
        Club(String name, Player... players) {
            clubName = name;
            playerList = new java.util.ArrayList<Player>(java.util.Arrays.asList(players));
        }
    }
    Then use the constructor like this:
    Java Code:
    Player player1 = new Player();
    Player player2 = new Player();
    Player player3 = new Player();
    Club myClub = new Club("My Club", player1, player2, player3);

  4. #4
    java_novice91 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Thank you guys,very much! I will try to do it now! Really appreciate it.
    Last edited by java_novice91; 05-03-2011 at 04:32 PM.

  5. #5
    java_novice91 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    What don't you get? Just use an ArrayList, its simpler to use, unless you have a reason to use a Set (i.e. no duplicates).

    Something like this should do:

    Java Code:
    class Club {
      private String clubName;
      private java.util.List<Player> playerList;
        Club(String name, Player... players) {
            clubName = name;
            playerList = new java.util.ArrayList<Player>(java.util.Arrays.asList(players));
        }
    }
    Then use the constructor like this:
    Java Code:
    Player player1 = new Player();
    Player player2 = new Player();
    Player player3 = new Player();
    Club myClub = new Club("My Club", player1, player2, player3);

    ozzyman, did you make a some kind of shortcut in this line: Club(String name, Player... players) or it should be like that?

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    The doubt about the syntax that you had previously are actually variable arguments in java also known as var args.

    Have a look here: VarArgs

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    It is a kind of shortcut, but it's legal Java. It's how you specify a variable number of arguments (aka 'varargs') - see Arbitrary Number of Arguments.

    ETA: D'oh! too late...

  8. #8
    java_novice91 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    After applying ArrayList and adding a constructor I get this mistake. It is silly, because in Players class I have the same constructor. What could it be? Or it is just because of my curve hands?
    Attached Thumbnails Attached Thumbnails Using Set in Java for putting all data in one collecion-screenshot-1.jpg  

  9. #9
    java_novice91 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    I am sorry for such small pic. Just can't do it bigger, my screenshot grabber does not want to/

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by java_novice91 View Post
    I am sorry for such small pic. Just can't do it bigger, my screenshot grabber does not want to/
    Better way, use code tags and post your code.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  11. #11
    java_novice91 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    import java.util.*;
    public class Club
    {

    private String clubName;
    private java.util.List<Player> playerList;

    Club(String name, Player ...players)
    {
    clubName = name;
    playerList = new java.util.ArrayList<Player>(java.util.Arrays.asLis t(players));
    }

    Player player1 = new Player(String personName, String personSurname, double personAge,
    double personHeight, double personWeight, int personPlayed, int personScored);
    ( Here it sais that ")" is missing).


    // Player player2 = new Player();
    // Player player3 = new Player();
    Club myClub = new Club("My Club", player1);

    }

  12. #12
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Where have you declared all these variables that you are passing in the Player constructor? You need to declare them inside the Club class.

    Plus, you don't need to mention the data types while passing parameters to the method/constructor. It should be like,

    Java Code:
    Player player1 = new Player(personName, personSurname, personAge,
    			personHeight, personWeight, personPlayed, personScored);

    Hope that makes some sense,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  13. #13
    java_novice91 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    Where have you declared all these variables that you are passing in the Player constructor? You need to declare them inside the Club class.

    Plus, you don't need to mention the data types while passing parameters to the method/constructor. It should be like,

    Java Code:
    Player player1 = new Player(personName, personSurname, personAge,
    			personHeight, personWeight, personPlayed, personScored);

    Hope that makes some sense,

    Goldest
    Oh! Yes it does! Thanks! I'll keep trying.

  14. #14
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by java_novice91 View Post
    Oh! Yes it does! Thanks! I'll keep trying.
    Best Of Luck :D

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Putting data into table - BIG PROBLEM
    By denonth in forum New To Java
    Replies: 1
    Last Post: 01-13-2011, 10:51 AM
  2. Replies: 3
    Last Post: 06-02-2010, 06:58 PM
  3. Putting Java Programs on a DVD
    By matt.monkeyboy in forum Advanced Java
    Replies: 4
    Last Post: 04-05-2010, 03:06 AM
  4. putting html into java
    By magda in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 01-27-2010, 02:16 PM
  5. Putting Image into Java applet
    By SprayAndPray69 in forum New To Java
    Replies: 1
    Last Post: 02-13-2009, 06:14 AM

Posting Permissions

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