-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerRank_Reflection_Attributes_with_getDeclaredMethods.java
More file actions
84 lines (72 loc) · 1.98 KB
/
HackerRank_Reflection_Attributes_with_getDeclaredMethods.java
File metadata and controls
84 lines (72 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package technicals;
import java.lang.reflect.Method;
import java.util.*;
/*
* In this problem, you will be given a class Solution in the editor. You have to fill in the incompleted lines so that it prints all the methods of another class called Student in alphabetical order.
* The Student class looks like this:
class Student{
private String name;
private String id;
private String email;
public String getName() {
return name;
}
public void setId(String id) {
this.id = id;
}
public void setEmail(String email) {
this.email = email;
}
public void anothermethod(){ }
......
......
some more methods if written
}
You have to print all the methods of the student class in alphabetical order like this:
anothermethod
getName
setEmail
setId
......
......
some more methods if written
*/
class Student{
private String name;
private String id;
private String email;
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getEmail() {
return email;
}
public void setName(String name) {
this.name = name;
}
public void setId(String id) {
this.id = id;
}
public void setEmail(String email) {
this.email = email;
}
}
class Solutions {
public static void main(String[] args){
Class student = Student.class;// uses class literal, not a function.Because if we use normal object creation process with new then we cannot use .getDeclaredmethods()
Method[] methods = student.getDeclaredMethods(); //Method type wrapper class to store the method names after retrieving them.
ArrayList<String> methodList = new ArrayList<>();
for(Method method : methods)
{
methodList.add(method.getName());
}
Collections.sort(methodList);
for(String name: methodList)
{
System.out.println(name);
}
}
}