cs106a – Assignment #7 – Task #2

The complete specification of assignment #7 can be found as part of the stream at iTunes.

Implement the FacePamphletProfile class

The FacePamphletProfile class encapsulates the information pertaining to one profile in the social network. That information consists of four parts:

  1. The name of the person with this profile, such as “Mehran Sahami” or “Julie Zelenski”
  2. The status associated with this profile. This is just a String indicating what the person associated with the profile is currently doing. Until it is explicitly set, the status should initially be the empty string.
  3. The image associated with that profile. This is a GImage. Until it is explicitly set, this field should initially be null since we don’t initially have an image associated with a profile.
  4. The list of friends of this profile. The list of friends is simply a list of the names (i.e., list of Strings) that are friends with this profile. This list starts empty. The data structure you use to keep track of this list is left up to you.

The last method in the starter implementation of FacePamphletProfile is a toString method whose role is to return a human-readable representation of the data stored in the profile. The general form of the string returned by this method is:

name (status): comma separated list of friend names

For example, if the variable profile contains the FacePamphletProfile data of a profile with name “Alice” whose status is “coding” and who has friends named Don, Chelsea, and Bob, then profile.toString() would return the string:

"Alice (coding): Don, Chelsea, Bob"

Add instance variables – strings for the name and the status, an image and a list for the names of the friends:

	private String name;
	private String status;
	private GImage image;
	private ArrayList<String> friends;

Initialize them in the constructor:

	public FacePamphletProfile(String name) {
		this.name = name;
		status = "";
		image = null;
		friends = new ArrayList<String>();

The setters and getters are pretty straight forward. Note as above when a local variable has the same name as a global one, it is necessary to address the instance variable directly:

	public String getName() {
		return name;

	public GImage getImage() {
		return image;

	public void setImage(GImage image) {
		this.image = image;
	public String getStatus() {
		return status;
	public void setStatus(String status) {
		this.status = status;

Check if a friend exists and act accordingly:

	public boolean addFriend(String friend) {
		if (friends.contains(friend)) return false;
		return true;

	public boolean removeFriend(String friend) {
		if (!friends.contains(friend)) return false;
		return true;

Use the iterator to loop over the friends to generate the toString:

	public Iterator<String> getFriends() {
		return friends.iterator();
	public String toString() {
		// "Alice (coding): Don, Chelsea, Bob"
		String result = getName() + " (" + getStatus() + ")";
		String next = ": ";
		Iterator<String> it = getFriends();
		while (it.hasNext()) {
			result += next + it.next();
			next = ", ";
		return result;

Use the main class for a short test of the added functionality, e.g.:

   public void actionPerformed(ActionEvent e) {
    	Object source = e.getSource();
    	if (source == addButton) {
    		if (!emptyTextField(nameField)) {
    			println("Add: " + nameField.getText());
    			FacePamphletProfile testDummy = new FacePamphletProfile(nameField.getText());
    	} else if (source == deleteButton) {

The code for this assignment is available on github.


Leave a Reply

Your email address will not be published.