CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Nov 2008
    Posts
    26

    Exclamation Arraylists - Return statement problem

    Hi I have this code where I can add, delete and edit members in a club. Now I am trying to create a method where the user can enter number between 1 and 12 representing the months and it should bring up all the members that registred in the month they entered. Please help I do not know how to get started, the code is below and the method that I am working with is the last one in the code:

    Code:
    import java.util.ArrayList;
    /**
     * Store details of club memberships.
     * 
     * @author () 
     * @version (01/12/2008)
     */
    public class Club
    {
        // Store arbitrary amount of members
         private ArrayList<Membership> members;
        
        /**
         * Perform and initialisation which is required
         * by the club.
         */
        public Club()
        {
            members = new ArrayList<Membership>();
        }
    
        /**
         * Add a new member to the club's list of members.
         * @param member The member object to be added.
         */
        public void join(Membership member)
        {
            members.add(member);
        }
    
        /**
         * @return The number of members (Membership objects) in
         * the club.
         */
        public int numberOfMembers()
        {
            return members.size();
        }
        
        /**
         * Determine the number of members who joined in the
         * given month.
         * @param month The month we are interested in.
         * @return The number of members.
         */
     public int joinedInMonth(int month)
        {
             HERE IS THE METHOD THAT I WANT TO USE.
        }
    }

  2. #2
    Join Date
    Nov 2008
    Posts
    26

    Re: Arraylists - Return statement problem

    I also have another class called Membership which is working perfeclty.

    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;
        }
    }

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Arraylists - Return statement problem

    You look through the list of memberships and for every one with a matching join-up month, you increment a counter... what's the problem?

    If you make an effort to write the code, we'll make an effort to help you get it right.

    If you cannot describe what you are doing as a process, you don't know what you're doing...
    W. E. Deming
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Nov 2008
    Posts
    26

    Re: Arraylists - Return statement problem

    Thats the thing I have never written a code which looks through an array and i find it really hard...im trying to practise this now because i have a project which invovles me making a naughts and crosses board and a battleships board.

  5. #5
    Join Date
    Nov 2008
    Posts
    26

    Re: Arraylists - Return statement problem

    How would i start the statment then i probably can figure out what im doing on my own.

  6. #6
    Join Date
    Nov 2008
    Posts
    26

    Re: Arraylists - Return statement problem

    I was thinking something like this what can the error be:

    Code:
     /**
         * Determine the number of members who joined in the
         * given month.
         * @param month The month we are interested in.
         * @return The number of members.
         */
     public int joinedInMonth(int month)
        {
            if(month <1 || month > 12){
                System.out.println("Error: Please type in a month number between 1 and 12");
            }else{
                return ArrayList.size;
            }
        }

  7. #7
    Join Date
    Apr 2007
    Posts
    442

    Re: Arraylists - Return statement problem

    you have List containing the members, each member has a method getMonth(). So something like this...

    Code:
    int count =0;
    for(MemberShip m: members){
     if(m.getMonth==month)count++;
    }
    return count;
    Remember that you also have a year to consider. So dealing with just month, seems silly.
    Also remember that if synchronization is needed, the members list should be syncronized.

  8. #8
    Join Date
    Nov 2008
    Posts
    26

    Re: Arraylists - Return statement problem

    Thanks and i know its silly but i need to practice this first for the months. Im pretending there is only ever 1 YEAR

  9. #9
    Join Date
    Nov 2008
    Posts
    26

    Re: Arraylists - Return statement problem

    whats is the 'm:' for?

  10. #10
    Join Date
    Apr 2007
    Posts
    442

    Re: Arraylists - Return statement problem

    m is the variable name. You read it like this, for each m of type MemberShip in collection members do the following.

  11. #11
    Join Date
    Nov 2008
    Posts
    26

    Re: Arraylists - Return statement problem

    Doesnt work even tho i changed the MemberShip to Membership.
    Code:
     public int joinedInMonth(int month)
        {
            int count =0;
            for(Membership m:members){
                if(m.getMonth == month)
                count++;
            }
                return count;
        }

  12. #12
    Join Date
    Apr 2007
    Posts
    442

    Re: Arraylists - Return statement problem

    What do you want me to do about it? Write the exception that you did recieve and did not think of studying or posting.

    m.getMonth -> m.getMonth()

    For further reference... even if there were no exceptions... Think it over, the method always returns an integer 0 or larger. Zero is returned if there are no members, the members list is null, or none of the members joined in that month. In every other case a value greater than zero is returned.

    So... "Doesnt work"... take a moment to study how DOES it work, and how it does not work. And if you then end up posting... post the error message too.

  13. #13
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Arraylists - Return statement problem

    Quote Originally Posted by Londbrok View Post
    What do you want me to do about it? Write the exception that you did recieve and did not think of studying or posting.
    This is a problem with providing sample code - it just gets copied verbatim with no thought and no understanding... it seems the more you provide, the more dependent the OP becomes and the less they think about what they're doing.

    Teaching may look like administering a dose, but even a dose must be worked on by the body if it is to cure. Each individual must cure his or her own ignorance...
    J. Barzun
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  14. #14
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Arraylists - Return statement problem

    Quote Originally Posted by Tech-Blogger View Post
    Thats the thing I have never written a code which looks through an array and i find it really hard...
    Good grief...

    Any decent book or tutorial will have examples of accessing arrays and how to write a 'for' loop. If they don't give an example of accessing an array from a loop, it's not hard to put two and two together. but even a simple Google search will give you an example at the top of the results list - try Googling "java array loop".

    If you find this hard, you'll struggle as a programmer.

    We think too much about effective methods of teaching and not enough about effective methods of learning. No matter how good teaching may be, each student must take the responsibility for his own education...
    J. Carolus S.J.
    Last edited by dlorde; December 11th, 2008 at 11:05 AM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  15. #15
    Join Date
    Apr 2007
    Posts
    442

    Re: Arraylists - Return statement problem

    Note to the original poster, and many others preceding him...

    Coding is very serious and lonely business, in the sense, that anything you recieve, be it from this forum or written by God himself, is your sole responsibility. You are the only one who controls what gets injected into your code, and what does not. If you do not understand it, do not include it until you do. So if you are not ready for processing arrays or lists, dont do it. Ask for things that you can comprehend, might seem like nagging, but it is, in fatc a very big thing. Keeps YOU in control of what YOU do.

    When things get larger, as they tend to get, either in actual business or later projects, what you will see in the effort of "fixing things", is no compile errors, but perfcectly compiling code that works. So learning to see methods, as dlorde often phrases as "contracts" is paramouth. What does it do? What does it not do, in contrast to what it is expected to do, by what ever block of code calling it.

    Judging from what you posted earlier, you seem well capable of handling errors that were present in the code. Rise to the exception!

    and yes... I posted non functional code in the earlier post, for which apologies are in order.
    Last edited by Londbrok; December 11th, 2008 at 08:22 PM.

Page 1 of 2 12 LastLast

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