Results 1 to 11 of 11
Thread: I don't get ArrayLists
- 05-03-2012, 02:20 AM #1
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
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
I have no idea how to do this...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; } }
Please help me and when you have time explain how something like this worksLast edited by Gio!?; 05-03-2012 at 02:23 AM.
- 05-03-2012, 02:44 AM #2
Re: I don't get ArrayLists
Please explain the problem.here's the problemIf you don't understand my response, don't ignore it, ask a question.
- 05-03-2012, 03:06 AM #3
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
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*...
- 05-03-2012, 03:09 AM #4
Re: I don't get ArrayLists
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.I need to store multiple variables.
Where is that information stored? How is it organized?need to display how many people have been registered in a certain monthIf you don't understand my response, don't ignore it, ask a question.
- 05-03-2012, 03:26 AM #5
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
Re: I don't get ArrayLists
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. ;)
- 05-03-2012, 03:31 AM #6
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
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.
- 05-03-2012, 03:32 AM #7
Re: I don't get ArrayLists
What are you asking how to do? What is a {}?I don't know how to make this { }
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.
- 05-03-2012, 03:42 AM #8
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
Re: I don't get ArrayLists
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:
Please show me how.Java Code:else { // NO IDEA WHAT TO DO HERE // Something with a Iterator or for-each loop?! if (??? == true) { amount++ } }
- 05-03-2012, 03:55 AM #9
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.
- 05-03-2012, 04:03 AM #10
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
- 05-03-2012, 12:50 PM #11
Similar Threads
-
Arraylists
By talia in forum New To JavaReplies: 5Last Post: 01-30-2012, 06:44 PM -
help with ArrayLists !
By bigbreez21 in forum New To JavaReplies: 3Last Post: 11-20-2011, 08:23 PM -
ArrayLists
By Freakzoyd in forum New To JavaReplies: 4Last Post: 11-12-2010, 04:27 AM -
ArrayLists for BlueJ
By heyit'skaye in forum New To JavaReplies: 1Last Post: 09-01-2010, 04:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks