Question
Hi, im having trouble getting my class club to call the method getMonth() from the class Membership. The goal in the JoinedInMonth method in the
Hi, im having trouble getting my class club to call the method getMonth() from the class Membership. The goal in the JoinedInMonth method in the club class is to search an int month in the array members to find how many members joined in the same month. Any help would be appreciated, thanks.
Here is the code in java
import java.util.ArrayList; public class Club { // Define any necessary fields here ... private ArrayList members; /** * Constructor for objects of class Club */ public Club() { // Initialise any fields here ... members = new ArrayList(); Membership member = new Membership(); //cant get to work. }
/** * 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(); } public int JoinedInMonth(int month) { int numofmembers = 0; if(month < 1 || month > 12){ System.out.println("Error invalid month." + " must be between 1 and 12"); return 0; } for(int i = 0; i < numofmembers; i++){ if(member.getMonth() == month){ //cant get it to call .getMonth. numofmembers = numofmembers + 1; } } return numofmembers; } }
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; }
//second class
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; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started