Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJiten Mistry
4,698 PointsIm not sure how to complete this exercise, could someone please help
Im a bit stuck on the logic behind this, could someone please give me pointers on how to complete it please.
package com.example.model;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
public class Contact {
private String mFirstName;
private String mLastName;
private Map<String, String> mContactMethods;
public Contact(String firstName, String lastName) {
mFirstName = firstName;
mLastName = lastName;
/* This stores contact methods by name
* eg: "phone" => "(555) 555-1234"
*/
mContactMethods = new HashMap<String, String>();
}
public void addContactMethod(String method, String value) {
// TODO: Add to the contact method map
mContactMethods.put(method, value);
}
/**
* Returns the available contact methods. eg: phone, pager,
*
* @return The name of the contact methods that are available
*/
public Set<String> getAvailableContactMethods() {
// FIXME: This should return the current contact method names.
Set<String> contacts = new HashSet<String>();
mContactsMethod.add(
return contacts;
}
/**
* Returns the value for the contact method if it exists,
*
* @param methodName The name of the contact method to look up.
* @return The name of the contact methods that are available
*/
public String getContactInfo(String methodName) {
// FIXME: return the value for the passed in *methodName*
return null;
}
public String getFirstName() {
return mFirstName;
}
public String getLastName() {
return mLastName;
}
}
3 Answers
Jeremy Faith
Courses Plus Student 56,696 PointsIt looks like you are stuck on Task 2. Following the hint How about a set of keys from mContactMethods?, you can use the method keySet() to access the keys from a map. Just return mContactMethods.keySet().
return mContactMethods.keySet()
Hope this helps.
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsCan you be more descriptive about which part you aren't understanding?
Step 1 says: In this first step, modify the addContactMethod method to store the information in the mContactMethods Map.
So youll want to scroll down to
public void addContactMethod(String method, String value){
// TODO: Add to the contact method map
}
just by looking at the method you can tell by void that nothing is going to be returned from it. It will only do something inside of it when it is called.
Now I dont want to just give you the answer here, because in coding, breaking your code is how you learn. A good way to figure out what you need to do is think about what the code looks like that would call this Contact object. So I will show you what that might look like to see if you follow and if that helps. Okay? Dont copy this because this wont pass this objective. Its an example to show you whats going to happen. Ill use comments in the code.
/** creating a new Contact object stored in newContact
** Property: mFirstName = "Mike"
** Property: mLastName = "TheFrog"
** Property: mContactMethods is an empty Map here.
**/
Contact newContact = new Contact("Mike", "TheFrog");
/**
** Here I want to call the addContactMethod and be able to pass in TWO strings that get added to my Map called mContactMethods by the logic YOU add to do so. Another contact method I could of used instead could of been "email", "mikethefrog@example.com".
**/
newContact.addContactMethod("phone", "(555) 555-1234");
does this kind of help? let me know
shu Chan
2,951 PointsThank you Chris! your answer inadvertently answered my question!
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsChris Howell
Python Web Development Techdegree Graduate 49,702 PointsAhh darn, apparently I need my morning coffee. Good attention to detail.
Refer to this answer instead of mine. Mine refers to Step 1 and may mislead you.
I read the question and somehow missed step 1 was finished. Ugh.