Results 1 to 11 of 11
  1. #1
    Gio!? is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default I don't get ArrayLists

    I made some code for a uni exercises but I'm stuck on one point

    I need to finish this mini-program which stores members in an arraylist.

    The arraylist has to store the members' name, month they joined and year they joined.

    You have to make a method which returns the amount of people who registered in a certain month (1 - 12 representing January - December)

    I use 3 different Classes (mainclass excluded) for this program.

    Java Code:
    public class ClubDemo
    {
        // instance variables - replace the example below with your own
        private Club club;
    
        /**
         * Constructor for objects of class ClubDemo
         */
        public ClubDemo()
        {
            club = new Club();
        }
    
        public void printNumberOfMembers()
        {
            System.out.println("The club has " +
                               club.numberOfMembers() +
                               " members.");
        }
        
        public void addMember(String name, int month, int year)
        {   
            if (name != null && name.length() > 3)
            {
                club.join(new Membership(name, month, year));
            }
            
        }
        
        public void initClub(int month)
        {
            String monthInString = null;
            
            if (month == 1)
            {
                monthInString = "January";
            }
            else if (month == 2)
            {
                monthInString = "February";
            }
            else if (month == 3)
            {
                monthInString = "March";
            }
            else if (month == 4)
            {
                monthInString = "April";
            }
            else if (month == 5)
            {
                monthInString = "May";
            }
            else if (month == 6)
            {
                monthInString = "June";
            }
            else if (month == 7)
            {
                monthInString = "July";
            }
            else if (month == 8)
            {
                monthInString = "August";
            }
            else if (month == 9)
            {
                monthInString = "September";
            }
            else if (month == 10)
            {
                monthInString = "October";
            }
            else if (month == 11)
            {
                monthInString = "November";
            }
            else if (month == 12)
            {
                monthInString = "December";
            }
            else
            {
                System.out.println("Month doesn't excist");
            }
            
            if (monthInString != null)
            {
                System.out.println("There's " + club.joinedInMonth(month) + " members registered at " + monthInString);
            }
        }
        
        
    }
    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;
        }
    }

    And here's the problem

    Java Code:
    public class Club
    {
        private ArrayList members;
        
        public Club()
        {
            members = new ArrayList<Membership>();
        }
    
        public void join(Membership member)
        {
            members.add(member);        
        }
        
        public int numberOfMembers()
        {
            return members.size();
        }
        
        public int joinedInMonth(int month)
        {
            int amount = 0;
            
            if (month < 1 || month > 12)
            {
                month = 0;
            }
            else
            {
                if (month == 0)
                {
                    System.out.println("ERROR: Month doesn't excist");
                    amount = 0;
                }
                else
                {
                    // NO IDEA WHAT TO DO HERE
                    
                    // Something with a Iterator or for-each loop?!
                }
            }
            
            return amount;
        }
        
    }
    I have no idea how to do this...

    Please help me and when you have time explain how something like this works
    Last edited by Gio!?; 05-03-2012 at 02:23 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: I don't get ArrayLists

    here's the problem
    Please explain the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Gio!? is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: I don't get ArrayLists

    Sorry, I should have been more detailed there.

    My problem is the following:

    I know how an ArrayList works if you're talking about single Strings, Integers, etc. being stored inside a ArrayList, however, with this program I need to store multiple variables. In this case a String for the name, an integer for the month and an integer for the year.

    With the method "joinedInMonth" I need to display how many people have been registered in a certain month NOT depending on the year* (which is the second parameter of the Class called "Membership").

    I don't know how to make this*...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: I don't get ArrayLists

    I need to store multiple variables.
    If the variables are related, create a small class to hold them and add an instance of that class with those different, related values to the arraylist.

    need to display how many people have been registered in a certain month
    Where is that information stored? How is it organized?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Gio!? is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: I don't get ArrayLists

    Quote Originally Posted by Norm View Post
    Where is that information stored? How is it organized?
    The information is stored in the ArrayList,

    this may seem pointless but I just want to learn things about Arraylists and I think that BlueJ gives me the perfect IDE to do this, however, this won't give me a program yet.

    For now the information should be stores like this (if I'm right) in the ArrayList:

    You need to add members by calling the "addMember" method in "ClubDemo"
    [0] Name1 3 1993
    [1] Name2 3 1989
    [2] Name3 2 1985
    [3] Name4 6 1999
    [4] Name5 3 1800
    [5] Name6 9 2012
    ...

    With the "initClub" method I want give a number which represents the number of the month, for example 3.

    { Via the method "joinedInMonth" in the class "Club" it should now check if the members are registered in the 3rd month.

    in this case 3 of them are true:

    [0] Name1 3 1993
    [1] Name2 3 1989

    [2] Name3 2 1985
    [3] Name4 6 1999

    [4] Name5 3 1800
    [5] Name6 9 2012
    ...


    "joinedInMonth" should now return "3" because 3 values are true. }

    I don't know how to make this { }*, if you would be so kind to explain me. ;)

  6. #6
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: I don't get ArrayLists

    you are on the right track. You need to iterate over the ArrayList and inspect each Membership object's month value. You will need to count how many match the month given. Your comments suggest a foreach loop, that would work fine.
    Last edited by yellowledbet; 05-03-2012 at 03:34 AM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: I don't get ArrayLists

    I don't know how to make this { }
    What are you asking how to do? What is a {}?

    ArrayLists can hold instances of any kind of class. What kind of class objects are you trying to store in an arraylist?

    Once you have added many objects to the arraylist, what are you trying to do with the contents of the arraylist?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Gio!? is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: I don't get ArrayLists

    Quote Originally Posted by yellowledbet View Post
    you are on the right track. You need to iterate over the ArrayList and inspect each Membership object's month value. You will need to count how many match the month given. Your comments suggest a foreach loop, that would work fine.
    Thank you, you at least know exactly what I mean.

    The thing is, I have not that much experience in JAVA so I know what you mean, but I don't know HOW to make something like this:

    Quote Originally Posted by yellowledbet View Post
    You need to iterate over the ArrayList and inspect each Membership object's month value..

    Java Code:
    else
                {
                    // NO IDEA WHAT TO DO HERE
                     
                    // Something with a Iterator or for-each loop?!
    
                if (??? == true)
                          {
                          amount++
                          }
                }
    Please show me how.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: I don't get ArrayLists

    Use a for loop for the length of the arraylist, get each element in turn, examine it to see if it is the one you want.
    Or use an enhanced for loop. See the tutorial for how to use one:
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Gio!? is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: I don't get ArrayLists

    Quote Originally Posted by Norm View Post
    Use a for loop for the length of the arraylist,
    Thank you very much, this is one of the two mysteries solved.

    Quote Originally Posted by Norm View Post
    examine it to see if it is the one you want.
    Here's the problem, I don't know how to examine it.

    That's what I want to ask.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: I don't get ArrayLists

    how to examine it.
    By examine I meant read the contents of the pages and look at the examples.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Arraylists
    By talia in forum New To Java
    Replies: 5
    Last Post: 01-30-2012, 06:44 PM
  2. help with ArrayLists !
    By bigbreez21 in forum New To Java
    Replies: 3
    Last Post: 11-20-2011, 08:23 PM
  3. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 04:27 AM
  4. ArrayLists for BlueJ
    By heyit'skaye in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 04:15 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
  •