Create a class ClubMember The class should track a member\'s name and email. The
ID: 3937785 • Letter: C
Question
Create a class ClubMember The class should track a member's name and email. The class needs a constructor and display function. The constructor must allow you to pass name and email. The display function must show member's name, and email. Write a driver that tests the class by creating an ArrayList for the ClubMember class. Add three members to the array. Print the members using the ArrayList 'for-each' loop. A sample run: java ClubMemberDriver Club members are: John Smith jsmith@att.net Mary Green mgreen@gmail.com Rex the Wonder Llama bllama@yahoo.comExplanation / Answer
import java.util.*;
//declaratoin for club member class
class ClubMember
{
//instance variable of class
String name;
String email;
//parametrized contructor
ClubMember(String n,String m)
{
name=n;
email=m;
}
//function to display m,embr
void display()
{
System.out.println(name+" "+email);
}
}
//end of club member class
//driver class
public class HelloWorld{
public static void main(String []args){
//create arraylist to hold 3 club members
ArrayList<ClubMember> members=new ArrayList<ClubMember>();
//add 3 members to arraylist
members.add(new ClubMember("John Smith","jsmitt@att.net"));
members.add(new ClubMember("Mary Green","mgreen@g1mail.comxx"));
members.add(new ClubMember("Rex The Wonder Liama","bllama@yahoo.com"));
System.out.println("Club Members are");
//display elements in arraylist
for(int i=0;i<members.size();i++)
members.get(i).display();
}
}
Sample Output:
Club Members are
John Smith jsmitt@att.net
Mary Green mgreen@g1mail.com
Rex The Wonder Liama bllama@yahoo1.com
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.