Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Java Reflection Attributes-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java Reflection Attributes – Hacker Rank Solution
Java Reflection Attributes – Hacker Rank Solution
Objective
JAVA reflection is a very powerful tool to inspect the attributes of a class in runtime. For example, we can retrieve the list of public fields of a class using getDeclaredMethods().
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. We will append your code with the Student class before running it. 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 ...... }
You have to print all the methods of the student class in alphabetical order like this:
anothermethod getName setEmail setId ...... ...... some more methods ......
There is no sample input/output for this problem. If you press “Run Code”, it will compile it, but it won’t show any outputs.
Hint: See the oracle docs for more details about JAVA Reflection Methods and Fields
Java Reflection Attributes – Hacker Rank Solution
public class Solution { public static void main(String[] args) { Class student = Student.class; // uses class literal, not a function. Method[] methods = student.getDeclaredMethods(); /* Get names from Methods */ ArrayList<String> methodNames = new ArrayList(); for (Method method : methods) { methodNames.add(method.getName()); } /* Sort and print names */ Collections.sort(methodNames); for (String name: methodNames) { System.out.println(name); } } }