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 trialBlake Larson
13,014 PointsAnnotated Methods
Objective of challenge is in the code.
import java.lang.annotation.Method;
public class TestAnalyzer {
/**
* Counts the number of methods in the class given by `clazz` that have been annotated
* with the @Test annotation.
*/
public static int getNumAnnotatedMethods(Class<?> clazz) {
int annMethods = 0;
Method[] methods = clazz.getDeclaredMethods();
for (Method m : methods) {
if (m.isAnnotationPresent()) {
annMethods++;
}
}
return annMethods;
}
}
public class SocialNetworkTest {
public void testTweet() {
}
public void testInsta() {
}
public void testFacebook() {
}
public void testPinterest() {
}
public void testSnapchat() {
}
}
public class TestAnalyzer {
/**
* Counts the number of methods in the class given by `clazz` that have been annotated
* with the @Test annotation.
*/
public static int getNumAnnotatedMethods(Class<?> clazz) {
return 0;
}
}
1 Answer
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsHi
I. change your import
it is wrong. Check the Method
package method here or in videos of the course:
https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html
II. Check the signature of isAnnotationPresent
method. It works ONLY with argument: class of Annotation, see doc here:
And example see here:
http://www.tutorialspoint.com/java/lang/package_isannotationpresent.htm
Try that out, and let me know if you've cracked it :) You've got this