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 Instanceof keyword-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 Instanceof keyword – Hacker Rank Solution
Java Instanceof keyword – Hacker Rank Solution
Problem :
The Java instanceof operator is used to test if the object or instance is an instanceof the specified type.
In this problem we have given you three classes in the editor:
- Student class
- Rockstar class
- Hacker class
In the main method, we populated an ArrayList with several instances of these classes. count method calculates how many instances of each type is present in the ArrayList. The code prints three integers, the number of instance of Student class, the number of instance of Rockstar class, the number of instance of Hacker class.
But some lines of the code are missing, and you have to fix it by modifying only lines! Don’t add, delete or modify any extra line.
To restore the original code in the editor, click on the top left icon in the editor and create a new buffer.
Sample Input
5 Student Student Rockstar Student Hacker
Sample Output
3 1 1
Java Instanceof keyword – Hacker Rank Solution
import java.util.*; class Student{} class Rockstar{ } class Hacker{} public class InstanceOFTutorial{ static String count(ArrayList mylist){ int a = 0,b = 0,c = 0; for(int i = 0; i < mylist.size(); i++){ Object element=mylist.get(i); if(element instanceof Student) a++; if(element instanceof Rockstar) b++; if(element instanceof Hacker) c++; } String ret = Integer.toString(a)+" "+ Integer.toString(b)+" "+ Integer.toString(c); return ret; } public static void main(String []args){ ArrayList mylist = new ArrayList(); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0; i<t; i++){ String s=sc.next(); if(s.equals("Student"))mylist.add(new Student()); if(s.equals("Rockstar"))mylist.add(new Rockstar()); if(s.equals("Hacker"))mylist.add(new Hacker()); } System.out.println(count(mylist)); } }
I not to mention my buddies were following the excellent tips and tricks from your web site while instantly I got an awful suspicion I never expressed respect to the website owner for those strategies. All the boys appeared to be totally thrilled to read them and now have surely been making the most of them. I appreciate you for being considerably considerate and then for considering such amazing tips millions of individuals are really needing to understand about. My honest apologies for not expressing appreciation to sooner.