Sort by

recency

|

156 Discussions

|

  • + 0 comments

    This code is the most right code but yet it doesn't works saying * Wrong Answer :( 1/1 test case failed * Sample Test case 0 * Compiler Message * Wrong Answer * Input (stdin) 5 Student Student Rockstar Student Hacker * Your Output (stdout) 3 1 1 * Expected Output 3 1 1

    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 []argh)
       {
          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));
       }
    }
    
  • + 0 comments

    There is an issue with the testcases for this program

    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;
    

    }

    This is the code I have written, which passes the testcase, but if I add {} after the if condition as shown below, or try to add any comment, the testcase will fail, which should not happen

    if(element instanceof Student) { a++; } //Or if you add any comment like this anywhere in the code, it will fail

  • + 0 comments

    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

  • + 0 comments

    Use this code This works perfectly 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

    This problem helps you understand runtime type checking in Java.

    No need to use .getClass() or generics — just use instanceof.

  • + 0 comments

    Here is Java InstanceOf Keyword solution - https://programmingoneonone.com/hackerrank-java-instanceof-keyword-problem-solution.html