Created
October 7, 2015 06:58
-
-
Save CarlosCorderoSaavedra/f7de0e38ef5fd8c3f4ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class Person | |
{ | |
private String name; | |
private ArrayList<String> friends; | |
public Person(String name) | |
{ | |
this.name = name; | |
friends = new ArrayList<String>(); | |
} | |
/** | |
* Adds the given friend to this Person's friends list. | |
* @param friend the friend to add. | |
*/ | |
public void addFriend(Person friend) | |
{ | |
friends.add(friend.name); | |
} | |
/** | |
* Gets a list of all of this Person's friends. | |
* @return the names of the friends separated by a comma and a space. | |
* e.g. "Sara, Cheng-Han, Cay" | |
*/ | |
public String getFriends() | |
{ | |
String friendsWithBrackets = friends.toString(); | |
return friendsWithBrackets.substring(1, friendsWithBrackets.length() - 1); | |
} | |
/** | |
* Returns the names of the friends with separator between them. | |
* @param separator string to put between names. | |
* @return the names of the friends. | |
*/ | |
public String getFriends(String separator) | |
{ | |
// TODO: | |
// Create a string with all the names separated by separator. | |
// make sure that there is not an extra trailing separator. | |
// If my friends are Cay and Cheng-Han, and the separator is # | |
// It should print | |
// Cay#Cheng-Han | |
// not | |
// Cay#Cheng-Han# | |
String friendSeparator=""; | |
for (int i = 0 ; i < friends.size();i++) { | |
if(friendSeparator.equals("")){ | |
friendSeparator = friends.get(i); | |
}else{ | |
friendSeparator = friendSeparator + separator + friends.get(i) ; | |
} | |
} | |
return friendSeparator; | |
} | |
/** | |
* Gets the number of friends this Person has. | |
* @return the number of friends. | |
*/ | |
public int getNumFriends() | |
{ | |
return friends.size(); | |
} | |
/** | |
* Gets the ith friend. | |
* @param i the index of the friend you want to get. | |
* @return the ith friend's name. | |
*/ | |
public String getFriend(int i) | |
{ | |
return friends.get(i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment