Results 1 to 14 of 14
  1. #1
    amekjian is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Exclamation Array Lists and Club class

    I have tried several times but can't seem to figure it out:
    4.26(From: Objects First with JAVA | A practical introduction using Blue J):
    Complete the numberOfMembers method to return the surrent size of the collection.
    This is what I have...
    Java Code:
    import java.util.ArrayList;
    
    public class Club
    {
        private ArrayList<memberships> members;
        
        /**
         * Constructor for objects of class Club
         */
        public Club()
        {
            members = newArrayList<memberships> members;
            
        }
    
        /**
         * Add a new member to the club's list of members.
         * @param member The member object to be added.
         */
        public void join(Membership member)
        {
        }
    
        /**
         * @return The number of members (Membership objects) in
         *         the club.
         */
        public int numberOfMembers()
        {
            return members.size;
        }
    }
    Yes, it will always return 0 but the next step is adding a join method which I think I can do.
    Thanks.
    Last edited by amekjian; 11-01-2010 at 10:42 PM.

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

    Default

    OK. What is your current question though?

  3. #3
    amekjian is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    Complete the numberOfMembers method to return current size of the collection. Until you have a method to add objects to the collection this will always return zero, but it will be ready for further testing later.
    Here is the class:
    Club:(First Error:Cannot find class Memberships at line 5)
    Java Code:
    import java.util.ArrayList;
    
    public class Club extends Membership
    {
        private ArrayList<memberships> members;
        
        /**
         * Constructor for objects of class Club
         */
        public Club()
        {
            members = newArrayList<memberships> members;
            
        }
    
        /**
         * Add a new member to the club's list of members.
         * @param member The member object to be added.
         */
        public void join(Membership member)
        {
              member.add(member);
        }
    
        /**
         * @return The number of members (Membership objects) in
         *         the club.
         */
        public int numberOfMembers()
        {
            return members.size;
        }
    }
    Here is Membership class:
    Java Code:
    public class Membership 
    {
        // The name of the member.
        private String name;
        // The month in which the membership was taken out.
        private int month;
        // The year in which the membership was taken out.
        private int year;
    
        /**
         * Constructor for objects of class Membership.
         * @param name The name of the member.
         * @param month The month in which they joined. (1 ... 12)
         * @param year The year in which they joined.
         */
        public Membership(String name, int month, int year)
            throws IllegalArgumentException
        {
            if(month < 1 || month > 12) {
                throw new IllegalArgumentException(
                    "Month " + month + " out of range. Must be in the range 1 ... 12");
            }
            this.name = name;
            this.month = month;
            this.year = year;
        }
        
        /**
         * @return The member's name.
         */
        public String getName()
        {
            return name;
        }
        
        /**
         * @return The month in which the member joined.
         *         A value in the range 1 ... 12
         */
        public int getMonth()
        {
            return month;
        }
    
        /**
         * @return The year in which the member joined.
         */
        public int getYear()
        {
            return year;
        }
    
        /**
         * @return A string representation of this membership.
         */
        public String toString()
        {
            return "Name: " + name +
                   " joined in month " +
                   month + " of " + year;
        }
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    First Error:Cannot find class Memberships at line 5)

    The actual message would have said something like "Cannot find class memberships at line 5)". On line 5 you have

    Java Code:
    private ArrayList<memberships> members;

    The thing in the angle brackets is the type of thing that the list is a list of. And the compiler is saying that you don't have a class called "memberships". Check for typos.

  5. #5
    amekjian is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    Thank you very much!:)
    The problem was that I had ArrayList<memberships> when it should have been
    ArrayList<Membership>
    PS: Your were right about the error coming back as memberships as apposed to Memberships.
    So now 5 questions down 12 to go!
    If I run into any trouble I will ask.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You're welcome. Good luck with the rest!

  7. #7
    amekjian is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    It was going good until...
    Define a method in the Club class with following description:
    Java Code:
    /**
    * Remove from the Club's collection all members who joined in the given month, and return 
    * them stored in a separate collection object.
    * @param month The month of the Membership.
    * @param year The year of the Membership.
    * @return The members who joined in the given month and year.
    */
    public ArrayList<Membership> purge(int month, int year)
    If the month param. is outside of the valid range (1-12), print an error message and return a collection object with no objects stored in it.

    Note: The purge method is significantly harder to write than any of the others in this class.
    .....I don't even know where to start... This is what I have so far...:
    Java Code:
    public ArrayList<Membership> purge(int month, int year)
    {
         if((month <= 12) && (month is >= 1)){
              Membership.remove(month);
    I'm stuck after that... :(

  8. #8
    amekjian is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    I'm not even sure that
    Java Code:
    Membership.remove(month);
    works...

  9. #9
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    where is Membership.remove(<snip>) defined? I cant see it in any of the source code...

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    remove() shouldn't be a Membership method. The purging is done by the Club instance.

    * Work through the members list checking if the month matches
    * If it does remove the entry from the list and add it to a second list
    * return the second list with the deleted Membership instances

  11. #11
    amekjian is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    Would I use an iterator to check through the members list?
    Can someone give me a hint on what the first line is after the if statement?
    It would help very much.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You can use an iterator or, more easily, use the enhanced for loop.

    I can't really say what the first line of the for block is ... because you haven't written it yet! What it should do is "check if the month matches". That is it should get the month associated with the Membership instance and see if it is the same as the month passed to the purge() method. If it is not then the for loop should just continue. If the months do match then proceed as I described in my prevous post (remove the instance from the list and add it to another one that will ultimately be returned by paurge()).

  13. #13
    amekjian is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    Could a for statement be used before the if statement?

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Yes - you an use any number of either, in any order.

    The important thing is to have a plan: a precise sequence of things you are going to do to implement the purge() method. Either reply #10 or whatever you can think of. In a sense it doesn't really matter: you can only be wrong! and that's easily corrected. But it is really important that you know what you are doing (the precise steps) before you start writing code. You can't expect to be able to cobble something together from guesswork, hope and bits of code posted here.

Similar Threads

  1. Help with arrays and array lists
    By ambernicole88 in forum New To Java
    Replies: 3
    Last Post: 12-04-2009, 10:47 PM
  2. null point exception in array lists
    By c_walker in forum New To Java
    Replies: 3
    Last Post: 10-17-2009, 06:38 AM
  3. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  4. please i need the code of comparing these two array lists.
    By raj reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 04-18-2008, 08:42 AM

Tags for this Thread

Posting Permissions

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