I'm having issues with looping/returning correct items in my ArrayList. When I run my program, and use my accessor method, it returns items in the terminal such as "Member@13243" instead of the actual member I'm trying to return. I have two classes, one "Family" and one "Member".
My tasks are: 2) getMembers() – returns a List of members that had been added to the Family. 3) getMembers(string s) - returns a List of members that had been added to the Family. a. An error should be output if the specified sex is not valid. 4) showMembers() – use Member's toString to print a list of Members that had been added to a Family 5) showMembers(string s) – use Member's toString to print a list of Members that had been added to a Family of the specified sex.
My code for class Family:
public class Family
{
// instance variables - replace the example below with your own
public int ID;
public String FamilyName;
public String Address;
public String City;
public String State;
public String ZipCode;
public ArrayList<Member> list;
public Family(int ID, String FamilyName, String Address, String City, String State, String ZipCode)
{
this.ID = ID;
this.FamilyName = FamilyName;
this.Address = Address;
this.City = City;
this.State = State;
this.ZipCode = ZipCode;
list = new ArrayList<Member>();
}
public void addMember(Member m ) {
list.add(m);
}
private ArrayList<Member> getMembers(){
return list;
}
public void showAll(){
System.out.println(ID);
System.out.println(Member.memberName);
System.out.println(FamilyName);
System.out.println(Member.memberSex);
System.out.println(Address);
System.out.println(City);
System.out.println(State);
System.out.println(ZipCode);
}
My code for class Member:
public class Member
{
private String memberName;
private String memberSex;
public Member(String memberName, String memberSex){
this.memberName = memberName;
this.memberSex = memberSex;
if (memberSex == "M"){
memberSex = "M";}
else if (memberSex == "F"){
memberSex = "F";}
else System.out.println("Please enter M or F for sex");
}
public String getMemberName(){
return memberName;
}
public String getMemberSex(){
return memberSex;
}
You should override toString method in your Member class.
@Override
public String toString() {
return "Member: "+memberName, "Sex "+memberSex;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.