Add a test harness to SocialNetwork. java to test your new methods by doing the
ID: 3910579 • Letter: A
Question
Add a test harness to SocialNetwork. java to test your new methods by doing the following. (Note that this is not a complete test of the class; it will just give you a start in using a test harness in the Lab·You can add more tests later if you wish.) Create a SociaNetwork object (Hint: see page 40 of the Topic 1 lecture notes) Add three friends to your social network Print the contents of your list of friends Test the getNunFriends method Test the clearFriends method (how will you show that this worked?)Explanation / Answer
Complete java program:
import java.util.ArrayList;
import java.util.List;
//class SocialNetwork STARTS
public class SocialNetwork
{
//data members of the class
private int numFriends;
private String fName,lName,mail;
//to hold list of friends
private List<SocialNetwork> friendsList;
//default constructor to initialize arraylist
public SocialNetwork()
{
friendsList=new ArrayList<SocialNetwork>();
}
//parametrized constructor to initilize SocialNetwork with fName,lName,mail
public SocialNetwork(String fName, String lName,String mail)
{
this.fName=fName;
this.lName=lName;
this.mail=mail;
}
//returns the number of friends in the list
public int getNumFriends() {
numFriends=friendsList.size();
return numFriends;
}
//clears the friends list
public void clearFriends() {
friendsList.clear();
}
//add method to add friends to SocialNetwork
public void add(String fName, String lName,String mail){
friendsList.add(new SocialNetwork(fName,lName,mail));
}
//getter and setter methods for fName,lName,mail
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
//toString() method to print contents of your list of friends
public String toString() {
String output="";
for(int i=0;i<friendsList.size();i++)
{
output=output+"First Name:"+friendsList.get(i).getfName()+", Last Name:"+friendsList.get(i).getlName()+", Mail:"+friendsList.get(i).getMail()+" ";
}
return output.trim();
}
public static void main(String args[])
{
//Create a SocialNetwork object
SocialNetwork contacts = new SocialNetwork();
//Add three friends to your social network
contacts.add("Shelly","Lee","lee@uwo.ca");
contacts.add("John","Wong","wong@uwo.ca");
contacts.add("Tina","Chan","chan@uwo.ca");
//Print the contents of your list of friends
System.out.println(contacts.toString());
//Test the getNumFriends() method
System.out.println("I have "+contacts.getNumFriends()+" friends in my contact list.");
//Test clearFriends() method
contacts.clearFriends();
//Test the getNumFriends() method after clearFriends() method
System.out.println("I have "+contacts.getNumFriends()+" friends in my contact list.");
}
}
//class SocialNetwork ENDS
Output:
First Name:Shelly, Last Name:Lee, Mail:lee@uwo.ca
First Name:John, Last Name:Wong, Mail:wong@uwo.ca
First Name:Tina, Last Name:Chan, Mail:chan@uwo.ca
I have 3 friends in my contact list.
I have 0 friends in my contact list.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.