public class MemberInfo{
// initializes memberInfo variables
private int gradYear;
private String name;
private boolean hasGoodStanding;
// MemberInfo constructor
public MemberInfo(String name, int gradYear, boolean hasGoodStanding){
this.gradYear = gradYear;
this.name = name;
this.hasGoodStanding = hasGoodStanding;
}
// getter for gradYear
public int getGradYear(){
return this.gradYear;
}
// getter for the member's standing
public boolean inGoodStanding(){
return this.hasGoodStanding;
}
}
public class ClubMembers{
// ArrayList defintions
private ArrayList<MemberInfo> memberList;
private ArrayList<MemberInfo> newMemberList;
private ArrayList<MemberInfo> returnList;
// ClubMembers constructor
public ClubMembers() {
// Initialize the memberList ArrayList in the constructor
memberList = new ArrayList<MemberInfo>();
newMemberList = new ArrayList<MemberInfo>();
returnList = new ArrayList<MemberInfo>();
}
// function to add new members
public void addMembers(String[] names, int gradYear){
//iterates through for the length of names, later does
for (int i = 0; i < names.length; i++){
// creates new MemberInfo objects based on the names given and adds that to the memberList
MemberInfo newMember = new MemberInfo(names[i], gradYear, true);
memberList.add(newMember);
}
return;
}
// function to remove members who have graduated
public ArrayList<MemberInfo> removeMembers(int year){
// iterates through memberList arraylist. This syntax means each n variable is of the type MemberInfo
for (MemberInfo n : memberList){
//checks if the person has graduated
if (n.getGradYear() > year){
newMemberList.add(n);
}
// checks if the people who have graduated are in good standings
else if (n.inGoodStanding()){
returnList.add(n);
}
}
// sets memberlist to the new list
memberList = newMemberList;
return returnList;
}
}
// Main function for testing
public class Main{
public static void main(String[] args) {
ClubMembers clubMembers = new ClubMembers();
clubMembers.addMembers(new String[]{"Toby", "Gene", "Dash"}, 2018);
clubMembers.addMembers(new String[]{"t", "g", "d"}, 2019);
clubMembers.addMembers(new String[]{"a", "b"}, 2020);
clubMembers.addMembers(new String[]{"bruh",}, 2017);
for (MemberInfo n : clubMembers.removeMembers(2018)){
System.out.println(n.getGradYear());
}
}
}
Main.main(null);
2018
2018
2018
2017
Major takeaways
- ChatGPT is very very very useful
- ArrayList<MemberInfo(closed bracket doesn’t work). This syntax is used to create an arraylist object. An arraylist is an array-like data structure that can store objects. MemberInfo is the type of object that it can store. I didn’t know this syntax beforehand.
- I also learned what a stringArray was. String[] is just an array of strings. I had never seen it before though
- I learned some syntax for working with different data types, like .add(object) and .get(index) for arraylists and .length() for stringArrays
- I also learned this syntax “for (String n : names)”. It reminded me of a forEach() loop in javascript. It esssentially iterates through each String in the stringArray called names. I later used this after learning about it in part b
// Correct code according to collegeboard
public class MemberInfo{
// initializes memberInfo variables
private int gradYear;
private String name;
private boolean hasGoodStanding;
// MemberInfo constructor
public MemberInfo(String name, int gradYear, boolean hasGoodStanding){
this.gradYear = gradYear;
this.name = name;
this.hasGoodStanding = hasGoodStanding;
}
// getter for gradYear
public int getGradYear(){
return this.gradYear;
}
// getter for the member's standing
public boolean inGoodStanding(){
return this.hasGoodStanding;
}
}
public class ClubMembers{
// ArrayList defintions
private ArrayList<MemberInfo> memberList;
// ClubMembers constructor
public ClubMembers() {
// Initialize the memberList ArrayList in the constructor
memberList = new ArrayList<MemberInfo>();
}
// function to add new members
public void addMembers(String[] names, int gradYear){
//iterates through for the length of names, later does
for (int i = 0; i < names.length; i++){
// creates new MemberInfo objects based on the names given and adds that to the memberList
MemberInfo newMember = new MemberInfo(names[i], gradYear, true);
memberList.add(newMember);
}
return;
}
// function to remove members who have graduated
public ArrayList<MemberInfo> removeMembers(int year){
ArrayList<MemberInfo> removed = new ArrayList<MemberInfo>();
for (int i = memberList.size() - 1; i >= 0; i--){
if (memberList.get(i).getGradYear() <= year){
if (memberList.get(i).inGoodStanding()){
removed.add(memberList.get(i));
}
memberList.remove(i);
}
}
return removed;
}
}
// Main function for testing
public class Main{
public static void main(String[] args) {
ClubMembers clubMembers = new ClubMembers();
clubMembers.addMembers(new String[]{"Toby", "Gene", "Dash"}, 2018);
clubMembers.addMembers(new String[]{"t", "g", "d"}, 2019);
clubMembers.addMembers(new String[]{"a", "b"}, 2020);
clubMembers.addMembers(new String[]{"bruh",}, 2017);
for (MemberInfo n : clubMembers.removeMembers(2018)){
System.out.println(n.getGradYear());
}
}
}
Main.main(null);
2017
2018
2018
2018
Checking My Answers:
a)
3/3
- I would get this point for accessing all elements of names
- I would get this point for instantiating a MemberInfo object with correct parameters
- I would get this point for adding MemberInfo objects to memberList
b)
4/6
- I’m not sure, but I thinkg I would get this point, because I do initialize an ArrayList of MemberInfo objects I just do it in other parts of the class, since I didn’t know the correct way to do it.
- I technically would not get this point, since I don’t use get(i) and instead create a new ArrayList to replace the old memberList. I think my way would still work though? Not sure.
- I would get this point, I call both methods
- I think I would get this point, technically it does behave differently for three cases so I’ll give it to myself
- I would get this point, I did identify the graduating members, well technically I identified the people not graduating
- I wouldn’t get this point. I didn’t remove from the list, I created a new list. I guess this would use more memory or something so it’s wrong?
Crossover Comments:
- Talked to much about the code too much
- Good descriptions
Fixes:
- There isn’t much else I could do for this, I just focused on shortening my descriptions of the code for the video.